I am adding string[] into Arraylist, But when i try to retrieve value from it, something wrong happening. and previous values of arraylist getting updated with new entry(String[])
Expected Output :
first , sec
1, 2
one, two
Current Output :
one, two
one, two
one, two
below is the code.
ArrayList<String[]> list = new ArrayList<String[]>();
String[] temp ={"First","Sec"};
list.add(temp);
temp[0] ="1";temp[1]="2";
list.add(temp);
temp[0] ="one";temp[1]="two";
list.add(temp);
printLog(list);
here is the print method
private void printLog(ArrayList<String[]> result) {
for (int i = 0; i < result.size(); i++) {
Log.d(TAG,"I="+i);
String[] temp = result.get(i);
Log.d(TAG,temp[0]+","+temp[1]+"\n");
}
}
Any suggessions.
You are adding reference of an array object to the list. You need to create new array each time.