?
import java.io.*;
public class Test01 {
public static void main(String[] args) throws IOException {
int chessArray [][] = new int [11][11];
chessArray [1][2] = 1;
chessArray [2][3] = 2;
chessArray [4][5] = 2;
int sum = 0;
//遍历输出原数组数组
for(int a []:chessArray){
for(int b :a){
System.out.printf("%d\t",b);
}
System.out.println();
}
//遍历原数组确定哪个有几个非零值
for (int i = 0;i<11;i++){
for (int j = 0;j<11;j++){
if(chessArray[i][j]!=0)
sum++;
}
}
//创建一个稀疏数组
int count = 0;
int spacesArray [][] = new int [sum+1][3];
spacesArray [0][0] = chessArray.length;
spacesArray [0][1] = chessArray[0].length;
spacesArray [0][2] = sum;
//便利稀疏数组赋值
for (int i = 1;i<chessArray.length;i++){
for (int j = 0;j<chessArray[0].length;j++){
if(chessArray[i][j]!=0){
count++;
spacesArray [count][0] = i;
spacesArray [count][1] = j;
spacesArray [count][2] = chessArray [i][j];
}
}
}
//遍历输出稀疏数组
for (int i = 0;i<spacesArray.length;i++){
System.out.printf("%d\t %d\t %d\t\n",
spacesArray[i][0],spacesArray[i][1],spacesArray[i][2]);
}
//还原稀疏数组为原数组
int chessArray1 [][] = new int [spacesArray[0][0]][spacesArray[0][1]];
for(int i = 1;i<spacesArray[0][2]+1;i++){
chessArray1[spacesArray[i][0]][spacesArray[i][1]]=spacesArray[i][2];
}
//遍历输出还原后的数组
System.out.println("---------------------------------");
for(int a []:chessArray1){
for (int b:a){
System.out.printf("%d\t",b);
}
System.out.println();
}
System.out.println("=------================================");
byte [][] b = new byte [sum+1][3];
for (int i = 0;i<spacesArray.length;i++){
b[i][0] = (byte)spacesArray[i][0];
b[i][1] = (byte)spacesArray[i][1];
b[i][2] = (byte)spacesArray[i][2];
}
????????//写入文件时的输出流
FileOutputStream fOps = new FileOutputStream(new File("haha"));
//读取文件时的输入流
????????FileInputStream fIps = new FileInputStream(new File("haha"));
for(byte [] a:b) {
ByteArrayInputStream ips = new ByteArrayInputStream(a);
for(byte a1:a){
int read = ips.read();
fOps.write(read);
}
}
????????//创建一个用于接收的数组
byte [] n = new byte [fIps.available()];
????????//读取数组
fIps.read(n);
byte [][] spaceArray1 = new byte [sum+1][3];
//将得到的一维数组转为二维稀疏数组
int index = 0;
for(int j = 0;j<n.length;j++){
spaceArray1[index][0] = n[j];
spaceArray1[index][1] = n[++j];
spaceArray1[index][2] = n[++j];
index++;
}
for(byte [] a :spaceArray1){
for(int v :a){
System.out.printf("%d\t",v);
}
System.out.println();
}
fIps.close();
fOps.close();
fOps.flush();
}
}