个人学习记录:希望大家能多多交流 提出宝贵意见 互帮互助 共同进步
喜欢把注释写的比较详细,可忽略。
加油!加油!加油!
package com.eliauk.sparsearray;
import java.io.*;
/**
* @author Eliauk
* @version 1.0.0
* @ClassName SparseArrayToFileAgin.java
* @Description 稀疏数组的转化过程和硬盘存储以及读取的IO流
* @createTime 2021年05月12日 09:17:00
*/
public class SparseArrayToFileAgin {
public static void main(String[] args) {
/**
* 1.创建一个二维数组,实现二维数组对稀疏数组的过程转化
* 2.将稀疏数组写入到硬盘中
* 3.读取硬盘中的数据,恢复成原二维数组
*/
//创建二维数组
int chessArray[][] = new int[11][12];
chessArray[1][1] = 1;
chessArray[2][2] = 2;
chessArray[3][3] = 3;
chessArray[4][4] = 4;
//SUM 记录数组中的有效元素
int sum = 0;
for(int[] row : chessArray){
for(int temp : row){
if(temp != 0){
sum++;
}
System.out.printf("%d\t",temp);
}
System.out.println("");
}
System.out.println("数组中有效元素的个数为:"+sum);
//创建稀疏数组
int sparseArray[][] = new int[sum+1][3];
//给稀疏数组赋值
sparseArray[0][0] = chessArray.length;
sparseArray[0][1] = chessArray[0].length;
sparseArray[0][2] = sum;
//定义一个迭代器 用来记录稀疏数组的行数
int count = 0;
for(int i = 0;i<chessArray.length;i++){
for(int j = 0;j<chessArray[0].length;j++){
if(chessArray[i][j] != 0){
count++;
sparseArray[count][0] = i;
sparseArray[count][1] = j;
sparseArray[count][2] = chessArray[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]);
}
//将稀疏数组写入到桌面文件中 C:\Users\XXX\Desktop\data.txt 路径自定义
File file = new File("C:\\Users\\XXX\\Desktop\\data.txt");
try {
FileOutputStream out = new FileOutputStream(file);
OutputStreamWriter writer = new OutputStreamWriter(out,"UTF-8");
for(int i = 0;i<sparseArray.length;i++){
if(i == sparseArray.length-1){
writer.write(sparseArray[i][0]+","+sparseArray[i][1]+","+sparseArray[i][2]);
}else{
writer.write(sparseArray[i][0]+","+sparseArray[i][1]+","+sparseArray[i][2]+",");
}
}
//关闭流释放资源
writer.close();
out.close();
//读取硬盘上的数据
FileInputStream input = new FileInputStream(file);
InputStreamReader reader = new InputStreamReader(input,"UTF-8");
StringBuffer buffer = new StringBuffer();
while (reader.ready()){
buffer.append((char) reader.read());
}
String str = buffer.toString();
System.out.println("写入硬盘的数据为: "+str);
String sb1[] = str.split(",");
//新建一个空的稀疏数组
int sparseArray2[][] = new int[sb1.length/3][3];
//给稀疏数组赋值
int count1 = 0;
for(int i = 0;i<sparseArray2.length;i++){
sparseArray2[i][0] = Integer.parseInt(sb1[count1]);
sparseArray2[i][1] = Integer.parseInt(sb1[count1+1]);
sparseArray2[i][2] = Integer.parseInt(sb1[count1+2]);
count1+=3;
}
reader.close();
input.close();
System.out.println("读取到的硬盘中的稀疏数组为:");
for(int[] row : sparseArray2){
for(int temp : row){
System.out.printf("%d\t",temp);
}
System.out.println("");
}
//将稀疏数组恢复成二维数组
int chessArray2[][] = new int[sparseArray2[0][0]][sparseArray2[0][1]];
for(int i = 1;i<sparseArray2.length;i++){
chessArray2[sparseArray2[i][0]][sparseArray2[i][1]] = sparseArray2[i][2];
}
System.out.println("恢复后的二维数组为: ");
for(int[] row : chessArray2){
for(int temp : row){
System.out.printf("%d\t",temp);
}
System.out.println("");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
控制台展示结果:
原自定义二维数组:
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 0 2 0 0 0 0 0 0 0 0 0
0 0 0 3 0 0 0 0 0 0 0 0
0 0 0 0 4 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
数组中有效元素的个数为:4
得到的稀疏数组为:
11 12 4
1 1 1
2 2 2
3 3 3
4 4 4
写入硬盘的数据为: 11,12,4,1,1,1,2,2,2,3,3,3,4,4,4
读取到的硬盘中的稀疏数组为:
11 12 4
1 1 1
2 2 2
3 3 3
4 4 4
恢复后的二维数组为:
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 0 2 0 0 0 0 0 0 0 0 0
0 0 0 3 0 0 0 0 0 0 0 0
0 0 0 0 4 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
重点在于知道稀疏数组的行和列代表的含义。