zoukankan      html  css  js  c++  java
  • 稀疏数组 + 输出文件

    稀疏数组

        //需求稀疏数组
        @Test
        public void test1(){
            //原始二维数组
            // 创建一个原始的二维数组 11 * 11
            // 0: 表示没有棋子, 1 表示 黑子 2 表蓝子
            int chessArr1[][] = new int[11][11];
            chessArr1[1][2] = 1;
            chessArr1[2][3] = 2;
            chessArr1[4][5] = 2;
            // 输出原始的二维数组
            System.out.println("原始的二维数组~~");
            for (int[] row : chessArr1) {
                for (int data : row) {
                    System.out.printf("%d	", data);
                }
                System.out.println("|");
                System.out.println();
    
            }
    
            // 将二维数组 转 稀疏数组的思
            // 1. 先遍历二维数组 得到非0数据的个数
            int sum = 0;
            for (int i = 0; i < 11; i++) {
                for (int j = 0; j < 11; j++) {
                    if (chessArr1[i][j] != 0) {
                        sum++;
                    }
                }
            }
    
            // 2. 创建对应的稀疏数组
            int sparseArr[][] = new int[sum + 1][3];
            // 给稀疏数组赋值
            sparseArr[0][0] = 11;
            sparseArr[0][1] = 11;
            sparseArr[0][2] = sum;
    
            // 遍历二维数组,将非0的值存放到 sparseArr中
            int count = 0; //count 用于记录是第几个非0数据
            for (int i = 0; i < 11; i++) {
                for (int j = 0; j < 11; j++) {
                    if (chessArr1[i][j] != 0) {
                        count++;
                        sparseArr[count][0] = i;
                        sparseArr[count][1] = j;
                        sparseArr[count][2] = chessArr1[i][j];
                    }
                }
            }
    
            System.out.println();
            System.out.println("得到稀疏数组为~~~~");
            for (int i = 0; i < sparseArr.length; i++) {
                //System.out.println();
                System.out.printf("%d	%d	%d	
    ", sparseArr[i][0], sparseArr[i][1], sparseArr[i][2]);
            }
            System.out.println();
    
            writeListToFile(chessArr1,"11");
            writeListToFile(sparseArr,"3");
    
    
        }
    

      输出文件方法:

        private void writeListToFile(int[][] Arrays,String line) {
    
            //文件路径
            File file =new File("输出文件地址");
            //判断文件是否存在
            if(!file.exists()){
                try {
                    file.createNewFile();
                    System.out.println("文件" + file.getName() +"不存在以为您创建。");
                } catch (IOException e) {
                    System.out.println("创建文件异常");
                    e.printStackTrace();
                }
            }else {
                System.out.println("文件" + file.getName() + "已经存在。");
            }
            //创建输出流
            FileOutputStream fos = null;
            PrintStream ps = null;
            //遍历数组
            int count = 0;
            for (int[] row : Arrays) {
                for (int data : row) {
                    ++count;
                    //System.out.print("计数器:"+count);
                    System.out.printf("%d	", data);
                    try{
                        //追加
                        fos =new FileOutputStream(file,true);
                        ps = new PrintStream(fos);
                    }catch (FileNotFoundException e){
                        e.printStackTrace();
                    }
                    //转换String
                    String str = Integer.toString(count);
                    int strData = String.valueOf(data).length();
    
                    if(line.equals(str)){
                        String string = data + " ";
                        ps.print(string);
                        ps.print("
    ");
                    }else{
                        //判断字符数大于1
                        if(strData<=1){
                            String string = data + " " +addBlank(strData);
                            ps.print(string);
                        }else {
                            String string = data + " ";
                            ps.print(string);
                        }
    
                    }
    
                }
                //清零
                count = 0;
                //System.out.println("|");
                System.out.println();
    
            }
            ps.print("
    "+"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"+"
    ");
            ps.close();
            System.out.println("写入文件完毕。");
        }
     

      对仗工整:

        //对仗整齐
        private String addBlank(int num) {
            //int intNum=Integer.valueOf(num).intValue();
            for(int i = 0;i<num;i++){
                System.out.print(" ");
                return " ";
            }
            return null;
        }
    

      

  • 相关阅读:
    高级定制_百度百科
    大叔也学Xamarin系列
    WebApi系列~dynamic让你的省了很多临时类
    大叔也说Xamarin~Android篇~支付宝SDK的集成
    知方可补不足~sqlserver中使用ROW_NUMBER进行的快速分页
    EF架构~有时使用SQL更方便
    【deep learning学习笔记】注释yusugomori的LR代码 --- LogisticRegression.h
    线程同步
    泛型接口的实现方式之二
    jsp获得本地及serverIP的方法
  • 原文地址:https://www.cnblogs.com/money131/p/13237008.html
Copyright © 2011-2022 走看看