写的比较通俗易懂,希望能帮助到有需要的人
import java
.io
.*
;
public class SparseArray {
public static void main(String
[] args
) {
int chess
[][] = new int[11][11];
chess
[1][2] = 1;
chess
[2][3] = 2;
System
.out
.println("初始的二维数组");
for (int[] ints
: chess
) {
for (int i
: ints
) {
System
.out
.print(i
+ "\t");
}
System
.out
.println();
}
int count
= 0;
for (int[] ints
: chess
) {
for (int i
: ints
) {
if (i
!= 0) {
count
++;
}
}
}
int sparse
[][] = new int[count
+ 1][3];
sparse
[0][0] = chess
.length
;
sparse
[0][1] = 11;
sparse
[0][2] = count
;
int flag
= 0;
for (int i
= 0; i
< chess
.length
; i
++) {
for (int j
= 0; j
< chess
[i
].length
; j
++) {
if (chess
[i
][j
] != 0) {
flag
++;
sparse
[flag
][0] = i
;
sparse
[flag
][1] = j
;
sparse
[flag
][2] = chess
[i
][j
];
}
}
}
System
.out
.println("初始的稀疏数组");
for (int i
= 0; i
< sparse
.length
; i
++) {
for (int j
= 0; j
< sparse
[i
].length
; j
++) {
System
.out
.print(sparse
[i
][j
] + "\t");
}
System
.out
.println();
}
FileWriter writer
= null
;
try {
writer
= new FileWriter(new File("sparse.txt"));
for (int[] ints
: sparse
) {
for (int i
: ints
) {
writer
.write(i
);
}
}
} catch (IOException e
) {
e
.printStackTrace();
} finally {
try {
if (writer
!= null
) {
writer
.close();
}
} catch (IOException e
) {
e
.printStackTrace();
}
}
FileReader reader
= null
;
StringBuffer buffer
= new StringBuffer();
try {
reader
= new FileReader(new File("sparse.txt"));
char[] cbuf
= new char[1024];
int len
;
while ((len
= reader
.read(cbuf
)) != -1) {
buffer
.append(cbuf
,0,len
);
}
} catch (IOException e
) {
e
.printStackTrace();
} finally {
try {
if (reader
!= null
) {
reader
.close();
}
} catch (IOException e
) {
e
.printStackTrace();
}
}
String string
= new String(buffer
);
int chess3
[][] = new int[string
.length()/ 3][3];
int k
=0;
for (int i
= 0; i
< chess3
.length
; i
++) {
for (int j
= 0; j
< chess3
[i
].length
; j
++) {
chess3
[i
][j
]= string
.charAt(k
);
k
++;
}
}
System
.out
.println("磁盘复原的稀疏数组");
for (int[] ints
: chess3
) {
for (int i
: ints
) {
System
.out
.print(i
+"\t");
}
System
.out
.println();
}
int chess2
[][] = new int[chess3
[0][0]][chess3
[0][1]];
for (int i
= 1; i
< chess3
.length
; i
++) {
chess2
[chess3
[i
][0]][sparse
[i
][1]] = chess3
[i
][2];
}
System
.out
.println("复原的二维数组");
for (int[] ints
: chess2
) {
for (int i
: ints
) {
System
.out
.print(i
+ "\t");
}
System
.out
.println();
}
}
}
转载请注明原文地址:https://tech.qufami.com/read-25033.html