Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说
二维数组数据_二维数组a[3][4]按行存储,希望能够帮助你!!!。
当一个数组中大部分元素为0,或者为同一个值得情况时,可以使用稀疏数组来保存该数组。
图一
如此图,可看成一个二维数组arr[11][11],其中绝大部分元素均为0
其中非0的元素有三个,他们储存在二维数组的中的位置分别为arr[1][4]、arr[2][1]、arr[3][4]
将其保存到稀疏数组sparseArr
如图二
其中row 代表 行 ;col 代表 列 ;vaule代表元素的值
图二中的第一行数字分别代表着 原二维数组的行数为11、列数为11、非零元素的个数为3
第二行数字分别代表着 原二维数组的第一个非零元素所在的行为1、列为4、值为1;
以此推类
由此可推出稀疏数组sparseArr[原二维数组非零元素个数+1][3]
//定义静态方法,将稀疏数组存入磁盘 public static void saveToFile(int[][] sparseArry) throws IOException { File file = new File("e:\\javaDome\\sparseArry.data"); FileWriter fileWriter = new FileWriter(file); for (int i = 0; i < sparseArry.length; i++) { fileWriter.write(sparseArry[i][0]+"\t"+sparseArry[i][1]+"\t"+sparseArry[i][2]); fileWriter.write(System.getProperty("line.separator")); //获取当前系统所需的换行符 \r、\n、\r\n } fileWriter.close(); }
//将稀疏数组从硬盘读入 public static int[][] readFile() throws IOException{ int[][] sparseArray = null; File file = new File("e:\\javaDome\\sparseArray.data"); FileReader fileReader = new FileReader(file); //创建字符读取缓冲流 BufferedReader bufferedReader = new BufferedReader(fileReader); String readLine = null ; // 用来储存读取的一行字符串 int count=0; // while ((readLine = bufferedReader.readLine()) != null){ String[] tempStr = readLine.split("\t");//readLine.split("\t");根据给定正则表达式的匹配拆分此字符串。 //判断数组是否创建,未创建则创建 if(sparseArray == null){ sparseArray = new int[Integer.parseInt(tempStr[2])+1][3]; } sparseArray[count][0] = Integer.parseInt(tempStr[0]); sparseArray[count][1] = Integer.parseInt(tempStr[1]); sparseArray[count][2] = Integer.parseInt(tempStr[2]); count++; } fileReader.close(); return sparseArray; } return sparseArry; }
//定义普通二维数组用来表示棋盘 //0:表示没有棋子 1:表示白子 2:表示黑子 int[][] chessArr1 = new int[11][11]; chessArr1[2][1] = 1; chessArr1[3][4] = 2; chessArr1[1][4] = 1; //遍历数组,得到非零个数 int sum = 0; //用来储存非零元素个数 for (int i = 0; i < 11; i++) { for (int j = 0; j < 11; j++) { if(chessArr1[i][j] != 0) { sum++; } } }
//将原二维数组数据储存到稀疏数组中 int count = 0 ; //用来记录第几个有效元素 for (int i = 0; i < 11; i++) { for (int j = 0; j < 11; j++) { if(chessArr1[i][j] != 0) { count++; sparseArr1[count][0] = i; sparseArr1[count][1] = j; sparseArr1[count][2] = chessArr1[i][j]; } } } //打印数组sparsearr1 traverseArray(sparseArr1); //稀疏数组储存到磁盘 saveToFile(sparseArr1);
//读取磁盘中的稀疏数组 int[][] sparseArr2 = readFile(); //定义二维数组储存稀疏数组转原二维数组 int[][] chessArr2 = new int[sparseArr2[0][0]][sparseArr2[0][1]]; //稀疏数组还原二维数组 for (int i = 1; i <= sparseArr2[0][2]; i++) { chessArr2[sparseArr2[i][0]][sparseArr2[i][1]] = sparseArr2[i][2]; }
import java.io.*; /** * 稀疏数组 */ public class sparsearray { //定义静态方法,将稀疏数组存入磁盘 public static void saveToFile(int[][] sparseArray) throws IOException { File file = new File("e:\\javaDome\\sparseArray.data"); FileWriter fileWriter = new FileWriter(file); for (int i = 0; i < sparseArray.length; i++) { fileWriter.write(sparseArray[i][0]+"\t"+sparseArray[i][1]+"\t"+sparseArray[i][2]); fileWriter.write(System.getProperty("line.separator")); //获取当前系统所需的换行符 \r、\n、\r\n } fileWriter.close(); } //将稀疏数组从硬盘读入 public static int[][] readFile() throws IOException{ int[][] sparseArray = null; File file = new File("e:\\javaDome\\sparseArray.data"); FileReader fileReader = new FileReader(file); //创建字符读取缓冲流 BufferedReader bufferedReader = new BufferedReader(fileReader); String readLine = null ; // 用来储存读取的一行字符串 int count=0; // while ((readLine = bufferedReader.readLine()) != null){ String[] tempStr = readLine.split("\t");//readLine.split("\t");根据给定正则表达式的匹配拆分此字符串。 //判断数组是否创建,未创建则创建 if(sparseArray == null){ sparseArray = new int[Integer.parseInt(tempStr[2])+1][3]; } sparseArray[count][0] = Integer.parseInt(tempStr[0]); sparseArray[count][1] = Integer.parseInt(tempStr[1]); sparseArray[count][2] = Integer.parseInt(tempStr[2]); count++; } fileReader.close(); return sparseArray; } //遍历数组的方法 static public void traverseArray(int[][] Arry){ //遍历数组查看数据 for (int[] row : Arry) { for (int data : row) { System.out.printf("%d\t",data); } System.out.println(); } System.out.println(); } public static void main(String[] args) throws IOException { //定义普通二维数组用来表示棋盘 //0:表示没有棋子 1:表示白子 2:表示黑子 int[][] chessArr1 = new int[11][11]; chessArr1[2][1] = 1; chessArr1[3][4] = 2; chessArr1[1][4] = 1; //遍历数组查看数据 traverseArray(chessArr1); //遍历数组,得到非零个数 int sum = 0; for (int i = 0; i < 11; i++) { for (int j = 0; j < 11; j++) { if(chessArr1[i][j] != 0) { sum++; } } } //定义一个二位数组作稀疏数组 int[][] sparseArr1 = new int[sum+1][3]; sparseArr1[0][0] = 11; //储存原二维数组 行数 sparseArr1[0][1] = 11; //储存原二维数组 列数 sparseArr1[0][2] = sum; //储存原二维数组有效元素个数 //将原二维数组数据储存到稀疏数组中 int count = 0 ; //用来记录第几个有效元素 for (int i = 0; i < 11; i++) { for (int j = 0; j < 11; j++) { if(chessArr1[i][j] != 0) { count++; sparseArr1[count][0] = i; sparseArr1[count][1] = j; sparseArr1[count][2] = chessArr1[i][j]; } } } //打印数组sparsearr1 traverseArray(sparseArr1); //稀疏数组储存到磁盘 saveToFile(sparseArr1); //读取磁盘中的稀疏数组 int[][] sparseArr2 = readFile(); //定义二维数组储存稀疏数组转原二维数组 int[][] chessArr2 = new int[sparseArr2[0][0]][sparseArr2[0][1]]; //稀疏数组还原二维数组 for (int i = 1; i <= sparseArr2[0][2]; i++) { chessArr2[sparseArr2[i][0]][sparseArr2[i][1]] = sparseArr2[i][2]; } //打印数组chessarr2 traverseArray(chessArr2); } }
今天的分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
上一篇
已是最后文章
下一篇
已是最新文章