java稀疏数组代码实现

将数组转为稀疏数组存到磁盘中,再从磁盘中读取稀疏数组恢复原来的数组:

代码:

package course;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Scanner;

/**
 * 稀疏数组
 * @author DELL
 *
 */
public class SparseArray {
	public static void main(String[] args) {
		// 创建一个原始的二维数组
		// 0 表示没有棋子,1 表示黑子,2 表示篮子
		int[][] chessArr1 = new int[11][11];
		chessArr1[1][2] = 1;
		chessArr1[2][3] = 2;
		// 输出原始的二维数组
		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 < 11; i++) {
			for (int j = 0; j < 11; j++) {
				if (chessArr1[i][j] != 0) {
					sum++;
				}
			}
		}
		System.out.println(sum);
		
		// 2. 创建对应的稀疏数组
		int sparseArr[][] = new int[sum+1][3];
		// 给稀疏数组赋值
		sparseArr[0][0] = 11;
		sparseArr[0][1] = 11;
		sparseArr[0][2] = sum;
		
		// 3. 遍历二维数组,将非0的值存放到稀疏数组中
		int count = 0; // 用于记录是第几个非零数据
		for (int i = 0; i < 11; i++) {
			for (int j = 0; j < 11; j++) {
				if (chessArr1[i][j] != 0) {
					count++;
					sparseArr[count][0] = i;
					sparseArr[count][1] = j;
					sparseArr[count][2] = chessArr1[i][j];
				}
			}
		}
		
		// 输出稀疏数组的形式
		System.out.println("**********************");
		System.out.println("得到的稀疏数组为:");
		for (int i = 0; i < sparseArr.length; i++) {
			System.out.printf("%d\t%d\t%d\t", sparseArr[i][0], sparseArr[i][1], sparseArr[i][2]);
			System.out.println();
		}
		
		File dest = new File("src/稀疏数组.txt");
		Writer os = null;
		BufferedWriter bw = null;
		try {
			os = new FileWriter(dest);
			bw = new BufferedWriter(os);
			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < sparseArr.length; i++) {
				for (int j = 0; j < sparseArr[i].length; j++) {
					sb.append(sparseArr[i][j] + " ");
				}
				sb.deleteCharAt(sb.length() - 1);
				bw.write(sb.toString());
				bw.newLine();
				sb.delete(0, sb.length());
			}
			bw.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (os != null) {
				try {
					os.close();
					bw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		
		// 将稀疏数组恢复成原始的二维数组
		File src = new File("src/稀疏数组.txt");
		Scanner scan = null;
		int rows;
		int cols;
		try {
			scan = new Scanner(src);
			rows = scan.nextInt();
			cols = scan.nextInt();
			sum = scan.nextInt();
			sparseArr = new int[sum+1][3];
			count = 1;
			while (scan.hasNextInt()) {
				for (int i = 0; i < 3; i++) {
					sparseArr[count][i] = scan.nextInt();
				}
				count++;
			}
			
			int[][] chessArr2 = new int[rows][cols];
			
			for (int i = 1; i < sparseArr.length; i++) {
				chessArr2[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
			}
			
			// 输出恢复后的二维数组
			System.out.println("*********************");
			System.out.println("恢复后的二维数组");
			for (int[] row: chessArr2) {
				for (int data: row) {
					System.out.printf("%d\t", data);
				}
				System.out.println();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			scan.close();
		}
		
	}
}

结果:

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	
**********************
得到的稀疏数组为:
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	

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值