稀疏数组

先看一个实际需求:

在这里插入图片描述
因为该二维数组的很多值是默认值 0, 因此记录了很多没有意义的数据.->稀疏数组。

稀疏数组的处理方法是:

  1. 记录数组一共有几行几列,有多少个不同的值
  2. 把具有不同值的元素的行列及值记录在一个小规模的数组中,从而缩小程序的规模
    在这里插入图片描述
    本例实现将二维数组转化成稀疏数组并保存到文件,再进行读取

代码实现

package com.jachin.sparsearray;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class SparseArray {

	public static void main(String[] args) {
		// 创建一个原始的二维数组11*11
		// 0:表示没有棋子,1:表示黑子 2:表示篮子
		int chessArr1[][] = new int[11][11];
		chessArr1[1][2] = 1;
		chessArr1[2][3] = 2;
		// 输出原始的二维数组
		System.out.println("原始的二维数组。。");
		for (int[] row : chessArr1) {
			for (int data : row) {
				System.out.print(data + "\t");
			}
			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++;
				}
			}
		}
		System.out.println("sum = " + sum);
		// ----------------------------------------------------------------
		// 2.创建对应的稀疏数组
		int sparseArr[][] = new int[sum + 1][3];
		// 给稀疏数组赋值
		sparseArr[0][0] = 11;
		sparseArr[0][1] = 11;
		sparseArr[0][2] = sum;

		// 遍历二维数组,将非0 的值存放到稀疏数组中
		int count = 0;
		for (int i = 0; i < chessArr1.length; i++) {
			for (int j = 0; j < chessArr1.length; j++) {
				if (chessArr1[i][j] != 0) {
					count++;
					sparseArr[count][0] = i;
					sparseArr[count][1] = j;
					sparseArr[count][2] = chessArr1[i][j];
				}
			}
		}

		// 保存稀疏数组到文件
		FileOutputStream fos = null;
		OutputStreamWriter opsw = null;
		try {
			File file = new File("d:\\input\\map.data");
			fos = new FileOutputStream(file);
			opsw = new OutputStreamWriter(fos);

			System.out.println("转换后的稀疏数组。。");
			for (int i = 0; i < sparseArr.length; i++) {
				System.out.printf("%d\t%d\t%d\t\n", sparseArr[i][0], sparseArr[i][1], sparseArr[i][2]);

				if (i == sparseArr.length - 1) {
					opsw.append(sparseArr[i][0] + "," + sparseArr[i][1] + "," + sparseArr[i][2]);
				} else {
					opsw.append(sparseArr[i][0] + "," + sparseArr[i][1] + "," + sparseArr[i][2] + ",");
				}
			}
			System.out.println("写入文件成功...");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (opsw != null) {
				try {
					opsw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		// 将稀疏数组从文件中读取到内存
		File file = new File("d:\\input\\map.data");
		FileInputStream fis = null;
		InputStreamReader isr = null;
		BufferedReader br = null;
		int readRow = 0;//记录行数
		//建新的稀疏数组
        int[][] sparseArr2 = new int[count+1][3];
		try {
			fis = new FileInputStream(file);
			isr = new InputStreamReader(fis);
			br = new BufferedReader(isr);
			String line = null;
			System.out.println("正在读取数据...");
			while ((line = br.readLine()) !=null) {
                System.out.println(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(br != null){
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(fis != null){
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(isr != null){
				try {
					isr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		// ----------------------------------------------------------------
		// 3.将稀疏数组恢复成原始的二维数组
		int chessArr2[][] = new int[sparseArr[0][0]][sparseArr[0][1]];

		for (int i = 1; i < sparseArr.length; i++) {
			chessArr2[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
		}

		// 恢复后的二维数组
		System.out.println("恢复后的二维数组。。");
		for (int[] row : chessArr1) {
			for (int data : row) {
				System.out.print(data + "\t");
			}
			System.out.println();
		}

	}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值