栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

稀疏数组demo

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

稀疏数组demo

稀疏数组demo
import java.io.*;
import java.lang.reflect.Array;
import java.nio.charset.StandardCharsets;


public class SparseArrayDemo {


    public static void main(String[] args) {

        //创建一个原始二维数组(10x10),模拟棋盘
        int[][] array = new int[10][10];
        //二维数组赋值(0:没有棋子,1:白棋,2:黑棋)
        array[1][5] = 1;
        array[5][2] = 2;
        array[3][7] = 2;
        //路径
        String path = "data.txt";

        System.out.println("---------------------");
        System.out.println("原始数组:");
        printArray(array);
        System.out.println("---------------------");

        System.out.println("数组转稀疏数组:");
        int[][] sparseArray = arrayToSparseArray(array);
        printArray(sparseArray);
        System.out.println("---------------------");

        System.out.println("稀疏数组保存到文件");
        writeArray(sparseArray,path);
        System.out.println("---------------------");


        System.out.println("读取文件到稀疏数组");
        int[][] sparseArrayFile = readArray(path);
        printArray(sparseArrayFile);
        System.out.println("---------------------");
        
        System.out.println("稀疏数组转数组");
        int[][] arrayFile = sparseArrayToArray(sparseArrayFile);
        printArray(arrayFile);
        System.out.println("---------------------");




    }




    
    public static int[][] arrayToSparseArray(int[][] array) {


        //获取数组中有效值数量(!=0)
        int count = 1;
        for (int[] arr : array) {
            for (int i : arr) {
                if (i != 0) {
                    count++;
                }
            }
        }

        //创建稀疏数组
        int[][] sparseArray = new int[count][3];

        //向稀疏数组赋值
        sparseArray[0][0] = array.length;
        sparseArray[0][1] = array[0].length;
        sparseArray[0][2] = count;

        int x = 1;
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                if (array[i][j] != 0) {
                    sparseArray[x][0] = i;
                    sparseArray[x][1] = j;
                    sparseArray[x][2] = array[i][j];
                    x++;
                }
            }
        }


        return sparseArray;
    }


    
    public static int[][] sparseArrayToArray(int[][] sparseArray) {

        //创建数组
        int[][] array = new int[sparseArray[0][0]][sparseArray[0][1]];

        //向数组赋值
        int x = 0;
        int y = 0;
        int z = 0;
        for (int i = 1; i < sparseArray.length; i++) {
            for (int j = 0; j < sparseArray[i].length; j++) {
                switch (j) {
                    case 0:
                        x = sparseArray[i][j];
                        break;
                    case 1:
                        y = sparseArray[i][j];
                        break;
                    case 2:
                        z = sparseArray[i][j];
                        break;

                }
            }
            array[x][y] = z;
        }

        return array;

    }


    
    public static void printArray(int[][] array) {

        //遍历
        for (int[] arr : array) {
            for (int i : arr) {
                System.out.printf(i + "t");
            }
            System.out.println();
        }

    }


    
    public static void writeArray(int[][] array, String path) {

        FileOutputStream fileOutputStream = null;

        try {

            File file = new File(path);

            fileOutputStream = new FileOutputStream(file, true);

            String value = "";

            for (int[] arr : array) {
                for (int i : arr) {
                    value += i + "t";
                }
                value += "n";
            }

            fileOutputStream.write(value.getBytes(StandardCharsets.UTF_8));

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            //关闭流
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }

        System.out.println("保存成功...");

    }


    
    public static int[][] readArray(String path) {


        int[][] array = null;

        File file = new File(path);

        FileInputStream fileInputStream = null;

        InputStreamReader inputStreamReader = null;


        if (file.isFile() && file.exists()) {

            try {

                fileInputStream = new FileInputStream(file);

                inputStreamReader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8);

                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

                String lineTxt = null;

                int x = 0;

                while ((lineTxt = bufferedReader.readLine()) != null) {


                    String[] line = lineTxt.split("t");

                    if (x == 0) {
                        array = new int[Integer.parseInt(line[2])][3];
                    }

                    array[x][0] = Integer.parseInt(line[0]);
                    array[x][1] = Integer.parseInt(line[1]);
                    array[x][2] = Integer.parseInt(line[2]);
                    x++;
                }

            } catch (Exception e) {

                e.printStackTrace();

            } finally {

                //关闭流
                if (inputStreamReader != null) {
                    try {
                        inputStreamReader.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                if (fileInputStream != null) {
                    try {
                        fileInputStream.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            }

            System.out.println("读取成功...");

        } else {

            System.out.println("文件不存在...");

        }

        return array;
    }


}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/632065.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号