【数据结构和算法】二维数组与稀疏数组的转换

package com.chw.study.day1209_spareArray;

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

public class SpareArray {

	public static void main(String[] args) throws Exception {
		
		//构造一个二维数组  1表示黑子  2表示白子  ,模拟象棋的存档
		int data[][] = new int[11][11];
		data[2][3]=1;
		data[2][4]=2;
		data[3][4]=1;
		data[3][5]=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 1 2 0 0 0 0 0 0 
		0 0 0 0 1 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 */
		for(int[] row:data){
			for(int c:row){
				System.out.print(c+" ");
			}
			System.out.println();
		}
		
		//算出非0的个数
		int sum=0;
		for(int[] row:data){
			for(int c:row){
				if(c!=0){
					sum++;
				}
			}
		}
		System.out.println("非零个数:"+sum);
		
		int spareArray[][] = new int[sum+1][3];
		spareArray[0][0]=11;
		spareArray[0][1]=11;
		spareArray[0][2]=sum;
		
		int count=0;
		for(int i=0;i<11;i++){
			for(int j=0;j<11;j++){
				if(data[i][j]!=0){
					count++;
					spareArray[count][0]=i;
					spareArray[count][1]=j;
					spareArray[count][2]=data[i][j];
				}
			}
		}
		/*
	   11 11 4 
		2 3 1 
		2 4 2 
		3 4 1 
		3 5 2 */
		for(int[] row:spareArray){
			for(int c:row){
				System.out.print(c+" ");
			}
			System.out.println();
		}
		
		//将数据保存到磁盘中,  D:\data.map
		save(spareArray);
		
		
		//读稀疏数组  D:\data.map
		int [][] _spareArray = readData("D:\\data.map");
		
		//稀疏数组  -> 二维数组
		int [][] _data = new int[_spareArray[0][0]][_spareArray[0][1]];
	    for(int i=1;i<_spareArray.length;i++){
	    	_data[_spareArray[i][0]][_spareArray[i][1]]=_spareArray[i][2];
	    }
	    for(int[] c:_data){
	    	for(int d:c){
	    		System.out.print(" "+d);
	    	}
	    	System.out.println();
	    }
		
	}

	private static int[][] readData(String path) throws Exception {
		File file = new File(path);
		FileInputStream fis = new FileInputStream(file);
		InputStreamReader reader = new InputStreamReader(fis);
		BufferedReader buff = new BufferedReader(reader);
		String line;
		int count=-1;
		int spareArray[][] = null;
		while((line=buff.readLine())!=null){
			System.out.println("------:"+line);
			String date[]=line.split(";");
			count++;
			if(count==0){
				spareArray = new int[Integer.valueOf(date[2])+1][3];
			}
			spareArray[count][0]=Integer.valueOf(date[0]).intValue();
			spareArray[count][1]=Integer.valueOf(date[1]).intValue();
			spareArray[count][2]=Integer.valueOf(date[2]).intValue();
		}
		for(int i=0;i<spareArray.length;i++){
			System.out.print(spareArray[i][0]+";"+spareArray[i][1]+";"+spareArray[i][2]);
			System.out.println();
		}
		return spareArray;
		
	}

	private static void save(int[][] spareArray) throws Exception {
		File file = new File("D:\\data.map");
		FileOutputStream fos = new FileOutputStream(file);
		OutputStreamWriter writer = new OutputStreamWriter(fos);
		for(int i=0;i<spareArray.length;i++){
			System.out.print(spareArray[i][0]+";"+spareArray[i][1]+";"+spareArray[i][2]);
			writer.append(spareArray[i][0]+";"+spareArray[i][1]+";"+spareArray[i][2]+"\n");
		}
	    writer.close();
	    fos.close();
	}
	
	
	
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值