稀疏數(shù)組
當(dāng)一個(gè)數(shù)組中大部分元素為0诡壁,或者為同一值的數(shù)組時(shí)鬓椭,可以使用稀疏數(shù)組來保存該數(shù)組昨寞。
稀疏數(shù)組的處理方式是:記錄數(shù)組一共有幾行幾列,有多少個(gè)不同值括袒;把具有不同值的元素和行列及值記錄在一個(gè)小規(guī)模的數(shù)組中次兆,從而縮小程序的規(guī)模
,為了節(jié)約空間起到壓縮的效果锹锰,將數(shù)據(jù)用另一種結(jié)構(gòu)來表示芥炭,即稀疏數(shù)組
形式
二維數(shù)組
0 0 0
0 1 0
0 0 0
0 0 0
稀疏數(shù)組
4 3 1
1 1 1
code
public class Test {
public static void main(String[] args) throws IOException {
int [][] arr = {{0,0,0},{0,1,0},{0,0,0},{0,0,0}};
System.out.println("二維數(shù)組:");
for(int i = 0; i < arr.length; i ++) {
for(int j = 0; j < arr[i].length; j ++) {
System.out.printf("%d\t", arr[i][j]);
}
System.out.println();
}
//創(chuàng)建稀疏數(shù)組
int sum = 0;
for(int i = 0; i < arr.length; i ++) {
for(int j = 0; j < arr[i].length; j ++) {
if(arr[i][j] != 0) {
sum ++;
}
}
}
int [][] sparseArr = new int[sum + 1][3];
sparseArr[0][0] = 4;
sparseArr[0][1] = 3;
sparseArr[0][2] = 1;
int count = 0;
for(int i = 0; i < arr.length; i ++) {
for(int j = 0; j < arr[i].length; j ++) {
if(arr[i][j] != 0) {
count ++;
sparseArr[count][0] = i;
sparseArr[count][1] = j;
sparseArr[count][2] = arr[i][j];
}
}
}
System.out.println("二維數(shù)組轉(zhuǎn)稀疏數(shù)組:");
for(int i = 0; i < sparseArr.length; i ++) {
for(int j = 0; j < sparseArr[i].length; j ++) {
System.out.printf("%d\t", sparseArr[i][j]);
}
System.out.println();
}
//稀疏數(shù)組轉(zhuǎn)二維數(shù)組
int[][] newArr = new int[sparseArr[0][0]][sparseArr[0][1]];
int count2 = sparseArr[0][2];
for(int i = 1; i < sparseArr.length; i ++) {
//將稀疏數(shù)組的有數(shù)據(jù)的,記錄在二維數(shù)組
newArr[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
}
//輸出轉(zhuǎn)換后二維數(shù)組
System.out.println("稀疏數(shù)組轉(zhuǎn)二維數(shù)組:");
for(int i = 0; i < newArr.length; i ++) {
for(int j = 0; j < newArr[i].length; j ++) {
System.out.printf("%d\t", newArr[i][j]);
}
System.out.println();
}
// System.out.printf("%d\t", 1);
System.out.println("讀取文件稀疏數(shù)據(jù)");
//讀取文件稀疏數(shù)據(jù)
int[][] ints = readFile();
}
//把稀疏數(shù)組存入本地文件txt
public static void writeFile() {
}
//讀取本地文件稀疏數(shù)組
public static int[][] readFile() throws IOException {
//
//模擬文件
File file = new File("D:/sparseArr.txt");
//將file轉(zhuǎn)字符字符輸入流
FileInputStream fileInputStream = new FileInputStream(file);
//將字符輸入流轉(zhuǎn)成buffer,可以逐行讀
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
//稀疏數(shù)組
int cols = 3;
int rows = 0;
int sum = 0;
//讀取一行
String content = bufferedReader.readLine();
if(Strings.isNotBlank(content)) {
String substring = content.substring(1, 2);
String[] split = content.split(substring);
rows = Integer.valueOf(split[0]);
cols = Integer.valueOf(split[1]);
sum = Integer.valueOf(split[2]);
}
System.out.printf("rows:%d,cols:%d,sum:%d\n", rows,cols, sum);
//解析除了第一行恃慧,稀疏數(shù)組組裝
int[][] sparseArr = new int[sum + 1][cols];
sparseArr[0][0] = rows;
sparseArr[0][1] = cols;
sparseArr[0][2] = sum;
int count = 0;
BufferedReader bufferedReader1 = new BufferedReader(new InputStreamReader(fileInputStream));
String content2 = bufferedReader1.readLine();
while (Strings.isNotBlank(content2)) {
String substring = content2.substring(1, 2);
String[] split = content2.split(substring);
if(count >= 1) {//只處理第二行數(shù)據(jù)
sparseArr[count][0] = Integer.valueOf(split[0]);
sparseArr[count][1] = Integer.valueOf(split[1]);
sparseArr[count][2] = Integer.valueOf(split[2]);
}
//讀取下一行
content2 = bufferedReader.readLine();
}
//打印解析后的稀疏數(shù)組
System.out.println("打印解析后的稀疏數(shù)組:");
for(int i = 0; i < sparseArr.length; i ++) {
for(int j = 0; j < sparseArr[0].length; j ++) {
System.out.printf("%d\t", sparseArr[i][j]);
}
System.out.println();
}
// int[][] sparseArr= new int[3][];
return null;
}
}
console
二維數(shù)組:
0 0 0
0 1 0
0 0 0
0 0 0
二維數(shù)組轉(zhuǎn)稀疏數(shù)組:
4 3 1
1 1 1
稀疏數(shù)組轉(zhuǎn)二維數(shù)組:
0 0 0
0 1 0
0 0 0
0 0 0
讀取文件稀疏數(shù)據(jù)
rows:4,cols:3,sum:1
打印解析后的稀疏數(shù)組:
4 3 1
0 0 0
場(chǎng)景
棋盤記錄