稀疏数组 (针对二维数组的优化方案)
- 用途:用于减少内存占用/序列化时的传输数据的大小等;
- 定义方式:
1. 永远是 3列
2. sparse[0][0]==origin.length;sparse[0][1]==origin[0].length;
3. 后续的 sparse[row][0]==valueRow;sparse[row][1]= valueCol ;sparse[row][2] == value;
其中,[row]从1开始递增。
关于稀疏数组的介绍就这些吧,下面是代码实现:
public class SparseArrays {
private SparseArrays() {
System.out.println("invoke constructor...");
}
public static int[][] fromSparseArr(int[][] sparse, int ignore) {
if (sparse == null) {
return null;
}
if (sparse.length <= 0 || sparse[0].length != 3) {
return null;
}
int[][] arr = new int[sparse[0][0]][sparse[0][1]];
for (int i = 0; i < arr.length; ++i) {
// each row
for (int j = 0; j < arr[0].length; ++j) {
// each column
arr[i][j] = ignore;
}
}
for (int i = 1; i < sparse.length; ++i) {
// row [1,last]
arr[sparse[i][0]][sparse[i][1]] = sparse[i][2];
}
return arr;
}
public static int[][] toSparseArr(int[][] arr, int ignore) {
if (arr == null) {
System.out.println("origin arr is null.");
return null;
}
int count = 0;
for (int[] ints : arr) {
// each row
for (int value : ints) {
// each column
if (ignore != value) {
count++;
}
}
}
if (count == 0) {
System.err.println("no special value in origin arr.");
return null;
}
int[][] sparseArr = new int[count + 1][3];
sparseArr[0][0] = arr.length;
sparseArr[0][1] = arr[0].length;
sparseArr[0][2] = count;
int sRow = 1;
for (int i = 0; i < arr.length; ++i) {
// each row
for (int j = 0; j < arr[0].length; ++j) {
// each column
if (ignore != arr[i][j]) {
sparseArr[sRow][0] = i;
sparseArr[sRow][1] = j;
sparseArr[sRow][2] = arr[i][j];
sRow++;
}
}
}
return sparseArr;
}
public static void main(String[] args) {
int[][] arr = {
{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, 1, 0, 0},
{0, 0, 0, 1, 0, 0},
{1, 0, 0, 2, 0, 0},
{5, 0, 0, 0, 3, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
};
int[][] sparseArr = toSparseArr(arr, 0);
System.out.println("origin==" + formatArr(arr));
System.out.println("origin---" + arr.length + " , " + arr[0].length);
System.out.println("======");
System.out.println("sparse==" + formatArr(sparseArr));
System.out.println("sparse---" + sparseArr.length + " , " + sparseArr[0].length);
System.out.println("#######");
System.out.println("restored: " + formatArr(fromSparseArr(sparseArr, 0)));
}
private static String formatArr(int[][] arr) {
if (arr == null) {
return "null";
}
StringBuilder sb = new StringBuilder("{\n\t");
for (int[] ints : arr) {
for (int value : ints) {
sb.append(value);
sb.append(", ");
}
int last = sb.lastIndexOf(",");
sb.deleteCharAt(last);
sb.append("},\n\t");
}
int last = sb.lastIndexOf("\t");
sb.deleteCharAt(last);
sb.append("}");
return sb.toString();
}
}
为了直观感受,添加了打印的函数,用于对比和校验算法的正确性。
输出如下:
origin=={
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, 1, 0, 0 },
0, 0, 0, 1, 0, 0 },
1, 0, 0, 2, 0, 0 },
5, 0, 0, 0, 3, 0 },
0, 0, 0, 0, 0, 0 },
0, 0, 0, 0, 0, 0 },
}
origin---10 , 6
======
sparse=={
10, 6, 6 },
4, 3, 1 },
5, 3, 1 },
6, 0, 1 },
6, 3, 2 },
7, 0, 5 },
7, 4, 3 },
}
sparse---7 , 3
#######
restored: {
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, 1, 0, 0 },
0, 0, 0, 1, 0, 0 },
1, 0, 0, 2, 0, 0 },
5, 0, 0, 0, 3, 0 },
0, 0, 0, 0, 0, 0 },
0, 0, 0, 0, 0, 0 },
}