1 package com.ljh.sparseArray; 2 3 public class SparseArray { 4 5 public static void main(String[] args) { 6 //创建原始的二维数组 11*11 7 //0表示没有棋子,1表示黑子,2表示蓝子 8 int chessArr1[][] = new int[11][11]; 9 chessArr1[1][2] = 1; 10 chessArr1[2][3] = 2; 11 chessArr1[6][1] = 1; 12 //输出原始的二维数组 13 System.out.println("原始的二维数组:"); 14 for(int[] row : chessArr1) { 15 for(int data : row) 16 System.out.printf("%d\t",data); 17 System.out.println(); 18 } 19 //将二维数组转稀疏数组的思想 20 //1.先遍历二维数组,得到非0数据的个数 21 int sum = 0; 22 for (int i = 0; i < 11; i++) { 23 for(int j = 0; j < 11; j++) { 24 if(chessArr1[i][j]!=0) { 25 sum++; 26 } 27 } 28 } 29 //2.创建对应的稀疏数组 30 int sparseArr[][] = new int[sum+1][3]; 31 //给稀疏数组赋值 32 sparseArr[0][0] = 11;//记录行数 33 sparseArr[0][1] = 11;//记录列数 34 sparseArr[0][2] = sum;//记录非零元素的个数 35 36 //遍历二维数组,将非0字符的值存放到稀疏数组中 37 int count = 0;//用于记录是第几个非0数据 38 for (int i = 0; i < 11; i++) { 39 for(int j = 0; j < 11; j++) { 40 if(chessArr1[i][j]!=0) { 41 count++; 42 sparseArr[count][0] = i; 43 sparseArr[count][1] = j; 44 sparseArr[count][2] = chessArr1[i][j]; 45 } 46 } 47 } 48 //输出稀疏数组的形式 49 System.out.println(); 50 System.out.println("得到的稀疏数组为~"); 51 for (int i = 0; i < sparseArr.length; i++) { 52 System.out.printf("%d\t%d\t%d\t\n",sparseArr[i][0],sparseArr[i][1],sparseArr[i][2]); 53 } 54 System.out.println(); 55 //将稀疏数组恢复成原始的二维数组 56 //1.先读取稀疏数组的第一行,根据第一行的数据,创建原始的二维数组 57 int chessArr2[][] = new int[sparseArr[0][0]][sparseArr[0][1]]; 58 //2.在读取稀疏数组后几行的数据(从第二行开始),并赋给原始的二维数组即可 59 for(int i = 1; i<sparseArr.length; i++) { 60 chessArr2[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2]; 61 } 62 //输出恢复后的二维数组 63 System.out.println("输出恢复后的二维数组:"); 64 for(int[] row : chessArr2) { 65 for(int data : row) 66 System.out.printf("%d\t",data); 67 System.out.println(); 68 } 69 } 70 71 }
结果展示:
1.3课后练习
1 package com.ljh.sparseArray; 2 3 import java.io.File; 4 import java.io.FileNotFoundException; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 8 public class StoreData { 9 10 public static void main(String[] args) throws IOException { 11 // TODO Auto-generated method stub 12 //创建原始的二维数组 11*11 13 //0表示没有棋子,1表示黑子,2表示蓝子 14 int chessArr1[][] = new int[11][11]; 15 chessArr1[1][2] = 1; 16 chessArr1[2][3] = 2; 17 chessArr1[6][1] = 1; 18 chessArr1[7][9] = 2; 19 chessArr1[2][8] = 2; 20 //输出原始的二维数组 21 System.out.println("原始的二维数组:"); 22 for(int[] row : chessArr1) { 23 for(int data : row) 24 System.out.printf("%d\t",data); 25 System.out.println(); 26 } 27 //将二维数组转稀疏数组的思想 28 //1.先遍历二维数组,得到非0数据的个数 29 int sum = 0; 30 for (int i = 0; i < 11; i++) { 31 for(int j = 0; j < 11; j++) { 32 if(chessArr1[i][j]!=0) { 33 sum++; 34 } 35 } 36 } 37 //2.创建对应的稀疏数组 38 int sparseArr[][] = new int[sum+1][3]; 39 //给稀疏数组赋值 40 sparseArr[0][0] = 11;//记录行数 41 sparseArr[0][1] = 11;//记录列数 42 sparseArr[0][2] = sum;//记录非零元素的个数 43 44 //遍历二维数组,将非0字符的值存放到稀疏数组中 45 int count = 0;//用于记录是第几个非0数据 46 for (int i = 0; i < 11; i++) { 47 for(int j = 0; j < 11; j++) { 48 if(chessArr1[i][j]!=0) { 49 count++; 50 sparseArr[count][0] = i; 51 sparseArr[count][1] = j; 52 sparseArr[count][2] = chessArr1[i][j]; 53 } 54 } 55 } 56 //输出稀疏数组的形式 57 System.out.println(); 58 File file = new File("./map.data"); 59 FileOutputStream fos = new FileOutputStream(file); 60 System.out.println("得到的稀疏数组为~"); 61 for (int i = 0; i < sparseArr.length; i++) { 62 System.out.printf("%d\t%d\t%d\t\n",sparseArr[i][0],sparseArr[i][1],sparseArr[i][2]); 63 String str = ""+sparseArr[i][0]+" "+sparseArr[i][1]+" "+sparseArr[i][2]; 64 fos.write(str.getBytes()); 65 if(i == sparseArr.length-1) break; 66 fos.write(‘\n‘); 67 } 68 fos.close(); 69 } 70 71 }
1 package com.ljh.sparseArray; 2 import java.io.BufferedReader; 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.FileReader; 7 public class ReadData { 8 public static void main(String[] args) throws Exception { 9 //使用BufferedReader读取文件,传递字符输入流 10 BufferedReader br = new BufferedReader(new FileReader("./map.data")); 11 //按行读取 12 String firstLine = br.readLine(); 13 //通过split函数,利用空格分隔,得到每一项 14 int rows = Integer.parseInt(firstLine.split(" ")[0]); 15 int cols = Integer.parseInt(firstLine.split(" ")[1]); 16 int num = Integer.parseInt(firstLine.split(" ")[2]); 17 int chessArr2[][] = new int[rows][cols]; 18 String line; 19 while((line = br.readLine())!=null) { 20 System.out.println(line); 21 rows = Integer.parseInt(line.split(" ")[0]); 22 cols = Integer.parseInt(line.split(" ")[1]); 23 num = Integer.parseInt(line.split(" ")[2]); 24 chessArr2[rows][cols] = num; 25 } 26 //输出恢复后的二维数组 27 System.out.println("输出恢复后的二维数组:"); 28 for(int[] row : chessArr2) { 29 for(int data : row) 30 System.out.printf("%d\t",data); 31 System.out.println(); 32 } 33 br.close(); 34 } 35 }
原文:https://www.cnblogs.com/liujinhui/p/14311250.html