JAVA的8个集合简单工具类实例代码
JAVA的8个集合简单工具类实例代码
前言:
JAVA的8个集合简单工具类实例代码,如果对你有帮助就看看。
正文:
本类一共集合8个方法如下:
1.获取INT型数组的最大值
2.获取INT型数组的最小值
3.INT型数组的排序
4.INT型数组位置查询
5.输出INT型数组
6.INT型数组的左右位置替换
7.一个能帮你做出决定的功能
8.二进制八进制十六进制转换
实例代码:
public class test4 {
public static void main(String[] agrs){
int[] arr = {3,4,1,8};
int max = ArrayTool.getMax(arr);
System.out.println(max);
int min = ArrayTool.getMin(arr);
System.out.println(min);
//排序
ArrayTool.paixu(arr);
//输出
ArrayTool.shuchu(arr);
//决定
ArrayTool.jueding();
}
}
//定义一个工具类
class ArrayTool{
//获取最大值
public static int getMax(int[] arr){
int max =0;
for (int i=0; i<=arr.length-1; i++){
if (arr[i] > max){
max = arr[i];
}
}
return max;
}
//获取最小值
public static int getMin(int[] arr){
int min = arr[0];
for (int i=0; i<=arr.length-1; i++){
if (arr[i] < min){
min = arr[i];
}
}
return min;
}
//给数组从小到大排序
public static void paixu(int[] arr){
int sun = arr[0];
for (int i=0; i<arr.length; i++){
//比对第一个数字和第二个数组的大小
for (int xx=i+1;xx<arr.length;xx++){
if(arr[i] > arr[xx]){
sun = arr[i];
arr[i] = arr[xx];
arr[xx] = sun;
}
}
System.out.print(arr[i]);
}
System.out.println();
}
//数组查找封装函数
public static int jiao(int[] arr , int key){
for (int x = 0; x<arr.length; x++){
if (arr[x] == key){
return x;
}
}
return -1;
}
//定义输出函数
public static void shuchu(int[] arr){
System.out.print("[");
for(int i=0;i<arr.length; i++){
if (i == arr.length-1){
System.out.print(arr[i]+"]");
}else{
System.out.print(arr[i]+", ");
}
}
System.out.println();
}
//把换位单独定义一个函数功能
public static void huan(int[] arr , int a , int b){
int sun = arr[a];
arr[a] = arr[b];
arr[b] = sun;
}
//定义一个帮你做决定功能
public static void jueding(){
int[] jieguo = {1,0,1,0,1,0,1,0,1,0,1,0};
int yes = 0;
int no = 0;
System.out.println("--------------------------------------------");
for (int i=0; i<5; i++){
final double xiaoshu = Math.random();
final int suiji = (int)(xiaoshu*12);
if (jieguo[suiji] == 1){
yes = yes + 1;
}else{
no = no + 1;
}
System.out.println(yes+":"+no);
}
if (yes > no){
System.out.println("YES胜出,答案:是");
System.out.println("--------------------------------------------");
}else{
System.out.println("NO胜出,答案:否");
System.out.println("--------------------------------------------");
}
}
//进制转换
/*
int num:代表需要进制转换的数字
int yu:代表&上几 15代表16进制&的数字 7代表8进制&的数字 1代表2进制&的数字
int yi:代表位移的数 4代表16进制位移的数 3代表8进制位移的数 1代表2进制位移的数字
实例:
cha(60 , 15 , 4);
System.out.println("------------------------------------");
cha(60 , 7 , 3);
System.out.println("------------------------------------");
cha(60 , 1 ,1);
*/
public static void cha(int num , int yu , int yi){
char[] cha = {'0' ,'1' ,'2' , '3' ,
'4' ,'5' , '6' , '7',
'8' , '9' , 'A',
'B' , 'C' , 'D' ,'E' , 'F',
};
char[] arr = new char[32];
int pos = arr.length-1;
while(num != 0){
int temp = num & yu;
arr[pos] = cha[temp];
num = num >> yi;
pos--;
}
for (int i=0; i<=arr.length-1; i++){
System.out.print(arr[i]);
}
}
}资源均来自第三方,谨慎下载,前往第三方网站下载 解压密码:www.xkwo.com 解压密码:www.xkwo.com 解压密码:www.xkwo.com 解压密码:www.xkwo.com 解压密码:www.xkwo.com 解压密码:www.xkwo.com 解压密码:www.xkwo.com 解压密码:www.xkwo.com 解压密码:123456 或者 aidezy.com
本站所有文章、数据、图片均来自互联网,一切版权均归源网站或源作者所有。
如果侵犯了你的权益请来信告知我们删除。邮箱:737597453@qq.com
上一篇:JAVA中主函数的作用详细解析


