Java - All Elements in List Become Identical -
i'm having trouble using loop assign elements list. here's section of code i'm having trouble with:
private static list<string[]> modify(list<string[]> data) { list<string[]> data2 = new arraylist<>(); (int i=0; i<data.size(); i++) { string[] block = data.get(i); // code here modify contents of block data2.add(block); } return data2; }
for reason, method returns list elements identical. i've tried outputting list elements see happening, , seems happening outside of loop. example, this:
private static list<string[]> modify(list<string[]> data) { list<string[]> data2 = new arraylist<>(); (int i=0; i<data.size(); i++) { string[] block = data.get(i); // code here modify contents of block data2.add(block); system.out.println(arrays.tostring(data2.get(i)); } return data2; }
displays list of different elements, whereas this:
private static list<string[]> modify(list<string[]> data) { list<string[]> data2 = new arraylist<>(); (int i=0; i<data.size(); i++) { string[] block = data.get(i); // code here modify contents of block data2.add(block); } (int i=0; i<data2.size(); i++) { system.out.println(arrays.tostring(data2.get(i))); return data2; }
displays list consisting of identical elements. seems me elements being correctly added list inside of loop, yet end becoming identical. why this, , how can fix it?
you should make temporary string manipulations, initialize string array, fill data , pass data2 this:
private static list<string[]> modify(list<string> data) { list<string[]> data2 = new arraylist<>(); (int i=0; i<data.size(); i++) { string tempstring = data.get(i); string[] block = new string[255]; // code here modify contents of block // fill block data block.add("modified data"); block.add("more manipulated data"); data2.add(block); } return data2; }
edit should fix no compiling problem!
Comments
Post a Comment