Article Directory
2. Sparse Arrays
1 Introduction
Sparse array is to compress redundant redundant data.
When most elements in an array are 0, or an array of the same value, you can use a sparse array to save the array.
2. use
- Record how many rows and columns the array has, and how many different values there are
- Record the rows, columns and values of elements with different values in a small-scale array, thereby reducing the size of the program
3. Icon
4. Code implementation
- the code
/**
* 五子棋保存棋子
* 二维数组 --> 稀疏数组
* [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
* [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
* [0, 0, 0, 2, 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, 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, 0]
*/
public class Test1 {
public static void main(String[] args) throws IOException {
String filePath = "F:\\projects\\算法与数据结构\\代码\\src\\一__稀疏数组\\map.data";
int[][] arr = readFile(filePath);
parseSparseArr(arr);
}
// 读取文件 --> 获取稀疏数组
public static int[][] readFile(String filePath) throws IOException {
// 1. 读取文件,获取二维数组
BufferedReader fileReader = new BufferedReader(new FileReader(filePath));
int line = 0;
String s = null;
int[][] sparseArr;
while ((s = fileReader.readLine()) != null) {
line++;
}
sparseArr = new int[line][];
line = 0;
BufferedReader fileReader2 = new BufferedReader(new FileReader(filePath));
while ((s = fileReader2.readLine()) != null) {
String[] split = s.split(",");
int[] arr = new int[]{
Integer.parseInt(split[0].trim()), Integer.parseInt(split[1].trim()), Integer.parseInt(split[2].trim())};
if(line == 0){
sparseArr[0] = arr;
line++;
}else {
sparseArr[line] = arr;
line++;
}
}
fileReader.close();
return sparseArr;
}
// 二维数组 --> 稀疏数组
public static int[][] parseArr(int[][] arr) {
int count = 1;
for (int[] value : arr) {
for (int j = 0; j < value.length; j++) {
if (value[j] != 0) {
count++;
}
}
}
int[][] sparseArr = new int[count][];
int c = 1;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j] != 0) {
sparseArr[c] = new int[]{
j, i, arr[i][j]};
c++;
}
}
}
sparseArr[0] = new int[]{
arr.length, arr[0].length, count};
// 打印
for (int[] ints : sparseArr) {
System.out.println(Arrays.toString(ints));
}
return sparseArr;
}
// 稀疏数组 --> 二维数组
public static void parseSparseArr(int[][] sparseArr) {
// 初始化二维数组
int[][] arr = new int[sparseArr[0][0]][sparseArr[0][1]];
// 获取有值的数量
int count = sparseArr[0][2];
// 赋值
for (int i = 1; i < count; i++) {
int[] ints = sparseArr[i];
arr[ints[0]][ints[1]] = ints[2];
}
for (int[] ints : arr) {
System.out.println(Arrays.toString(ints));
}
}
}
11, 11, 5
1, 2, 1
2, 3, 2
4, 4, 2
6, 7, 2
- Review code (simplified)
public class 复盘 {
public static void main(String[] args) {
// 1. 创建一个普通二维数组
int[][] attr = new int[10][10];
attr[2][3] = 1;
attr[5][6] = 1;
attr[7][7] = 1;
// 2. 打印原始数组
System.out.println("------------------------------");
for (int[] ints : attr) {
System.out.println(Arrays.toString(ints));
}
int[][] parseAttr = parseAttr(attr);
// 3. 打印稀疏数组
System.out.println("------------------------------");
for (int[] ints : parseAttr) {
System.out.println(Arrays.toString(ints));
}
int[][] attrArray = parseSparseAttr(parseAttr);
// 4. 打印普通数组
System.out.println("------------------------------");
for (int[] ints : attrArray) {
System.out.println(Arrays.toString(ints));
}
}
// 数组 --> 稀疏数组
public static int[][] parseAttr(int[][] attr) {
// 1. 获取有效数值个数
int count = 0;
for (int[] ints : attr) {
for (int anInt : ints) {
if (anInt != 0) count++;
}
}
// 2. 初始化稀疏数组
int[][] sparseArray = new int[count+1][3];
// 3. 获取有效数值并且赋值稀疏数组
// 第一行 row col value
sparseArray[0] = new int[]{
attr.length, attr[0].length, count};
count = 1;
for (int i = 0; i < attr.length; i++) {
for (int j = 0; j < attr[i].length; j++) {
if (attr[i][j] != 0) {
sparseArray[count] = new int[]{
j, i, attr[i][j]};
count++;
}
}
}
return sparseArray;
}
// 稀疏数组 --> 数组
public static int[][] parseSparseAttr(int[][] sparseAttr) {
// 1. 初始化普通数组
int[][] attr = new int[sparseAttr[0][1]][sparseAttr[0][0]];
// 2. 遍历稀疏数组给数组赋值
for (int i = 1; i < sparseAttr.length; i++) {
attr[sparseAttr[i][1]][sparseAttr[i][0]] = sparseAttr[i][2];
}
return attr;
}
}