1.稀疏数组
稀疏数组可以看做是普通数组的压缩,但是这里说的普通数组是值无效数据量远大于有效数据量的数组,当一个数组中大部分元素为0(相同数字,可以是0,1,...),
或者为同一个值的数组时,可以使用稀疏数组来保存该数组。
1 1 1 1 1 1
1 5 1 1 1 1
15 1 1 1 1 1 ------------------------------> 6 6 2
1 1 1 1 1 1 1 1 5
1 1 1 1 1 1 2 0 15
1 1 1 1 1 1原始数组 稀疏数组
2.实现思路
(1)二维数组转稀疏数组
- 遍历 原始的二维数组,得到有效数据的个数 sum
- 根据sum 就可以创建 稀疏数组 sparse[sum + 1][3]
- 将二维数组的有效数据数据存入到 稀疏数组
(2)稀疏数组转换成普通的二维数组
- 稀疏数组的行数取决于有效数据的个数,列数始终为3
- 第一行每一列表示二维数组总的行数,列数,有效数据个数
- 此后的行数每一列则分别代表有效数据在二维数组中的行数,列数,及对应的值
public class SparseArrays {
int rows;//行数
int col;//列数
int number;//全局数字
int [][]array;//原始数组
LinkedList<Integer> list=new LinkedList<>();
public SparseArrays(int rows, int col, int number) {
this.rows = rows;
this.col = col;
this.number = number;
array=new int [rows][col];
for(int i=0;i<rows;i++){
for(int j=0;j<col;j++){
array[i][j]=number;
}
}
}
//指定不同数字的位置
public void initArray(int row,int column,int num){
if(row<=rows&&column<=col){
array[row-1][column-1]=num;
}else{
System.out.println("输入无效");
}
}
//得到不同的个数
public int getDiffence(){
int sum=0;
for(int []row:array){
for(int data:row){
if(data!=number){
sum++;
}
}
}
return sum;
}
//转换成稀疏矩阵
private int [][] toSparseArray(String filename){
int i,j;
int k=1;
int rowss=getDiffence();
int [][]spares=new int [rowss+1][3];
//
spares[0][0]=rows;
spares[0][1]=col;
spares[0][2]=rowss;
for(i=0;i<rows;i++){
for(j=0;j<col;j++){
if(array[i][j]!=number){
spares[k][0]=i;
spares[k][1]=j;
spares[k][2]=array[i][j];
k++;
}
}
}
writeFile(filename,spares);//将结果写入文件
return spares;
}
//转换成原始矩阵
public int [][] toArrays(String filename){
int [][]spares=readFile(filename);
int [][]arrays=new int [spares[0][0]][spares[0][1]];
for(int []row:arrays){
for(int data:row){
data=number;
}
}
for(int i=1;i<spares.length;i++){
arrays[spares[i][0]][spares[i][1]]=spares[i][2];
}
return arrays;
}
//打印一个数组
public void print(int [][]arr){
for(int []row:arr){
for(int data:row){
System.out.print(data);
System.out.print('\t');
}
System.out.println();
}
}
//写文件
public void writeFile(String fileName,int [][]arr){
File f=new File(fileName);
FileOutputStream fos=null;
OutputStreamWriter osw=null;
BufferedWriter bw=null;
try {
if (!f.exists()) {
f.createNewFile();
}
fos = new FileOutputStream(f);
osw = new OutputStreamWriter(fos, "utf-8");
bw = new BufferedWriter(osw);
for (int[] roww : arr) {
for (int d : roww) {
bw.write(Integer.toString(d));
bw.flush();
bw.write("\t");
bw.flush();
}
bw.write('\n');
bw.flush();
}
}catch (Exception e){
e.printStackTrace();
}finally {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//得到文件的行数
public static int LineCounts(String filename) throws IOException{
int count = 0;
File file = new File(filename);
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
while((br.readLine())!=null){
count++;
}
return count;
}
//读文件
public int [][] readFile( String fileName){
File f=new File(fileName);
FileInputStream fis=null;
InputStreamReader isr=null;
BufferedReader br=null;
LineNumberReader lnr;
int [][]spares =null;
try {
if (!f.exists()) {
f.createNewFile();
}
fis = new FileInputStream(f);
isr = new InputStreamReader(fis, StandardCharsets.UTF_8);
br = new BufferedReader(isr);
// lnr=new LineNumberReader(new FileReader(fileName));
// int sum=lnr.getLineNumber();
int sum=LineCounts(fileName);
spares=new int [sum][3];
String s;
int k=0;
while ((s=br.readLine())!=null){
String []str;
str=s.split("\t");
spares[k][0]=Integer.parseInt(str[0]);
spares[k][1]=Integer.parseInt(str[1]);
spares[k][2]=Integer.parseInt(str[2]);
k++;
}
}catch (Exception e){
e.printStackTrace();
}finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return spares;
}
public static void main(String[] args) {
SparseArrays t=new SparseArrays(6,6,1);
t.initArray(2,2,5);
t.initArray(3,1,15);
t.print(t.array);
System.out.println();
t.print(t.toSparseArray("test.txt"));
t.print(t.toArrays("test.txt"));
}
}