数组的Arrays 工具类

方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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

  数组常见异常

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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

  空指针异常

1
2
3
4
5
6
7
8
9
10
11
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

  

posted @   烟雨楼台,行云流水  阅读(74)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
点击右上角即可分享
微信分享提示