Java Collections & aliasing -


How do you deal with aliasing in Java? One simple solution is to make a copy of what we call an ArrayList, but when I try to write the code, my data is overwritten with the newly added data. In detail: Arreelist & lt; ArrayList & gt; Temp = new arreelist & lt; ArrayList & gt; (); ArrayList & LT; ArrayList & gt; PersonRecord = New Arrestists & lt; ArrayList & gt; (); ArrayList & LT; String & gt; PersonDetail = New Arrestist & lt; String & gt; (); ... while (input.hasNextLine ()) {string line = input.nxtain (); String [] token = line split ("", 0); For (string s: token) {personDetail.add (s); } Temp.add (personDetail); PersonRecord.addAll (temp); Temp.clear (); PersonDetail.clear (); } [[Peter, M, 1972], [Peter, M., 1972]] Instead of / pre>

:

  [[Peter, M, 1972], [Anne, F., 1974]]    

You should get inside a person to get a new array list for each person:

  ArrayList & lt; ArrayList & gt; PersonRecord = New Arrestists & lt; ArrayList & lt; String & gt; & Gt; (); While (Input hasNextLine ()) {ArrayList & lt; String & gt; = New Arrestist & lt; String & gt; (); String line = input NXtine (); String [] token = line split ("", 0); For (string s: token) {personDetail.add (s); } PersonRecord.add (personDetail); }   

temp is not necessary in any case.

Comments