Java: writing a list of deep objects without creating a new list

I have a little problem while doing my programming task. I want to read some lines from a file (no problem so far) and do tokenization. Each line contains about 4 tokens, and each must find a place in the list. After all, each line must also be on the list.

A small example to clarify this:

File content:

foo boo bar
bii buu baa

output:

[[foo, boo, bar], [bii, buu, baa]]

And heres the code I am dealing with

String fileContent = fileloader(file.toString());
List<String> linesList = new ArrayList<String>();
String[] lines = fileContent.split("\n"); 
for(String line:lines){
  String[] splittedLine = line.split("\t");
  for(String words:splittedLine){
    linesList.add(words);
  }
  lexiconContent.add(linesList); 
  linesList.removeAll(linesList);
}

      

I'm guessing it's a link issue because the first iteration works well! But in the second iteration, it copies the second actual content also to the first position of the list (0)

, etc.

Finally I got something like [[], [], [], []]

+3


source to share


2 answers


The problem is that you only created one list and added a link to that list to your external list, changing it on each iteration. So, the final modification of this list will be reflected for all links.

You can solve this problem by creating a new linesList

one every time in the loop: -

List<String> linesList = null;  // Don't initialize here
String[] lines = fileContent.split("\n"); 

for(String line:lines){
    String[] splittedLine = line.split("\t");
    linesList = new ArrayList<String>(); // Initialize a new list everytime.

    for(String words:splittedLine){
        linesList.add(words);
    }
    lexiconContent.add(linesList); 
}

      



And yes, you can also simplify the for loop: -

for(String line:lines){
    String[] splittedLine = line.split("\t");
    lexiconContent.add(new ArrayList<String>(Arrays.asList(splittedLine))); 
}

      

This way, you don't have to iterate over the array and add individual elements to your list. You don't really need an intermediate list at all.

+3


source


In your code .. Instead of creating a new ArrayList for each iteration in the loop: for(String line:lines)

You simply add words (in a nested loop) to the old object ArrayList

you used right from the first iteration of the outer loop and keep that same reference value throughout the subsequent lexiconContent

ArrayList index . Also at the end of each iteration of the outer loop, you clean up linesList

. So you are left with N number of records in lexiconContent

... If the element in each index of lexiconContent is nothing but the reference value of one ArrayList (LineList) object, which is empty !!!



You should use following code instead:
String fileContent = fileloader(file.toString());
List<String> linesList = null;
String[] lines = fileContent.split("\n"); 
for(String line:lines){
  List<String> linesList = new ArrayList<String>();
  String[] splittedLine = line.split("\t");
  for(String words:splittedLine){
    linesList.add(words);
  }
  lexiconContent.add(linesList); 
}

      

0


source







All Articles