Say I have the following piece of java code
ArrayList<Double> myList = new Double[100];
for (Double x : myList)
x = randomDouble();
Does this actually modify myList or just the dummy variable?
I realize I should just try this code segment out, but I think this is the sort of thing I should be able to google or search for on this site, and several queries so far have turned up nothing useful.
It doesn’t modify
myList. It works by callingmyList.iterator(), then (repeatedly)hasNext()andnext(), none of which changemyList.Also, Java does not have C++-style references. That means you don’t need to worry (even without looking at the API) about
xbeing a reference that could modifymyList.Finally, that’s invalid syntax. It should be: