稀疏素组
- 当一个数组中大部分元素为0,或者为同一个值的数组时,可以使用稀疏数组来保存该数组。
- 稀疏数组的处理方法是:
– 记录数组一共有几行几列,有多少个不同的值
– 把具有不同值的元素的行列及值记录在一个小规模的数组中,从而缩小程序的规模
import java.awt.Desktop;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class SparseArray {
public static void main(String[] args) throws Exception {
int chessArr1[][] = new int[11][11];
chessArr1[1][2] = 2;
chessArr1[2][3] = 3;
for (int[] row : chessArr1) {
for (int data : row) {
System.out.printf("%d\t", data);
}
System.out.println();
}
int sum = 0;
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (chessArr1[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 < 11; i++) {
for (int j = 0; j < 11; j++) {
if (chessArr1[i][j] != 0) {
count++;
sparseArr[count][0] = i;
sparseArr[count][1] = j;
sparseArr[count][2] = chessArr1[i][j];
}
}
}
File file = new File("E:\\map.data");
FileOutputStream fos = new FileOutputStream(file);
OutputStreamWriter write = new OutputStreamWriter(fos, "UTF-8");
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]);
if (i == sparseArr.length - 1) {
write.append(sparseArr[i][0] + "," + sparseArr[i][1] + "," + sparseArr[i][2]);
} else {
write.append(sparseArr[i][0] + "," + sparseArr[i][1] + "," + sparseArr[i][2] + ",");
}
}
System.out.println("写入文件中...");
write.close();
fos.close();
System.out.println("打开文件中...");
Desktop.getDesktop().open(file);
System.out.println("------------------------------先读取_map.data");
FileInputStream fis = new FileInputStream(file);
InputStreamReader reader = new InputStreamReader(fis, "UTF-8");
StringBuffer sb = new StringBuffer();
while (reader.ready()) {
sb.append((char) reader.read());
}
System.out.println(sb.toString());
reader.close();
fis.close();
System.out.println("------------------------------恢复成稀疏数组_sparseArrHf");
String[] str = sb.toString().split(",");
int sparseArrHf[][] = new int[str.length / 3][3];
int i = 0;
for (String s : str) {
sparseArrHf[(i - (i % 3)) / 3][i % 3] = Integer.parseInt(s);
i++;
}
System.out.println("------------------------------再恢复成二维数组_chessArr22");
int chessArr22[][] = new int[sparseArrHf[0][0]][sparseArrHf[0][1]];
for (int i3 = 1; i3 < sparseArrHf.length; i3++) {
chessArr22[sparseArrHf[i3][0]][sparseArrHf[i3][1]] = sparseArrHf[i3][2];
}
System.out.println();
for (int[] row : chessArr22) {
for (int data : row) {
System.out.printf("%d\t", data);
}
System.out.println();
}
System.out.println("--------------------------------------------------------恢复完成");
int chessArr2[][] = new int[sparseArr[0][0]][sparseArr[0][1]];
for (int i3 = 1; i3 < sparseArr.length; i3++) {
chessArr2[sparseArr[i3][0]][sparseArr[i3][1]] = sparseArr[i3][2];
}
System.out.println();
System.out.println("恢复后的二维数组");
for (int[] row : chessArr2) {
for (int data : row) {
System.out.printf("%d\t", data);
}
System.out.println();
}
}
}