How does Java handle ArrayList refrerences and assignments?
- by Jonathan
Hey all-
I mostly write in C but am using Java for this project. I want to know what Java is doing here under the hood.
ArrayList<Integer> prevRow, currRow;
currRow = new ArrayList<Integer>();
for(i =0; i < numRows; i++){
prevRow = currRow;
currRow.clear();
currRow.addAll(aBunchOfItems);
}
Is the prevRow = currRow line copying the list or does prevRow now point to the same list as currRow? If prevRow points to the same list as currRow, I should create a new ArrayList instead of clearing....
private ArrayList<Integer> someFunction(ArrayList<Integer> l){
Collections.sort(l);
return l;
}
main(){
ArrayList<Integer> list = new ArrayList<Integer>(Integer(3), Integer(2), Integer(1));
list = someFunction(list); //Option 1
someFunction(list); //Option 2
}
In a similar question, do Option 1 and Option 2 do the same thing in the above code?
Thanks-
Jonathan