尚硅谷的数据结构中JAVA将数组转为稀疏数组,并将该稀疏数组写到文件中,最后再读取文件恢复成原来最开始的数组

2023年8月10日12:06:30

尚硅谷的数据结构中JAVA将数组转为稀疏数组,并将该稀疏数组写到文件中,最后再读取文件恢复成原来最开始的数组

1.如果二维数组中有许多重复的元素,则保存数组是很废空间资源的。可以使用数组的稀疏数组来减小数组的大小。

上述二维数组的稀疏数组如下:
尚硅谷的数据结构中JAVA将数组转为稀疏数组,并将该稀疏数组写到文件中,最后再读取文件恢复成原来最开始的数组

图中给出了这个稀缺数组第一行中每个元素的具体含义。

至于第二行和第三行中元素的含义,我只以第二行为例

第二行中的第三个元素:表示原始数组中的元素“1”

第二行中的第一个元素:元素“1”的行号索引,从0开始

第二行中的第二个元素:元素“1”的列号索引,从0开始

下面是完整的代码,数组转换部分是指教程视频,IO部分是教程视频中给出的作业。

package com.atguigu.sparsearray;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;

public class SparseArray 
{
	public static void main(String[] args) throws Exception
	{
		//定义原始的数组
		int [][] chessArray=new int[11][11];//元素的默认值是0
		chessArray[1][2]=1;//黑色的棋子
		chessArray[2][3]=2;	//蓝色的棋子
		for (int[] is : chessArray)
		{
			for (int a : is) 
			{
				System.out.print(a+"\t");
			}
			System.out.println();
		}
		
		int sum=0;//总共有多少个不同的值
		for (int i = 0; i < chessArray.length; i++) //chessArray.length其实是二维数组的行数
		{
			for (int j = 0; j < chessArray.length; j++) //遍历一行,也就是一维数组,这里行数和列数相等,所以可以用chessArray.length
			{
				if(chessArray[i][j]!=0)
				{
					sum++;
				}
			}
		}
		
		//定义一个稀疏数组
		int [][] sparseArray=new int[sum+1][3];//稀疏数组的列数是固定的“3”
		//给稀疏数组的元素赋值
		sparseArray[0][0]=11;
		sparseArray[0][1]=11;
		sparseArray[0][2]=sum;
		int count=0;
		for (int i = 0; i < 11; i++) 
		{
			for (int j = 0; j < 11; j++) 
			{
				if(chessArray[i][j]!=0)
				{
					count++;
					sparseArray[count][0]=i;
					sparseArray[count][1]=j;
					sparseArray[count][2]=chessArray[i][j];
				}
			}
		}
		
		System.out.println("-----------------------------");
		System.out.println("稀疏数组:");
		//打印被赋值之后的稀疏数组
		for (int[] is : sparseArray) 
		{
			for ( int s2 : is) 
			{
				System.out.print(s2+"\t");
			}
			System.out.println();
		}
		System.out.println("-----------------------------");
		System.out.println("把稀疏数组保存到文件中。。。");
		FileOutputStream fileOutputStream=new FileOutputStream(new File("chess.txt"));
		
		for (int i = 0; i < sparseArray.length; i++) 
		{
			for (int j = 0; j < 3; j++) 
			{
				int a=sparseArray[i][j];
				if(j==2)//如果是最后一列就不加逗号
				{
					fileOutputStream.write((String.valueOf(a)).getBytes());
				}
				else //否则就加逗号
				{
					fileOutputStream.write((String.valueOf(a)+",").getBytes());
				}
			}
			fileOutputStream.write("\n".getBytes());
		}
		System.out.println("------------------");
		System.out.println("读取文件中的稀疏数组并恢复成原来的数组:");
		
		BufferedReader bufferedReader=new BufferedReader(new FileReader("chess.txt"));//字符缓冲流
		String line=null;
		int c=0;
		String row=null;
		String col=null;
		int [][] chessRestore=null;
		while((line=bufferedReader.readLine())!=null)
		{
			c++;
			if(c==1)//如果是稀疏矩阵的第一行
			{
				String [] array=line.split(",");
				row=array[0];
				col=array[1];
				chessRestore=new int[Integer.parseInt(row)][Integer.parseInt(col)];
			}
			else 
			{
				String [] array=line.split(",");
				String hang=array[0];
				String lie=array[1];
				String val=array[2];
				chessRestore[Integer.parseInt(hang)][Integer.parseInt(lie)]=Integer.parseInt(val);
			}
			
			
		}
		
		for (int[] is : chessRestore) 
		{
			for (int is2 : is) 
			{
				System.out.print(is2+"\t");
			}
			System.out.println();
		}
		
		
		
	}

}


运行结果图,最终文件会生成在你创建项目文件里

尚硅谷的数据结构中JAVA将数组转为稀疏数组,并将该稀疏数组写到文件中,最后再读取文件恢复成原来最开始的数组

  • 作者:GEM的左耳返
  • 原文链接:https://blog.csdn.net/weixin_49437670/article/details/117933907
    更新时间:2023年8月10日12:06:30 ,共 2568 字。