I have a CSV file of neighboring regions. I have read the file with java and created 2D array of Strings. the data that I have looks something like this:
100 , 101
100, 102
100, 152
200, 201
200, 202
This data means that region 100 is neighbors with 101,102,152. I want to create a new 2D array where the fist element is the "key" and the following elements are the neighboring regions. something like this:
100 101 102 152
200 201 202
I used an array of strings because some of the values are not all integers.
Here is what I was trying:
List<List<String>> outputLines = new ArrayList<>();
List<String> inner = new ArrayList<>();
for(int i =0; i<lines.size();i++){
if(inner.isEmpty()){
inner.add(array[i][0]);
inner.add(array[i][1]);
}
else if(inner.get(0).equals(array[i][0])){
inner.add(array[i][1]);
}
else{
outputLines.add(inner);
inner.clear();
inner.add(array[i][0]);
inner.add(array[i][1]);
}`
My issue is that when I clear the inner list it so I can start populating the list with the new one it also deletes the list that I passed to the output list. I can't figure out why.
outputLineslist unchanged, then you will need tocloneitinner = new ArrayList<>();instead ofinner.clear().Map<T, List<T>>instead ofList<List<T>>. A map is more suited for this problem