• java数据结构(一)--稀疏数组


    1.数据结构包括:线性结构和非线性结构

     2.稀疏数组和队列

     

     

     二维数组和稀疏数组互转

     稀疏数组的代码实现

    /**
     * 稀疏数组
     */
    public class SparseArray {
    
    
        public static void main(String[] args) throws IOException {
            String path = "E:\rray.txt";
            //1.定义一个二维数组11*11 0--没有棋子 1-白棋  2-黑棋
            int rray[][] = new int[11][11];
            rray[1][2] = 1;
            rray[2][3] = 2;
            rray[6][8] = 2;
            System.out.println("二维数组为:");
            saveFile("二维数组为:", path);
            for (int[] crow : rray) {
                for (int roll : crow) {
                    System.out.printf("%d	", roll);
                }
                System.out.println();
            }
            //数组存盘
            saveFile(rray, path);
            //2.二维数组转化为稀疏数组
            //先得到棋子个数
            int sum = 0;
            for (int[] crow : rray) {
                for (int roll : crow) {
                    if (roll != 0) {
                        sum++;
                    }
                }
            }
            System.out.println("棋子个数:" + sum);
            saveFile("棋子个数:" + sum, path);
            //创建稀疏数组
            int parseArr[][] = new int[sum + 1][3];
            parseArr[0][0] = 11;
            parseArr[0][1] = 11;
            parseArr[0][2] = sum;
            int count = 0;
            for (int i = 0; i < 11; i++) {
                for (int j = 0; j < 11; j++) {
                    if (rray[i][j] != 0) {
                        count++;
                        parseArr[count][0] = i;
                        parseArr[count][1] = j;
                        parseArr[count][2] = rray[i][j];
                    }
                }
            }
            //打印稀疏数组
            System.out.println("稀疏数组为:");
            for (int[] crow : parseArr) {
                for (int roll : crow) {
                    System.out.printf("%d	", roll);
                }
                System.out.println();
            }
            System.out.println("稀疏数组长度:" + (parseArr.length));
            saveFile("稀疏数组长度:" + (parseArr.length), path);
            saveFile("稀疏数组为:", path);
            //数组存盘
            saveFile(parseArr, path);
    
            //3.稀疏数组转二维数组
            //创建二维数组
            int newRray[][] = new int[parseArr[0][0]][parseArr[0][1]];
            for (int i = 1; i < parseArr.length; i++) {
                newRray[parseArr[i][0]][parseArr[i][1]] = parseArr[i][2];
            }
            System.out.println("还原后的二维数组:");
            saveFile("还原后的二维数组:", path);
            //数组存盘
            saveFile(newRray, path);
            for (int[] crow : newRray) {
                for (int roll : crow) {
                    System.out.printf("%d	", roll);
                }
                System.out.println();
            }
            //读取存储的文件
            String s = readFile(path);
            System.out.println(s);
        }
    
        /**
         * 将数组存盘
         *
         * @param rray
         * @param path
         */
        private static void saveFile(int[][] rray, String path) throws IOException {
            File file = new File(path);
            BufferedWriter bf = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true), "UTF-8"));
            for (int[] crow : rray) {
                for (int col : crow) {
                    bf.write(col + "	");
                }
                bf.write("
    ");
            }
            bf.flush();
            bf.close();
        }
    
        /**
         * 将文字存盘
         *
         * @param str
         * @param path
         */
        private static void saveFile(String str, String path) throws IOException {
            File file = new File(path);
            BufferedWriter bf = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true), "UTF-8"));
            bf.write(str + "
    ");
            bf.flush();
            bf.close();
        }
    
        /**
         * 读取文件内容
         *
         * @param path
         */
        private static String readFile(String path) throws IOException {
            File file = new File(path);
            BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8"));
            StringBuilder sb = new StringBuilder();
            while (br.readLine()!=null){
                sb.append(br.readLine());
                sb.append("
    ");
            }
            return sb.toString();
        }
    
    }
  • 相关阅读:
    会议总结
    排球比赛积分规则
    我的计算机历程和认识
    排球积分程序
    《如何成为一个高手》观后感
    十八周总结
    十六周总结(流程)
    排球计分程序
    十四周学习总结
    十三周学习总结
  • 原文地址:https://www.cnblogs.com/wiliamzhao/p/14548587.html
Copyright © 2020-2023  润新知