1.打印数组内容:
public static void printArray(int[] arr) { if(arr!=null&&arr.length>0){ System.out.print("该数组的内容是:["); for (int i = 0; i < arr.length; i++) { System.out.print(i== arr.length-1?arr[i]:arr[i]+","); } System.out.print("]"); }else{ System.out.print("该数组是空数组"); } }
2.找到数组中的索引(在数组中的元素不重复的情况下):
public static int findIndexOnArray(int[] arr,int number){ int index = -1; OUT:for (int i = 0; i < arr.length; i++) { if(arr[i]==number){ index=i; break OUT; } } return index; }
3.判断两个数组是否完全一样:
public static boolean compare(int[] arr,int[] brr){ if(arr.length!=brr.length){ return false; } for (int i = 0; i < arr.length; i++) { if(arr[i]!=brr[i]){ return false; } } return true; }
4.创建一个不重复的随机数组:
public static int[] creatCode() { Random rd = new Random(); int[] arr = new int[7]; for (int i = 0; i < arr.length - 1; i++) { while (true) { int date = rd.nextInt(33) + 1; boolean flag = true; for (int j = 0; j < i; j++) { if (date == arr[j]) { flag = false; break; } } if (flag) { arr[i] = date; break; } } } arr[arr.length - 1] = rd.nextInt(16) + 1; return arr; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)