I am having a problem with adding new elements to an ArrayList
, I will show you my problem :
ArrayList<ArrayList<String>> arr1= new ArrayList<ArrayList<String>>();
ArrayList<String> arr2= new ArrayList<String>();
So I am adding new elements from a text file , spliting it line by line , then word by word , example : we suppose my text file contains :
a,b,c,d
e,f,g,h
i,j,k,l
What I need is to put each word in arr2 , then i put arr2 into arr1 with the following code :
while ((line = file.readLine()) != null) {
String[] words = line.split("\\,");
if (arr2.isEmpty()) {
for (int ae = 0; ae < words.length; ae++) {
arr2.add(ae, words[ae]);
}
arr1.add(arr2);
} else {
for (int ae = 0; ae < words.length; ae++) {
arr2.set(ae, words[ae]);
}
arr1.add(arr2);
}
}
file.close();
So what I need is :[[a,b,c,d],[e,f,g,h],[i,j,k,l]]
but I get : [[i,j,k,l],[i,j,k,l],[i,j,k,l]]
Any solution please?!