如何替换2D java arraylist中的空元素?
我打算用前导/尾随元素值替换如何替换2D java arraylist中的空元素?,java,arraylist,hashmap,guava,opencsv,Java,Arraylist,Hashmap,Guava,Opencsv,我打算用前导/尾随元素值替换ArrayList中的空元素(如果类似或不适用)。程序在运行的初始步骤中返回false,因此我无法继续实现它。我做错了什么?请为我建议一个合适的方法,谢谢 File file=new File("file1.csv"); Scanner inputStream; List<String> header = null; //Store the header in a separate l
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
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
可能行
String[]value=line.split(“,”)
是错误的,我在您的输入文件中没有看到任何,
,谢谢@ivan uqido的回复。实际加载的文件是一个csv文件…只是为了可读性而粘贴了分隔符。
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