当一个数组中大部分元素为0,或者为同一个值的数组时,可以用稀疏数组来保存该数组。
public class SparseArray {public static void main(String[] args) {//创建一个原始的二维数组 11*11//0 表示没有棋子 1 表示黑子 2表示蓝子int chessArr[][] = new int[11][11];chessArr[1][2] = 1;chessArr[2][3] = 2;System.out.println("====原始的二维数组====");printArray(chessArr);//将二维数组 转 稀疏数组//1、先遍历二维数组,得到非零数据的个数int sum = 0;for (int i = 0; i < chessArr.length; i++) {for (int j = 0; j < chessArr.length; j++) {if (chessArr[i][j] != 0) {sum++;}}}System.out.println("sum = " + sum);//2、创建对应的稀疏数组 (3列是固定的)int sparseArray[][] = new int[sum + 1][3];//给稀疏数组赋值sparseArray[0][0] = 11;sparseArray[0][1] = 11;sparseArray[0][2] = sum;//3、遍历数组,将非0的值放在sparseArray中int count = 0;//用于记录是第几个非0数据for (int i = 0; i < chessArr.length; i++) {for (int j = 0; j < chessArr.length; j++) {if (chessArr[i][j] != 0) {count++;sparseArray[count][0] = i;sparseArray[count][1] = j;sparseArray[count][2] = chessArr[i][j];}}}System.out.println("====得到的稀疏数组====");for (int i = 0; i < sparseArray.length; i++) {System.out.printf("%d\t%d\t%d\t\n", sparseArray[i][0],sparseArray[i][1],sparseArray[i][2]);}System.out.println();//将稀疏数组 转 原始二维数组// 1.先读取稀疏数组的第一行,根据第一行的数据,创建原始的二维数组int[][] chessArr2 = new int[sparseArray[0][0]][sparseArray[0][1]];// 2.读取稀疏数组的后几行数据(从第二行开始),并复制给原始的二维数组即可for (int i = 1; i < sparseArray.length; i++) {chessArr2[sparseArray[i][0]][sparseArray[i][1]] = sparseArray[i][2];}// 3.输出恢复后的二维数组System.out.println("====恢复后的二维数组====");printArray(chessArr2);}public static void printArray(int[][] array) {for (int[] row : array) {for (int data : row) {System.out.printf("%d\t",data);}System.out.println();}}
}
输出结果如下:
原始的二维数组
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 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 0 0 0 0
sum = 2
得到的稀疏数组
11 11 2
1 2 1
2 3 2
恢复后的二维数组
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 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 0 0 0 0
/*** 存储稀疏数组,相邻数据使用\t划分* @param path 文件的存放路径* @param sparseArr 稀疏数组对象*/public static void save(String path,int[][] sparseArr) {FileWriter fileWriter = null;try {fileWriter = new FileWriter(path);for (int[] row : sparseArr) {fileWriter.write(row[0]+"\t" + row[1] + "\t" + row[2]);fileWriter.write("\r\n");}} catch (IOException e) {e.printStackTrace();}finally {try {fileWriter.close();} catch (IOException e) {e.printStackTrace();}}}/*** 读取二维稀疏数组,相邻数据使用\t划分* @param path 文件的存放路径* @return 二维的稀疏数组*/public static void read(String path) {int[][] sparseArr = null;BufferedReader bufferedReader = null;try {bufferedReader = new BufferedReader(new FileReader(path));String lineStr = null;int lineCount = 0;while ((lineStr = bufferedReader.readLine()) != null ) {String[] tempStr = lineStr.split("\t");if (lineCount == 0) {// 稀疏数组的[0,2]位置记录了非0数据个数,所以稀疏数组大小为[Integer.parseInt(tempStr[2]) + 1][3]sparseArr = new int[Integer.parseInt(tempStr[2] ) +1 ][3];}sparseArr[lineCount][0] =Integer.parseInt(tempStr[0]); sparseArr[lineCount][1] =Integer.parseInt(tempStr[1]); sparseArr[lineCount][2] =Integer.parseInt(tempStr[2]); lineCount++;}} catch (Exception e) {e.printStackTrace();}finally {try {bufferedReader.close();} catch (IOException e) {e.printStackTrace();}}}
注:rear是队列最后(含), front是队列最前(不含)
public class ArrayQueueDemo {public static void main(String[] args) {//创建一个队列ArrayQueue arrayQueue = new ArrayQueue(3);char key = ' ';// 接受用户输入Scanner scanner = new Scanner(System.in);boolean loop = true;System.out.println("s(show):显示队列");System.out.println("e(exit):退出程序");System.out.println("a(add):添加数据到队列");System.out.println("g(get):从队列取数据");System.out.println("h(head):查看队列头的数据");while (loop) {key = scanner.next().charAt(0);// 接收第一个字符switch (key) {case 's':arrayQueue.showQueue();break;case 'a':System.out.println("输入一个数:");int value = scanner.nextInt();arrayQueue.addQueue(value);break;case 'g':try {int res = arrayQueue.getQueue();System.out.printf("取出的数据是%d\n", res);continue;} catch (RuntimeException e){System.out.println(e.getMessage());}break;case 'h':try {int res = arrayQueue.headQueue();System.out.printf("队列头的数据是%d\n", res);} catch (RuntimeException e){System.out.println(e.getMessage());}break;case 'e':scanner.close();loop = false;break;default:break;}}System.out.println("程序退出");}
}class ArrayQueue{private int maxSize;//表示数组的最大容量private int front; //队列头private int rear; //队列尾private int[] arr; //该数组用于存放数据,模拟队列public ArrayQueue(int maxSize) {this.maxSize = maxSize;arr = new int[maxSize];front = -1;// 指向队列头部,指向队列头部的数据的前一个位置rear = -1; // 指向队列尾,指向队列尾部的数据}/*** 判断队列是否满* @return*/public boolean isFull() {// 例如最大容量为5,rear是指向队列尾部数据,所以rear为4(maxSize - 1)的时候就为满了return rear == maxSize -1;}/*** 判断队列是否为空* @return*/public boolean isEmpty() {// 因为不是循环队列,头尾不相连,所以rear == front 时队列就为空return rear == front;}/*** 添加数据到队列* @param n*/public void addQueue(int n) {// 判断队列是否满if (isFull()) {System.out.println("队列满,不能加入数据~~");return;}rear++;arr[rear] = n; }/*** 数据出队列* @return*/public int getQueue() {if (isEmpty()) {// 通过抛出异常throw new RuntimeException("队列为空,不能取数据~~");}front++;return arr[front];}/*** 显示队列的所有数据*/public void showQueue() {if (isEmpty()) {System.out.println("队列空的,没有数据~~");return;}for (int i = 0; i < arr.length; i++) {System.out.printf("arr[%d] = %d\n", i, arr[i]);}}/*** 显示队列的头数据,不是取数据而仅仅是显示* @return*/public int headQueue() {if (isEmpty()) {// 通过抛出异常throw new RuntimeException("队列为空,不能取数据~~");}return arr[front + 1];}
}
上一篇:画马
下一篇:候鸟属于隐藏记录第四卷