1-稀疏数组
稀疏数组
基本介绍
在一个数组中, 大部分的元素为同一个值,可以用稀疏数组来保存该数组
压缩数组
稀疏数组的基本处理方法
- 记录数组一共有几行几列 有多少不同值
- 把具有不同值的行列及其值记录在一个小规模数组中 从而缩小数组规模

6x7 -》 3x9
应用


二维数组转稀疏数组的思路
1.遍历原始的二维数组,得到有效数据的个数sum
2.根据sum就可以创建稀疏数组sparseArr int[sum+1] [3]
3.将二维数组的有效数据数据存入到稀疏数组
稀疏数组转原始的二维数组的思路
1.先读取稀疏数组的第一行,根据第一行的数据,创建原始的二维数组,比如上面的chessArr2 = int [11][11]
2.在读取稀疏数组后几行的数据,并赋给原始的二维数组即可.
package com.company.sparseArray;
import javax.sound.midi.Soundbank;
import java.lang.reflect.Array;
import java.util.concurrent.ForkJoinPool;
/**
* @Function :
* date 2021/5/8 - 14:28
* How :
*/
public class SparseArray {
public static void main(String[] args) {
//创建原始二维数组 10x10 0 代表空 1代表A 2代表B
int chessArray[][] = new int[11][11];
chessArray[1][2] = 1;
chessArray[2][3] = 2;
System.out.println("原始二维数组");
for (int row[] : chessArray) {
for (int col : row){
System.out.printf("\t"+col);
}
System.out.println();
}
//遍历数组 得到非0的数据个数
int sum = 0;
for (int row[] : chessArray) {
for (int col : row){
if (col != 0){
sum++;
}
}
}
System.out.println("有效数据个数"+sum);
System.out.println("-----------------------------------------");
//创建稀疏数组
int sparseArray[][] = new int[sum+1][3];
//稀疏数组赋值
sparseArray[0][0] = 11;
sparseArray[0][1] = 11;
sparseArray[0][2] = sum;
//遍历数组 将非零的数据存入
int count = 0;
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (chessArray[i][j] != 0){
count++;
sparseArray[count][0] = i;
sparseArray[count][1] = j;
sparseArray[count][2] = chessArray[i][j];
}
}
}
System.out.println();
System.out.println("得到的稀疏数组为:");
for (int row[] : sparseArray){
for(int col : row){
System.out.printf("\t"+col);
}
System.out.println();
}
System.out.println();
System.out.println("-----------------------------------------");
//将稀疏数组恢复为二维数组
int arrayRow = sparseArray[0][0];
int arrayCol = sparseArray[0][1];
int chessArray2[][] = new int[arrayRow][arrayCol];
System.out.println("恢复后的二维数组:");
for (int i = 1; i <sparseArray.length ; i++) {
chessArray2[sparseArray[i][0]][sparseArray[i][1]] = sparseArray[i][2];
}
for (int row[] : chessArray2){
for(int col : row){
System.out.printf("\t"+col);
}
System.out.println();
}
}
}
结果:
附:包含IO代码
package com.company.sparseArray;
import javax.sound.midi.Soundbank;
import java.io.*;
import java.lang.reflect.Array;
import java.util.concurrent.ForkJoinPool;
/**
* @Function :
* date 2021/5/8 - 14:28
* How :
*/
public class SparseArray {
public static void main(String[] args) {
//创建原始二维数组 10x10 0 代表空 1代表A 2代表B
int chessArray[][] = new int[11][11];
chessArray[1][2] = 1;
chessArray[2][3] = 2;
System.out.println("原始二维数组");
for (int row[] : chessArray) {
for (int col : row){
System.out.printf("\t"+col);
}
System.out.println();
}
//遍历数组 得到非0的数据个数
int sum = 0;
for (int row[] : chessArray) {
for (int col : row){
if (col != 0){
sum++;
}
}
}
System.out.println("有效数据个数"+sum);
System.out.println("-----------------------------------------");
//创建稀疏数组
int sparseArray[][] = new int[sum+1][3];
//稀疏数组赋值
sparseArray[0][0] = 11;
sparseArray[0][1] = 11;
sparseArray[0][2] = sum;
//遍历数组 将非零的数据存入
int count = 0;
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (chessArray[i][j] != 0){
count++;
sparseArray[count][0] = i;
sparseArray[count][1] = j;
sparseArray[count][2] = chessArray[i][j];
}
}
}
System.out.println();
System.out.println("得到的稀疏数组为:");
for (int row[] : sparseArray){
for(int col : row){
System.out.printf("\t"+col);
}
System.out.println();
}
System.out.println();
System.out.println("-----------------------------------------");
//将稀疏数组恢复为二维数组
int arrayRow = sparseArray[0][0];
int arrayCol = sparseArray[0][1];
int chessArray2[][] = new int[arrayRow][arrayCol];
System.out.println("恢复后的二维数组:");
for (int i = 1; i <sparseArray.length ; i++) {
chessArray2[sparseArray[i][0]][sparseArray[i][1]] = sparseArray[i][2];
}
for (int row[] : chessArray2){
for(int col : row){
System.out.printf("\t"+col);
}
System.out.println();
}
//持久化
File file = null;
FileWriter out = null;
try {
file = new File("./array.txt"); //存放数组数据的文件
out = new FileWriter(file); //文件写入流
//将数组中的数据写入到文件中。每行各数据之间TAB间隔
for (int i=0; i<3; i++){
for (int j = 0; j < sum+1; j++) {
out.write(sparseArray[i][j]+"\t");
}
out.write('\n');
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (out != null){
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("持久化成功!");
}
// 读取持久化
BufferedReader in = null;
int row = 0;
String line = null;
int arr[][] = new int [3][3];
try {
in = new BufferedReader(new FileReader(file));
while((line = in.readLine()) != null){
String[] temp = line.split("\t");
for(int j=0;j<temp.length;j++){
arr[row][j] = Integer.parseInt(temp[j]);
}
row++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (in!=null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
for (int row1[] : arr){
for(int col : row1){
System.out.printf("\t"+col);
}
System.out.println();
}
}
}

浙公网安备 33010602011771号