二维数组 和 稀疏数组的相互转换 及 数据存入文件中
二维数组 和 稀疏数组的相互转换 及 数据存入文件中
import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; /* 稀疏数组 和 二维数组 */ public class SparseArray { public static void main(String[] args) throws IOException { //原始的二维数组的大小 int[][] array = new int[11][11]; array[1][3] = 1; array[2][4] = 2; array[2][5] = 2; array[2][7] = 2; System.out.println("原始的二维数据"); for (int[] row : array) { for (int data : row) { System.out.printf("%d\t", data); } System.out.println(); } //获取sum的个数 int sum = 0; for (int i = 0; i < 11; i++) { for (int j = 0; j < 11; j++) { if (array[i][j] != 0) { sum++; } } } //创建稀疏数组 int[][] parse = new int[sum + 1][3]; parse[0][0] = 11; parse[0][1] = 11; parse[0][2] = sum; //稀疏数组赋值 int count = 0; for (int i = 0; i < 11; i++) { for (int j = 0; j < 11; j++) { if (array[i][j] != 0) { count++; parse[count][0] = i; parse[count][1] = j; parse[count][2] = array[i][j]; } } } //打印输出稀疏数组 for (int i = 0; i < parse.length; i++) { System.out.printf("%d\t%d\t%d\t\n", parse[i][0], parse[i][1], parse[i][2]); } System.out.println(); //1.创建源 File dest = new File("SpasreArray.data"); //2.选择流:之所以选择字符流进行操作,主要由于其比字节流分割字符相比有分割方便的方法 Writer os = null; os = new FileWriter(dest); //3.进行数据的拷贝,其中\t,即table符号,作为数字的分隔符(主要是数字的位数在此例中是不确定的) for (int i = 0; i < parse.length; i++) { for (int j = 0; j < 3; j++) { os.write(parse[i][j] + "\t"); } os.write("\r\n"); } //4.释放资源 os.close();
System.out.println("稀疏数组恢复二维数组");
int [][]array1 = new int[parse[0][0]][parse[0][1]];
for (int i = 1; i < parse.length; i++) {
array1[parse[i][0]][parse[i][1]] = parse[i][2];
}
for (int[] row : array1) {
for (int data : row) {
System.out.printf("%d\t",data);
}
System.out.println();
}
} }
控制台打印
"C:\Program Files\Java\jdk1.8.0_91\bin\java.exe" "-javaagent:D:\IntelliJ IDEA 2018.2.4\lib\idea_rt.jar=56550:D:\IntelliJ IDEA 2018.2.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_91\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\rt.jar;E:\idearproject\Test1\out\production\Test1" SparseArray 原始的二维数据 0 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 2 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 打印二维数组转换为稀疏数组 11 11 4 1 3 1 2 4 2 2 5 2 2 7 2 稀疏数组恢复二维数组 0 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 2 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 Process finished with exit code 0
本地文件:
11 11 4 1 3 1 2 4 2 2 5 2 2 7 2
读取本地文件的数组到稀疏数组中:
/** * 二、将本地文件map.data转换为稀疏数组 */ //1.创建源 File src = new File("SpasreArray.data"); //2.选择流 BufferedReader in = new BufferedReader(new FileReader(src)); //3.1进行数据的搬移,但是数组首要考虑的事情是数组要多大? int row =0;//用于创建要创建的二维稀疏数组的大小确定 String line; //一行数据 //逐行读取,并将每个数组放入到数组中 while ((line = in.readLine()) != null) { row++; } int sparseArr2[][] = new int [row][3]; //3.2文本数据转移到稀疏数组中 int rowtmp = 0; //由于读取完毕整个文本文档,所以不妨”重启“流 in.close(); in = new BufferedReader(new FileReader(src)); while ((line = in.readLine()) != null) { String[] temp = line.split("\t"); for (int j = 0; j < temp.length; j++) { sparseArr2[rowtmp][j]=Integer.parseInt(temp[j]); } rowtmp++; } //4.关闭流 in.close(); //验证文件读取是否正确 System.out.println("文件读取数据"); for(int[]temp1:sparseArr2) { for (int temp2 : temp1) { System.out.printf("%d\t", temp2); } System.out.println(); }
完整代码:
import java.io.*; /* 稀疏数组 和 二维数组 */ public class SparseArray { public static void main(String[] args) throws IOException { //原始的二维数组的大小 int[][] array = new int[11][11]; array[1][3] = 1; array[2][4] = 2; array[2][5] = 2; array[2][7] = 2; System.out.println("原始的二维数据"); for (int[] row : array) { for (int data : row) { System.out.printf("%d\t", data); } System.out.println(); } //获取sum的个数 int sum = 0; for (int i = 0; i < 11; i++) { for (int j = 0; j < 11; j++) { if (array[i][j] != 0) { sum++; } } } //创建稀疏数组 int[][] parse = new int[sum + 1][3]; parse[0][0] = 11; parse[0][1] = 11; parse[0][2] = sum; //稀疏数组赋值 int count = 0; for (int i = 0; i < 11; i++) { for (int j = 0; j < 11; j++) { if (array[i][j] != 0) { count++; parse[count][0] = i; parse[count][1] = j; parse[count][2] = array[i][j]; } } } //打印输出稀疏数组 System.out.println("打印二维数组转换为稀疏数组"); for (int i = 0; i < parse.length; i++) { System.out.printf("%d\t%d\t%d\t\n", parse[i][0], parse[i][1], parse[i][2]); } System.out.println(); //1.创建源 File dest = new File("SpasreArray.data"); //2.选择流:之所以选择字符流进行操作,主要由于其比字节流分割字符相比有分割方便的方法 Writer os = null; os = new FileWriter(dest); //3.进行数据的拷贝,其中\t,即table符号,作为数字的分隔符(主要是数字的位数在此例中是不确定的) for (int i = 0; i < parse.length; i++) { for (int j = 0; j < 3; j++) { os.write(parse[i][j] + "\t"); } os.write("\r\n"); } //4.释放资源 os.close(); //打印稀疏数组 //System.out.println("稀疏数组的二维数据"); // for (int[] row : parse) { // for (int data : row) { // System.out.printf("%d\t",data); // } // System.out.println(); // } // for (int i = 0; i < parse.length; i++) { // System.out.printf("%d\t%d\t%d\t\n",parse[i][0],parse[i][1],parse[i][2]); // } // System.out.println(); // System.out.println("稀疏数组恢复二维数组"); int [][]array1 = new int[parse[0][0]][parse[0][1]]; for (int i = 1; i < parse.length; i++) { array1[parse[i][0]][parse[i][1]] = parse[i][2]; } for (int[] row : array1) { for (int data : row) { System.out.printf("%d\t",data); } System.out.println(); } /** * 二、将本地文件map.data转换为稀疏数组 */ //1.创建源 File src = new File("SpasreArray.data"); //2.选择流 BufferedReader in = new BufferedReader(new FileReader(src)); //3.1进行数据的搬移,但是数组首要考虑的事情是数组要多大? int row =0;//用于创建要创建的二维稀疏数组的大小确定 String line; //一行数据 //逐行读取,并将每个数组放入到数组中 while ((line = in.readLine()) != null) { row++; } int sparseArr2[][] = new int [row][3]; //3.2文本数据转移到稀疏数组中 int rowtmp = 0; //由于读取完毕整个文本文档,所以不妨”重启“流 in.close(); in = new BufferedReader(new FileReader(src)); while ((line = in.readLine()) != null) { String[] temp = line.split("\t"); for (int j = 0; j < temp.length; j++) { sparseArr2[rowtmp][j]=Integer.parseInt(temp[j]); } rowtmp++; } //4.关闭流 in.close(); //验证文件读取是否正确 System.out.println("文件读取数据"); for(int[]temp1:sparseArr2) { for (int temp2 : temp1) { System.out.printf("%d\t", temp2); } System.out.println(); } } }
致敬:2020年的自己
--------------------------------------------
即使不为了什么远大理想,为了好好生活,你也得努力奋斗啊,不然别说什么风花雪月了,柴米油盐也能让你一筹莫展。