I'm trying to read CSV file like this:
1,0,0,0,2,0,0,2,
0,2,0,0,0,0,0,5,
0,0,0,0,0,0,5,0,
This is my expected outcome:
[1, 5, 8]
[2, 8,]
[7]
This is my Java coding:
CSVReader a = new CSVReader(new FileReader("CM.csv"));
List<String[]> aa = a.readAll();
List<Integer> list = new ArrayList<>();
Object[] CM = new Object[3];
for (int i = 0; i < aa.size(); i++) {
for (int x = 0; x < aa.get(i).length-1; x++) {
if ( Integer.parseInt(aa.get(i)[x].trim()) >= 1 ){
list.add(x+1);
}
}
CM[i] = list;
list.clear();
}
for (int i = 0; i < CM.length; i++) {
System.out.print(CM[i]);
System.out.print("\n");
}
But I get Null outcome. If I delete the list.clear() line then get weird outcome like this :
[1, 5, 8, 2, 8, 7]
[1, 5, 8, 2, 8, 7]
[1, 5, 8, 2, 8, 7]
I want to store the list into the array, please ignore the way how i read the CSV file...