In the code below
// Assume there are non-null string arrays arrayA and arrayB
// Code 1
ArrayList<String[]> al = new ArrayList<String[]>();
String[] tmpStr = new String[2];
for (int ii = 0; ii < arrayA.length; ii++) {
tmpStr[0] = arrayA[ii];
tmpStr[1] = arrayB[ii];
al.add(ii, tmpStr);
}
// Code 2
ArrayList<String[]> al = new ArrayList<String[]>();
for (int ii = 0; ii < arrayA.length; ii++) {
String[] tmpStr = new String[2];
tmpStr[0] = arrayA[ii];
tmpStr[1] = arrayB[ii];
al.add(ii, tmpStr);
}
Code 2 gives the wanted results -- that is, al now contains {arrayA(ii), arrayB(ii)} for each of its index. In code 1, however, al contains {arrayA(last_index), arrayB(last_index)} for all of its indices. Why is that?