Java sparse array
definition
Sparse array: the value of most of the elements in the array are not used (or both 0), only a small part of the use of space in the array, resulting in a waste of memory space.
Using the new compression method representation of the original array is a sparse array.
Why use a sparse array?
In order to save memory space.
The principle sparse array
The introduction of application scenarios
Developers need to develop a backgammon game, in order to achieve archiving, undo and determine the outcome of the chess game, you need to store the location of these pieces, because the board is a rectangle, you can use a two-dimensional array.
But imagine playing chess process, allocating memory location we could have a lot of unused (ie, most of the data array is 0 or the same value), so here the introduction of a sparse array.
Achieve sparse array
It must be emphasized that the sparse array is transformed by a two-dimensional array made.
The sparse array number of columns is fixed, a total of three, namely, rows, columns, corresponding to a value (a value corresponding to the two-dimensional array).
The first line is the record corresponding to the sparse array of two-dimensional array is several odd row and the number of valid values.
Each row is the next valid value recorded in the corresponding two-dimensional array of values and ranks.
Next, we use an example to represent, for example there is a two-dimensional array: according to the above method can be converted into a sparse array.
Sparse array in Java
So sparse array of two-dimensional arrays and how to convert it?
Two-dimensional array into a sparse array
- Traversing the original two-dimensional array, the number of valid data obtained sum
- The sum create a sparse array int [sum + 1] [3]
- Valid data is stored in two-digit groups sparse array (rms ranks and the number of the first acts of two-dimensional array)
public static int[][] twoToSparse(int[][] two){
int sum = 0;
// 第一步 计算非零值的个数
int row = two.length;
int col = two[0].length;
for (int i=0;i<row;i++){
for (int j=0;j<col;j++){
if(two[i][j]!=0){
sum++;
}
}
}
//创建稀疏数组
int sparseArr[][] = new int[sum+1][3];
//给稀疏数组赋值
sparseArr[0][0] = 11;
sparseArr[0][1] = 11;
sparseArr[0][2] = sum;
// 遍历二维数组,把非零的数据填充进去
int count = 0;
for (int i=0;i<row;i++){
for (int j=0;j<col;j++){
if(two[i][j]!=0){
count++;
sparseArr[count][0] = i;
sparseArr[count][1] = j;
sparseArr[count][2] = two[i][j];
}
}
}
return sparseArr;
}
Sparse array is converted into a two-dimensional array
- According to the first row sparse, create an original two-digit group
- And assigning the read data back to the original two-dimensional array can be
public static int[][] sparseToTwo(int[][] sparse){
// 将稀疏数组恢复成二维数组
int rows = sparse[0][0];
int cols = sparse[0][1];
int myCount = sparse[0][2];
int[][] reArr = new int[rows][cols];
for(int i=1;i<sparse.length;i++){
int arrrow = sparse[i][0];
int arrcol = sparse[i][1];
int arrNum = sparse[i][2];
reArr[arrrow][arrcol] = arrNum;
}
return reArr;
}
The complete code
package com.folm.dataStructure.SparseArray;
import java.io.*;
import java.util.Arrays;
public class SparseArray {
public static int[][] twoToSparse(int[][] two){
int sum = 0;
// 第一步 计算非零值的个数
int row = two.length;
int col = two[0].length;
for (int i=0;i<row;i++){
for (int j=0;j<col;j++){
if(two[i][j]!=0){
sum++;
}
}
}
//创建稀疏数组
int sparseArr[][] = new int[sum+1][3];
//给稀疏数组赋值
sparseArr[0][0] = 11;
sparseArr[0][1] = 11;
sparseArr[0][2] = sum;
// 遍历二维数组,把非零的数据填充进去
int count = 0;
for (int i=0;i<row;i++){
for (int j=0;j<col;j++){
if(two[i][j]!=0){
count++;
sparseArr[count][0] = i;
sparseArr[count][1] = j;
sparseArr[count][2] = two[i][j];
}
}
}
return sparseArr;
}
public static int[][] sparseToTwo(int[][] sparse){
// 将稀疏数组恢复成二维数组
int rows = sparse[0][0];
int cols = sparse[0][1];
int myCount = sparse[0][2];
int[][] reArr = new int[rows][cols];
for(int i=1;i<sparse.length;i++){
int arrrow = sparse[i][0];
int arrcol = sparse[i][1];
int arrNum = sparse[i][2];
reArr[arrrow][arrcol] = arrNum;
}
return reArr;
}
public static void main(String[] args){
// 创建一个原始的二维数组 11 * 11
// 0:表示没有棋子 1 表示黑色 0 表示蓝色
int chessArr1[][] = new int[11][11];
chessArr1[1][2] = 1;
chessArr1[2][3] = 2;
chessArr1[4][5] = 2;
// 输出原始的二维数组
System.out.println("原始的二维数组");
for(int[] row:chessArr1){
for(int data:row){
System.out.printf("%d\t",data);
}
System.out.println();
}
int sparseArr[][] = twoToSparse(chessArr1);
// 输出稀疏数组的形式
System.out.println();
System.out.println("得到的稀疏数组的形式:");
for(int i=0;i<sparseArr.length;i++){
System.out.printf("%d\t%d\t%d\t\n",sparseArr[i][0],sparseArr[i][1],sparseArr[i][2]);
}
int[][] newTwo = sparseToTwo(sparseArr);
System.out.println("之后的二维数组:");
for(int[] row:newTwo){
for(int data:row){
System.out.printf("%d\t",data);
}
System.out.println();
}
}
}
Good luck!