如何替换2D Java arraylist中的空元素?

问题描述 投票:0回答:1

我打算将ArrayList中的空元素替换为前导/尾随元素值(如果相似或不适用)。程序在运行的初始步骤中返回false,因此我无法继续实现它。我究竟做错了什么?请为我建议一种合适的方法,谢谢

            File file=new File("file1.csv");
            Scanner inputStream;
            List<String> header = null; //Store the header in a separate list
            List<List<String>> lines = new ArrayList<>();
            try{
                inputStream = new Scanner(file);

                while(inputStream.hasNext()){
                    String line= inputStream.next();
                    String[] values = line.split(",");
                    if (header == null){
                        header= Arrays.asList(values);
                        continue;//go to the next line as header is read
                    }
                    // Adds the currently parsed line 
                    lines.add(Arrays.asList(values));
                }
                inputStream.close();
            }catch (FileNotFoundException e) {
                e.printStackTrace();
            }
             //fails; outputs "false"
               System.out.println(lines.removeAll(Arrays.asList(Arrays.asList(" "))));

              //test on 1D arraylist- successful
              List<String> colors = new ArrayList<>(Arrays.asList("R", " ", "G", " ", "B"));
              colors.removeAll(Arrays.asList(" "));
              System.out.println(colors);   
// OTHER part of program ..., file2 is expected results   

file1

ID  577 592 598 600 612 650 700 822 825 830 840 870
Line0   A           A                   A           A
Line1   B           B                   NA          B
Line2   B           A                   A           A


file2

ID  577 592 598 600 612 650 700 822 825 830 840 870
Line0   A   A   A   A   A   A   A   A   A   A   A   A
Line1   B   B   B   B   NA  NA  NA  NA  NA  NA  NA  B
Line2   B   NA  NA  A   A   A   A   A   A   A   A   A


java arraylist hashmap guava opencsv
1个回答
0
投票

也许行String[] values = line.split(",");错误,在您的输入文件中看不到任何,

© www.soinside.com 2019 - 2024. All rights reserved.