Java array 02 (multidimensional array, Arrays class, bubble sort, sparse array)

Multi-dimensional array
1. A multi-dimensional array can be regarded as an array of arrays. For example, a two-dimensional array is a special array, each element of which is a one-dimensional array
2. Two-dimensional array:

int a[][] = new int [2][5];

Code example (traversing a two-dimensional array)

package com.hao.array;

public class ArrayDemo04 {
    public static void main(String[] args) {
        int [][] array = {
   
   {1,2},{2,3},{3,4},{4,5}};
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j]+" ");
            }
            System.out.println();
        }
    }
}

Sample output
Insert picture description here

Arrays class
1. Array tool class java.util.Arrays
2. Since the array object itself does not have any method for us to call, but the API provides a tool class Arrays for us to use, so that we can perform some basic data objects Operation
3. View the JDK help document
4. The methods in the Arrays class are static methods modified by static. When in use, you can directly use the class name to call, and "do not use" the applicable object to call
with the following common functions:
1. To the array Assignment: use the fill method
2. Sort the array: use the sort method, in ascending order
3. Compare the array: use the equals method to compare whether the values ​​of the elements in the array are equal
4. Find the array elements: use the binarySearch method to divide the sorted array into two Search method operation
code example

package com.hao.array;

import java.util.Arrays;

public class ArrayDemo05 {
    public static void main(String[] args) {
        int [] array= {1,5,7,11,4,78,600,100};
        System.out.println(Arrays.toString(array));//遍历数组
        Arrays.sort(array);//数组进行升序排序
        System.out.println(Arrays.toString(array));
        Arrays.fill(array,0);//数组填充
        System.out.println(Arrays.toString(array));
    }
}

Insert picture description here
Code example

package com.hao.array;

import java.util.Arrays;

public class ArrayDemo05 {
    public static void main(String[] args) {
        int [] array= {1,5,7,11,4,78,600,100};
        System.out.println(Arrays.toString(array));//遍历数组
        Arrays.sort(array);//数组进行升序排序
        System.out.println(Arrays.toString(array));
        Arrays.fill(array,2,4, 0);//2<=下标<4填充成0
        System.out.println(Arrays.toString(array));
    }
}

Sample output code sample for
Insert picture description here
bubble sorting

package com.hao.array;

import java.util.Arrays;

public class ArrayDemo06 {
    public static void main(String[] args) {
        int [] array={2,4,6,7,22,1,14,77};
        sort(array);
        //遍历数组
        System.out.println(Arrays.toString(array));

    }
    //冒泡排序
    //1.比较数组中,两个相邻的元素,如果第一个数比第二个大,我们就交换他们的位置
    //2.每一次比较,都会产生出一个最大,或者最小的数字
    //3.下一轮则可以减少一次排序
    //4.一次循环,直到结束
    public static int[] sort(int [] array){
        int temp=0;
        for (int i = 0; i < array.length-1; i++) {
            for (int j = 0; j < array.length-1-i; j++) {
                if(array[j]>array[j+1]){
                    temp=array[j+1];
                    array[j+1]=array[j];
                    array[j]=temp;
                }
            }
        }
        return array;
    }
}

Output example
Insert picture description here
sparse array
1. When most of the elements in an array are 0 or an array of the same value, you can use a sparse array to save the array
. 2. The processing method of sparse array is:
1) How many rows of the record array are Columns, how many different values ​​are there?
2) Record the elements, ranks and values ​​with different values ​​in a small-scale array, thereby reducing the size of the program.
Example:
Insert picture description here
Code example (creation and backtracking of sparse array)

package com.hao.array;

public class ArrayDemo07 {
    public static void main(String[] args) {
        //1.创建一个11*11的二维数组  0:没有棋子  1:黑棋  2:白棋
        int [][] array1=new int[11][11];
        array1[1][2]=1;
        array1[2][3]=2;
        array1[3][4]=1;
        //输出原始数组
        System.out.println("输出原始数组:");
        for (int[] ints : array1) {
            for (int anInt : ints) {
                System.out.print(anInt+"\t");
            }
            System.out.println();
        }
        System.out.println("================================================================");//分割线
        //转换为稀疏数组
        //获取有效值的个数
        int sum=0;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if(array1[i][j]!=0){
                    sum++;
                }
            }
        }
        System.out.println("有效值的个数为"+sum);
        //2.创建一个稀疏数组的数组
        int array2[][]=new int[sum+1][3];
        array2[0][0]=11;
        array2[0][1]=11;
        array2[0][2]=sum;
        //遍历二维数组,将非零的值,存放稀疏数组中;
        int count=1;
        for (int i = 0; i < array1.length; i++) {
            for (int j = 0; j < array1[i].length; j++) {
                if(array1[i][j]!=0){
                    array2[count][0]=i;
                    array2[count][1]=j;
                    array2[count][2]=array1[i][j];
                    count++;
                }
            }
        }
        //输出稀疏数组
        for (int i = 0; i < array2.length; i++) {
            System.out.println(array2[i][0]+"\t"+array2[i][1]+"\t"+array2[i][2]+"\t");
        }
        System.out.println("================================================================");//分割线
        //还原稀疏数组
        System.out.println("还原稀疏数组:");
        int [][] array3=new int[array2[0][0]][array2[0][1]];
        for (int i = 0; i < array3.length; i++) {
            for (int j = 0; j < array3[i].length; j++) {
                array3[i][j]=0;
            }
        }
        for (int i = 1; i < array2.length; i++) {
                array3[array2[i][0]][array2[i][1]]=array2[i][2];
        }
        for (int i = 0; i < array3.length; i++) {
            for (int j = 0; j < array3[i].length; j++) {
                System.out.print(array3[i][j]+"\t");
            }
            System.out.println();
        }
    }
}

Sample output
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_51224492/article/details/111420725