[Java array learning] sparse array

Sparse array

Requirement: Write a Gobang game with the functions of saving, exiting and replaying

Introduction:

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 to the array.

The way to deal with sparse arrays is:

The record array has a total of several rows and columns, and how many different values ​​are there

Record the elements, ranks and values ​​with different values ​​in a small-scale array, thereby reducing the size of the program

public static void main(String[] args) {
    
    

    int[][] array1 =new int[11][11];

    array1[1][2]=333;
    array1[2][3]=444;
    array1[3][4]=555;
    array1[4][5]=666;
    array1[5][6]=777;
    array1[6][7]=888;
    //输出原始数组
    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 < array1.length; i++) {
    
    
        for (int j = 0; j < array1[i].length; j++) {
    
    
            if (array1[i][j]!=0){
    
    
                sum++;
            }
        }
    }
    System.out.println("有效值的个数:"+sum);

    System.out.println("================================");
    //创建一个稀疏数组的数组
    int[][] array2 =new int[sum+1][3];
    array2[0][0]=11;
    array2[0][1]=11;
    array2[0][2]=sum;

    //遍历二维数组,将非零的值存入稀疏数组中
    int count=0;//计数作用
    for (int i = 0; i < array1.length; i++) {
    
    
        for (int j = 0; j < array1[i].length; j++) {
    
    
            if (array1[i][j]!=0){
    
    
                count++;
                array2[count][0]=i;
                array2[count][1]=j;
                array2[count][2]=array1[i][j];
            }
        }
    }

    //输出稀疏数组
    System.out.println("稀疏数组:");
    for (int i = 0; i < array2.length; i++) {
    
    
        for (int j = 0; j < array2[i].length; j++) {
    
    
            System.out.print(array2[i][j]+"\t");
        }
        System.out.println("");
    }
    System.out.println("============================");
    System.out.println("还原 ");
    //还原,并且把0 项全转换为1
    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++) {
    
    
            for (int k = 1; k < array2.length; k++) {
    
    
                if (i==array2[k][0]&&j==array2[k][1]){
    
    
                    array3[i][j]=array2[k][2];
                    System.out.println(array3[i][j]);
                    break;    //这里的break注意:不能掉!!!!!!!!!!!!!!
                }else{
    
    
                    array3[i][j]=1;
                }
            }
        }
    }
    //输出
    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("");
    }
    }
/*输出结果为:
输出原始数组
0	0	0	0	0	0	0	0	0	0	0	
0	0	333	0	0	0	0	0	0	0	0	
0	0	0	444	0	0	0	0	0	0	0	
0	0	0	0	555	0	0	0	0	0	0	
0	0	0	0	0	666	0	0	0	0	0	
0	0	0	0	0	0	777	0	0	0	0	
0	0	0	0	0	0	0	888	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
=================
有效值的个数:6
================================
稀疏数组:
11	11	6	
1	2	333	
2	3	444	
3	4	555	
4	5	666	
5	6	777	
6	7	888	
============================
还原 
333
444
555
666
777
888
1	1	1	1	1	1	1	1	1	1	1	
1	1	333	1	1	1	1	1	1	1	1	
1	1	1	444	1	1	1	1	1	1	1	
1	1	1	1	555	1	1	1	1	1	1	
1	1	1	1	1	666	1	1	1	1	1	
1	1	1	1	1	1	777	1	1	1	1	
1	1	1	1	1	1	1	888	1	1	1	
1	1	1	1	1	1	1	1	1	1	1	
1	1	1	1	1	1	1	1	1	1	1	
1	1	1	1	1	1	1	1	1	1	1	
1	1	1	1	1	1	1	1	1	1	1	
*/

Guess you like

Origin blog.csdn.net/weixin_44302662/article/details/115057459