package com.chw.study.day1209_spareArray;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class SpareArray {
public static void main(String[] args) throws Exception {
//构造一个二维数组 1表示黑子 2表示白子 ,模拟象棋的存档
int data[][] = new int[11][11];
data[2][3]=1;
data[2][4]=2;
data[3][4]=1;
data[3][5]=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 1 2 0 0 0 0 0 0
0 0 0 0 1 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 */
for(int[] row:data){
for(int c:row){
System.out.print(c+" ");
}
System.out.println();
}
//算出非0的个数
int sum=0;
for(int[] row:data){
for(int c:row){
if(c!=0){
sum++;
}
}
}
System.out.println("非零个数:"+sum);
int spareArray[][] = new int[sum+1][3];
spareArray[0][0]=11;
spareArray[0][1]=11;
spareArray[0][2]=sum;
int count=0;
for(int i=0;i<11;i++){
for(int j=0;j<11;j++){
if(data[i][j]!=0){
count++;
spareArray[count][0]=i;
spareArray[count][1]=j;
spareArray[count][2]=data[i][j];
}
}
}
/*
11 11 4
2 3 1
2 4 2
3 4 1
3 5 2 */
for(int[] row:spareArray){
for(int c:row){
System.out.print(c+" ");
}
System.out.println();
}
//将数据保存到磁盘中, D:\data.map
save(spareArray);
//读稀疏数组 D:\data.map
int [][] _spareArray = readData("D:\\data.map");
//稀疏数组 -> 二维数组
int [][] _data = new int[_spareArray[0][0]][_spareArray[0][1]];
for(int i=1;i<_spareArray.length;i++){
_data[_spareArray[i][0]][_spareArray[i][1]]=_spareArray[i][2];
}
for(int[] c:_data){
for(int d:c){
System.out.print(" "+d);
}
System.out.println();
}
}
private static int[][] readData(String path) throws Exception {
File file = new File(path);
FileInputStream fis = new FileInputStream(file);
InputStreamReader reader = new InputStreamReader(fis);
BufferedReader buff = new BufferedReader(reader);
String line;
int count=-1;
int spareArray[][] = null;
while((line=buff.readLine())!=null){
System.out.println("------:"+line);
String date[]=line.split(";");
count++;
if(count==0){
spareArray = new int[Integer.valueOf(date[2])+1][3];
}
spareArray[count][0]=Integer.valueOf(date[0]).intValue();
spareArray[count][1]=Integer.valueOf(date[1]).intValue();
spareArray[count][2]=Integer.valueOf(date[2]).intValue();
}
for(int i=0;i<spareArray.length;i++){
System.out.print(spareArray[i][0]+";"+spareArray[i][1]+";"+spareArray[i][2]);
System.out.println();
}
return spareArray;
}
private static void save(int[][] spareArray) throws Exception {
File file = new File("D:\\data.map");
FileOutputStream fos = new FileOutputStream(file);
OutputStreamWriter writer = new OutputStreamWriter(fos);
for(int i=0;i<spareArray.length;i++){
System.out.print(spareArray[i][0]+";"+spareArray[i][1]+";"+spareArray[i][2]);
writer.append(spareArray[i][0]+";"+spareArray[i][1]+";"+spareArray[i][2]+"\n");
}
writer.close();
fos.close();
}
}
【数据结构和算法】二维数组与稀疏数组的转换
最新推荐文章于 2024-01-08 11:05:10 发布