I have a loop in which at the end of the loop a String[] is added to an ArrayList(which is declared in the class not a method) and at the beginning of the loop said String[] is cleared of its contents:
String[] a = new String[2];
while(true){
a[0] = "";
a[1] = "";
-----some code----
(that adds to a[0] and a[1])
-----some code----
//lets call our ArrayList list
list.add(a);
}
so more often than not, what is stored on the list is an empty String. I think it is because java moves on to the next step too fast but I don't know for sure, any help please?
Here is all of my code:
static ArrayList<String[]> Names = new ArrayList<String[]>();
public static void read(BufferedReader stream){
String[] aux = new String[2];
char it = 2;
try{
while(it != (char) -1){
aux[0] = "";
aux[1] = "";
it = (char) stream.read();
while(Character.isLetter(it)){
aux[0] += it;
it = (char) stream.read();
}
it = (char) stream.read();
while(Character.isDigit(it)){
aux[1] += it;
it = (char) stream.read();
}
Names.add(aux);
stream.read();
}
}catch(IOException e){
System.out.print("IOException(read): ");
System.err.println(e.getMessage());
}
}
When you add the array referenced by a (or aux in the second example) to your list, The variable a still refers to the string array. When you re-initialize the string array elements to empty strings you also wipe the entries in the list because they are the same data structure. You have multiple references to the same array.
You need to create a new array for each pass through the loop so that the list elements will actually contain separate arrays. Move the line initializing the array
String[] a = new String[2];
to inside the while loop. That way the array will get reallocated so that the local variable won't be pointing to the same array you previously added to the arrayList.
Here's a small test program that reproduces the problem:
import java.util.*;
public class DupeArr {
public void testBad() {
System.out.println("bad, multiple references to same array");
List<String[]> list = new ArrayList<String[]>();
String[] arr = {"a", "b"};
for (int i = 0; i < 2; i++) {
arr[0] = "" + i;
arr[1] = "" + (i * 10);
list.add(arr);
}
System.out.println(list.get(0)[0]);
System.out.println(list.get(0)[1]);
System.out.println(list.get(1)[0]);
System.out.println(list.get(1)[1]);
System.out.println(list.get(0)[0].equals(list.get(1)[0]));
System.out.println(list.get(0)[1].equals(list.get(1)[1]));
// printing true means these lists point to the same array
System.out.println("same reference=" + (list.get(0) == list.get(1)));
}
public void testGood() {
System.out.println("good, new array for each list item");
List<String[]> list = new ArrayList<String[]>();
for (int i = 0; i < 2; i++) {
String[] arr = {"a", "b"};
arr[0] = "" + i;
arr[1] = "" + (i * 10);
list.add(arr);
}
System.out.println(list.get(0)[0]);
System.out.println(list.get(0)[1]);
System.out.println(list.get(1)[0]);
System.out.println(list.get(1)[1]);
System.out.println(list.get(0)[0].equals(list.get(1)[0]));
System.out.println(list.get(0)[1].equals(list.get(1)[1]));
// printing false means these lists point to different arrays
System.out.println("same reference=" + (list.get(0) == list.get(1)));
}
public static void main(String[] args) {
DupeArr dupeArr = new DupeArr();
dupeArr.testBad();
dupeArr.testGood();
}
}
The output for this is
bad, multiple references to same array
1
10
1
10
true
true
same reference=true
good, new array for each list item
0
0
1
10
false
false
same reference=false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With