Java basics --- two-dimensional array

Java basics-two-dimensional array

[Foreword: The same is true for multiple loops, the array can also be two-dimensional, three-dimensional or even more, but generally no more than 3 layers]

One, the definition of a two-dimensional array

The same dimension array is just adding [], for example:
1.int[][] a = new int[3][4];
2.int[][] s = new int[4][];
s[ 0][0] = 1;
s[0][1] = 13;
s[0][2] = 21;
s[0][3] = 4;
[Note: The row must be written, the column does not need to be written]

Second, use the Arrays class

Insert picture description here

//具体使用方式
int[] a = {1,2,3};
int[] b = {1,2,3};
a=b;
System.out.println(a.equals(b));
System.out.println(Arrays.equals(a,b));
Arrays.sort(a);
Arrays.toString(a);
Arrays.copyOf(b,3);
Arrays.fill(a,3);
Arrays.binarySearch(a,2);

Three, common problems

1. Find the scores and average scores of 3 classes with 5 students in each class

int[][] arr = new int[3][5];                //3个班5名学员成绩
for (int i = 0; i < arr.length; i++) {      //二维数组出入
    for (int j = 0; j < arr[i].length; j++) {
        arr[i][j]=(int)(Math.random()*100);
    }
}
for (int i = 0; i < arr.length; i++) {      //二维数组输出
    System.out.println(Arrays.toString(arr[i]));
}
for (int i = 0; i < arr.length; i++) {
    int sum = 0;
    double avg = 0;
    for (int j = 0; j < arr[i].length; j++) {
        sum+=arr[i][j];
    }
    System.out.println("第"+(i+1)+"班总成绩"+sum);
    System.out.println("第"+(i+1)+"班平均分"+sum/arr[i].length);
}

The results are shown in the figure
Insert picture description here
2. First enter the class size, and then enter the scores of 3 subjects other than the number of languages, output the top two in mathematics, the second two in Chinese, and the average score in English

		 //输入
        System.out.println("请输入班级人数");
        Scanner sc = new Scanner(System.in);
        int number = sc.nextInt();
        double score[][] = new double[number][3];
        for(int i=0;i<number;i++){
            for (int j = 0; j < 3; j++) {
                System.out.println("请输入第"+(i+1)+"个人的成绩");
                score[i][j]=sc.nextDouble();
            }
        }
        for (int i = 0; i <score.length ; i++) {
            System.out.println(Arrays.toString(score[i]));
        }

        //计算
        double[] chinese_score=new double[number];      //语文成绩
        double[] math_score=new double[number];         //数学成绩
        double[] english_score=new double[number];      //英语成绩
        for (int i = 0; i < score.length; i++) {
            chinese_score[i] = score[i][0];
            math_score[i] = score[i][1];
            english_score[i]  =score[i][2];
        }
        Arrays.sort(chinese_score);
        System.out.println("语文成绩前两名的成绩"+chinese_score[number-1]+"\t"+chinese_score[number-2]);
        Arrays.sort(math_score);
        System.out.println("数学成绩后两名的成绩"+math_score[0]+"\t"+math_score[1]);
        int sum = 0;                                   //英语成绩总分
        for (int i = 0; i < english_score.length; i++) {
            sum+=english_score[i];
        }
        System.out.println("英语成绩平均分"+sum/number);

The result is shown in Figure
Insert picture description here
3. First enter a poem, put it into a two-dimensional array, and then find the character with the most occurrences, and replace it with 0

char[][] poe = new char[4][5];
        System.out.println("请输入5言绝句");
        Scanner input = new Scanner(System.in);
        for (int i = 0; i < poe.length; i++) {
            String s = input.next();
//            poe[i] = Arrays.copyOf(s.toCharArray(),5);    //这一种方式也可以
            for (int j = 0; j < poe[i].length; j++) {
                poe[i][j] = s.charAt(j);            //输入
            }
        }

        for (int i = 0; i < poe.length; i++) {      //输出
            System.out.println(Arrays.toString(poe[i]));
        }

        char[] temp = new char[20];                     //创建新一维数组用来存放二维数组的数据
        int temp_count = 0;                             //一维数组的长度
        for (int i = 0; i < poe.length; i++) {
            for (int j = 0; j < poe[i].length; j++) {   //变成一维数组
                temp[temp_count++] = poe[i][j];
            }
        }
        System.out.println(Arrays.toString(temp));     //输出变成一维数组后的数据

        int[] cnt = new int[temp.length];              //每个元素出现的次数累加
        Arrays.fill(cnt,1);
        for (int i = 0; i < temp.length; i++) {
            for(int j=i+1;j<temp.length;j++){
                if(temp[i]==temp[j]){
                    cnt[i]++;
                }
            }
        }

        int max = 0;                                //定义出现次数最大值
        int pos = 0;                                //定义出现次数最多的字符的下标
        char a = ' ';                               //等会根据下标取出哪个字符,然后到二维数组中替换
        for (int i = 0; i < cnt.length; i++) {
            if(max<cnt[i]){                         //出现次数最大值交换
                max = cnt[i];
                pos = i;
            }
        }
        a = temp[pos];                              //存储出现次数最多的字符

        System.out.println("被替换的字符为:"+a);
        for (int i = 0; i < poe.length; i++) {
            for (int j = 0; j < poe[i].length; j++) {
                if(a==poe[i][j]){                    //在二维数组中遍历出现最高次数的值,将它替换
                    poe[i][j] = '0';
                }
            }
        }

        System.out.println("替换过后");
        for (int i = 0; i < poe.length; i++) {      //输出替换后的结果
            System.out.println(Arrays.toString(poe[i]));
        }

The result is shown in the figure
Insert picture description here

Four, dichotomy search

//定义中间位置,与输入的数比较
//比中间数大,头变成中间位置+1
//比中间数小,尾变成中间位置-1
//如果只有1个元素,直接比较
//只要找到就立刻结束

int a[]={10,30,49,34,78,33,2};
int num = new Scanner(System.in).nextInt();
int low = 0;
int high = a.length-1;
while (low<=high) {
    if(high==low){
        if(num==a[0]){
            System.out.println(num+"在"+"0处");
            break;
        }else {
            System.out.println("不在");
            System.exit(-1);
        }
    }
    int mid = (high + low) / 2;
    if (num > a[mid]) {
        low = mid + 1;
    } else if (num < a[mid]) {
        high = mid - 1;
    } else {
        System.out.println(num+"在"+mid);
        break;
    }
}

Five, sort

See the next chapter for details.

Guess you like

Origin blog.csdn.net/qq_43288259/article/details/112545906