稀疏数组的实现
public class SparseArray {
public static void main(String[] args) {
int chessArr1[][] = new int[11][11];
chessArr1[1][2] = 1;
chessArr1[2][3] = 2;
chessArr1[4][2] = 1;
chessArr1[5][3] = 2;
System.out.println("原始的二维数组:");
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];
}
}
}
System.out.println();
StringBuffer stringBuffer = new StringBuffer();
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]);
stringBuffer.append(Integer.toString(sparseArr[i][0]) + " " + Integer.toString(sparseArr[i][1]) + " " + Integer.toString(sparseArr[i][2]) + "\n");
}
System.out.println();
File file = new File("D:\\code\\spring\\DataStructures\\src\\main\\java\\com\\wpc\\sparseArray\\map.data");
OutputStream outputStream = null;
try{
outputStream = new FileOutputStream(file);
outputStream.write(stringBuffer.toString().getBytes());
}catch (IOException e){
e.printStackTrace();
}finally {
try {
outputStream.close();
}catch (IOException e){
e.printStackTrace();
}
}
try {
BufferedReader reader = new BufferedReader(new FileReader("D:\\code\\spring\\DataStructures\\src\\main\\java\\com\\wpc\\sparseArray\\map.data"));
String line = null;
line = reader.readLine();
String[] strings = line.split("\\s");
int[][] sparseArr3 = new int[Integer.parseInt(strings[0])][Integer.parseInt(strings[1])];
while ((line = reader.readLine()) != null){
String[] strings2 = line.split("\\s");
sparseArr3[Integer.parseInt(strings2[0])][Integer.parseInt(strings2[1])] = Integer.parseInt(strings2[2]);
}
for (int[] row : sparseArr3){
for (int i : row){
System.out.printf("%d\t",i);
}
System.out.println();
}
System.out.println("文件读取输出");
}catch (IOException e){
e.printStackTrace();
}
int chessArr2[][] = new int[sparseArr[0][0]][sparseArr[0][1]];
for (int i = 1; i < sparseArr.length; i++){
chessArr2[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
}
for (int[] row : chessArr2){
for (int data : row){
System.out.printf("%d\t", data);
}
System.out.println();
}
}
}
结果输出
原始的二维数组:
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 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
得到的稀疏数组:
11 11 4
1 2 1
2 3 2
4 2 1
5 3 2
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 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 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 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