目录
代码实现二维数组和稀疏数组的相互转换以及将二维数组写入文件并读取复原二维数组
什么是稀疏数组?
-
第一行
-
第一列记录原二维数组的行数
-
第二列记录原二维数组的列数
-
第三列记录原二维数组不同数据的个数
-
-
其他行
-
第一列记录不同数据在原数组所在的行
-
第二列记录不同数据在原数组所在的列
-
第三列记录不同数据的值
-
-
原二维数组有n个不同的数据,稀疏数组就有n+1行
什么时候用稀疏数组
-
当二维数组中相同的数据很多,有少数有特殊意义的数据与这些数据不同时(例如二维数组表示棋盘或地图时)
二维数组转稀疏数组的思路
-
遍历原始的二维数组,得到有效数据的个数
-
根据有效数据的个数就可以创建稀疏数组
-
再次遍历二维数组将有效数据的坐标和值存入稀疏数组中
稀疏数组转二维数组的思路
-
先读取稀疏数组的第一行创建二维数组
-
再读取后几行数据并存入二维数组之中
代码实现二维数组和稀疏数组的相互转换以及将二维数组写入文件并读取复原二维数组
模拟棋盘,二维数组转稀疏数组
// 创建一个原始的二维数组11*11
//模拟棋盘0:空位1:黑子2:白子
int chessArr1[][] = new int[11][11];
chessArr1[1][2] = 1;
chessArr1[2][3] = 2;
//输出原始的二维数组
System.out.println("------原始的二维数组----");
for(int[] row: chessArr1) {
for(int data: row) {
System.out.printf("%d\t",data);
}
System.out.println();
}
//将二维数组转稀疏数组
//1.遍历原始的二维数组,得到有效数据的个数
int n=0;
for(int i=0;i<chessArr1.length;i++) {
for(int j=0;j<chessArr1[i].length;j++) {
if(chessArr1[i][j]!=0) {
n++;
}
}
}
//2.根据有效数据的个数就可以创建稀疏数组
int[][] sparesArr = new int[n+1][3];
//3.再次遍历二维数组将有效数据的坐标和值存入稀疏数组中
//存第一列
sparesArr[0][0]=chessArr1.length;
sparesArr[0][1]=chessArr1[0].length;
sparesArr[0][2]=n;
//存其他列
int count=0;
for(int i=0;i<chessArr1.length;i++) {
for(int j=0;j<chessArr1[i].length;j++) {
if(chessArr1[i][j]!=0) {
count++;
sparesArr[count][0]=i;
sparesArr[count][1]=j;
sparesArr[count][2]=chessArr1[i][j];
}
}
}
//输出稀疏数组
System.out.println("--------稀疏数组--------------");
for(int i=0;i<sparesArr.length;i++) {
System.out.printf("%d\t%d\t%d\t\n",sparesArr[i][0],sparesArr[i][1],sparesArr[i][2]);
}
将稀疏数组存入本地文件中
//将稀疏数组存在文件中
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("d:/稀疏.txt")));
for(int i=0;i<sparesArr.length;i++) {
bw.write(sparesArr[i][0]+" "+sparesArr[i][1]+" "+sparesArr[i][2]);
bw.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
bw.flush();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
从读文件并还原成稀疏数组
//从文件中读取稀疏数组
BufferedReader br = null;
int[][] sparesArr2=null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream("d:/稀疏.txt")));
String str="";
List<String[]> list = new ArrayList<>();
while((str=br.readLine())!=null) {
list.add(str.split(" "));
}
sparesArr2=new int[list.size()][3];
String[] strs = new String[1];
for(int i=0;i<list.size();i++) {
strs = list.get(i);
sparesArr2[i][0]=Integer.parseInt(strs[0]);
sparesArr2[i][1]=Integer.parseInt(strs[1]);
sparesArr2[i][2]=Integer.parseInt(strs[2]);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//输出得到的稀疏数组
System.out.println("----文件中读到的稀疏数组----");
for(int i=0;i<sparesArr2.length;i++) {
System.out.printf("%d\t%d\t%d\t\n",sparesArr2[i][0],sparesArr2[i][1],sparesArr2[i][2]);
}
将稀疏数组还原成二维数组
//稀疏数组转二维数组
//1.先读取稀疏数组的第一行创建二维数组
int[][] chessArr2 = new int[sparesArr2[0][0]][sparesArr2[0][1]];
//2.再读取后几行数据并存入二维数组之中
for(int i=1;i<sparesArr2.length;i++) {
chessArr2[sparesArr2[i][0]][sparesArr2[i][1]]=sparesArr2[i][2];
}
//输出转为的二维数组
System.out.println("----转出的二维数组----");
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 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
--------稀疏数组--------------
11 11 2
1 2 1
2 3 2
----文件中读到的稀疏数组----
11 11 2
1 2 1
2 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 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