方法
public class HelloWorld { public static void main(String[] args){ // //System.out.println("helloworld"); // //int[] arr = new int[]{1,2,3}; // //for ( // int[] // int[] ids = new int[5]; // int p =4; // for (int i=0;i<ids.length;i++ ){ // ids[i] =p; // p++; // System.out.println(ids[i]); // } // Scanner scanner = new Scanner(System.in); // System.out.println("请输入学生人数:"); // int number = scanner.nextInt(); // int [] scores = new int[number]; // System.out.println("请输入"+number+"个成绩"); // for (int i =0;i<scores.length;i++){ // scores[i] = scanner.nextInt(); // } // int maxScore = 0; // for (int i=0;i<=scores.length;i++){ // if(scores[i] > maxScore){ // maxScore = scores[i]; // } // } // char level; // for (int i = 0;i<scores.length;i++){ // if (maxScore-scores[i]<=10){ // level ='A'; // }else if (maxScore-scores[i]<=20){ // level ='B'; // }else if (maxScore-scores[i]<=30){ // level ='C'; // }else { // level ='D'; // } // System.out.println("stubent" + scores[i] + ",grade is"+level); // }// Scanner scan = new Scanner(System.in);s // Scanner scan = new Scanner(System.in); // System.out.print("请输入学生个数:"); // int cx = scan.nextInt(); // int[] Grades = new int[cx]; // for (int i = 0 ;i <cx ;){ // i++; // System.out.print("请输入学生"+i+"成绩:"); // int sxi = scan.nextInt(); // Grades[i-1] = sxi; // //System.out.println(Grades[i-1]); // } // int masx =0; // for (int i=0; i<Grades.length;i++){ // if (masx<Grades[i]){ // masx=Grades[i]; // } // } // System.out.println("最高成绩"+masx); // for (int i= 0;i<Grades.length;i++){ // if(Grades[i]>=90){ // System.out.println(Grades[i]+"优秀"); // }else if(Grades[i]>=80){ // System.out.println(Grades[i]+"好"); // }else if(Grades[i]>=70){ // System.out.println(Grades[i]+"良好"); // } else if (Grades[i]>=60) { // System.out.println(Grades[i]+"勉强及格"); // }else{ // System.out.println(Grades[i]+"继续努力"); // } // } //判断两个数组是否相等 int[] cx = new int[]{1,2,3,4}; int[] cx2 = new int[]{1,2,8,5,3,9,4}; boolean is = Arrays.equals(cx,cx2); System.out.println(is); //输出数组信息 System.out.println(Arrays.toString(cx)); //将指定值填充数组中 Arrays.fill(cx,10); System.out.println(Arrays.toString(cx)); //对数组排序 Arrays.sort(cx2); System.out.println(Arrays.toString(cx2)); int[] cx3 = new int[]{-12,8,78,34,-87,72,91}; Arrays.sort(cx3); System.out.println(Arrays.toString(cx3)); //根据数组元素找下标;提前排好序,否则会出错 int index = Arrays.binarySearch(cx3,78); int index2 = Arrays.binarySearch(cx3,72); System.out.println(index); System.out.println(index2); } } 测试 false [1, 2, 3, 4] [10, 10, 10, 10] [1, 2, 3, 4, 5, 8, 9] -7 5
数组常见异常
public class HelloWorld { public static void main(String[] args){ int[] cz = new int[]{1,2,3,4,5}; for (int i = 0 ;i<=cz.length;i++){ System.out.println(cz[i]); } } } 1 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 地址越界异常 2 at com.chenxi.java.HelloWorld.main(HelloWorld.java:7) 3 4 5
空指针异常
int [] cx = new int[]{2,8,6}; cx = null; System.out.println(cx[0]); 1 Exception in thread "main" java.lang.NullPointerException 2 at com.chenxi.java.HelloWorld.main(HelloWorld.java:11) 3 4 5