一个类中声明两个方法
方法1.将二维数组存入txt文件中
方法2.读取txt文件并写出到新的二维数组中
package sparseArray;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class readwrite {
/**\
* 将二维数组存入txt文件中
* @param array
*/
public static void saveArray(int[][] array){
FileWriter fw = null;
try {
//1.写入到哪个文件
File file = new File("Array.txt");//这里用的相对路径(本工程下)
//2.创建输出流对象,传入文件对象
fw = new FileWriter(file);
//3.如果该文件不存在就创建
if (!file.exists()){
file.createNewFile();
}
//4.写入操作:遍历将数组写入txt文件中:
for (int i = 0; i < array.length; i++) {
//数据前n-1列尾部加入","
for (int j = 0; j < array[0].length-1; j++) {
fw.write(array[i][j]+",");
}
//数组最后一列后面不加","
fw.write(array[i][array[0].length-1]+"");
//换行
fw.write("\n");
}
//刷新,写入文件
fw.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭流
if (fw!=null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**\
* 读取txt文件并写出到新的二维数组中
* @return int[][]
*/
public static int[][] readArray(){
//将二维数组返回
FileReader fr = null;
BufferedReader br = null;
int[][] array = null;
try {
//1.指明要读取的txt文件的路径
fr = new FileReader("Array.txt");
//2.把文件放入缓冲流中
br = new BufferedReader(fr);
//3.创建一个集合用来存放读取文件中的内容
List<String> list = new ArrayList<>();
//3.1 用来存放一行的数据
String line;
//4.逐行读取txt文件中的内容
while ((line = br.readLine())!=null){
//4.1读取的行添加到list集合中
list.add(line);
}
//5.获取文件有多少行
int row = list.size();
//6.获取数组有多少列
String s = list.get(0);//先获取链表索引为0的值
int column = s.split("\\,").length;//然后根据正则表达式逗号获取有多少个逗号分开的数据,即行数
//7.根据文件行数和列数来创建一个新数组,用来返回稀疏数组
array = new int[row][column];
//8.记录输出当前行
int count = 0;
//9.遍历集合并将集合中的数据放入新数组array中
for (String str:list){
//10.将读取的数组用","来分割,用字符串数组来接收
String[] strs = str.split("\\,");
for (int i = 0; i < column; i++) {
array[count][i] = Integer.valueOf(strs[i]);
}
count++;//行数+1
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭流
if (fr!=null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (br!=null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//返回稀疏数组
return array;
}
}
主方法测试类:
public class SparseArray {
public static void main(String[] args) {
//1.创建一个原始的二维数组 11*11
int chessArr1[][] = new int[11][11];
//给棋盘赋值 0 无子 1 黑子 2蓝子
chessArr1[1][2] = 1;
chessArr1[2][3] = 2;
chessArr1[3][4] = 2;
chessArr1[6][5] = 1;
//............可以接着下//
//输出原始的二维数组
System.out.println("原始的二维数组为:");
for (int[] row: chessArr1){//外层控制行数
for (int data: row){//内层给每一行的数赋值
System.out.printf("%d\t",data);
}
System.out.println();
}
//二维数组转稀疏数组:
//1.遍历二维数组,取到非0数据的个数
int sum = 0;
for (int i = 0; i < chessArr1.length; i++) {
for (int j = 0; j < chessArr1.length; j++) {
if (chessArr1[i][j]!=0){
sum++;
}
}
}
//2.创建对应的稀疏数组
int sparesArr[][] = new int[sum+1][3];
//2.1给稀疏数组赋值
sparesArr[0][0] = 11;
sparesArr[0][1] = 11;
sparesArr[0][2] = sum;
//遍历二维数组,将非0值存放到sparesArr中
int count = 0; //用于记录第几个是非0数据
for (int i = 0; i < chessArr1.length; i++) {
for (int j = 0; j < chessArr1.length; j++) {
if (chessArr1[i][j]!=0){
count++;
sparesArr[count][0] = i; //第一个非0数据放在稀疏数组的第二行
sparesArr[count][1] = j; //第二个非0数据放在稀疏数组的第三行
sparesArr[count][2] = chessArr1[i][j];
}
}
}
System.out.println("得到的稀疏数组为:");
//输出稀疏数组的形式
for (int i = 0; i < sparesArr.length; i++) {
System.out.printf("%d\t%d\t%d\t\n", sparesArr[i][0], sparesArr[i][1], sparesArr[i][2]);
}
System.out.println();
//把稀疏数组存入到文件中
System.out.println("-----------------存盘--------------------");
readwrite.saveArray(sparesArr);
System.out.println("-----------------分割线--------------------");
//把稀疏数组从文件中写出到控制台
System.out.println("-----------------恢复--------------------");
int[][] array = readwrite.readArray();
for (int[] row:array){
for (int data:row){
System.out.printf("%d\t",data);
}
System.out.println();
}
System.out.println();
System.out.println("恢复原来的二维数组为:");
//恢复原来的二维数组
//1.创建原始的二维数组
int chessArr3[][] = new int[array[0][0]][array[0][1]];
//2.读取稀疏数组的后几行,并从第二行开始赋给原来的二维数组
for (int i = 1; i < array.length; i++) {
chessArr3[array[i][0]][array[i][1]] = array[i][2];
}
//3.输出恢复后的二维数组
for (int[] row:chessArr3){
for (int data:row){
System.out.printf("%d\t",data);
}
System.out.println();
}
}
}