Wednesday, December 1, 2010

How to use ArrayList in Java

How to use ArrayList in Java
Useful tips here, but it din mention the big frustration I have today when using ArrayList.
when reassigning the same array to an ArrayList, make sure to reinitialize the Array, or else all items inside the ArrayList will refer to the same array and u will have duplicates of the last Array

Object[] tempObj = new Object[3];
while ((s = br.readLine()) != null) {
if(c == (rowItems)){
c = 0;
tempObj = new Object[3];//REINITIALIZE HERE
}
//if (s != null && s.equals("")==false) {
tempObj[c] = s;
System.out.println("Line" + c + ": " + tempObj[c]);
//}
if (c == (rowItems - 1)) {
userData.add(tempObj);
System.out.println("Expected result: \'" + tempObj[0].toString() + "\', \'" + tempObj[1].toString() + "\', \'" + tempObj[2].toString() + "\'");
}
c++;
}

No comments:

Post a Comment