Does foreach construct java hard or soft copy?

Let's say I have the following Java code snippet

ArrayList<Double> myList = new Double[100];
for (Double x : myList)
    x = randomDouble();

Does this really change myList or just a dummy variable?

I understand that I should just try this segment of code, but I think that this is what I should be able to Google or search on this site, and a few requests still have not brought anything useful.

+3
source share
3 answers

He does not change myList. It works by calling myList.iterator(), then (repeatedly) hasNext()and next(), none of which will change myList.

, Java ++. , ( API) x , myList.

, . :

ArrayList<Double> myList = new ArrayList<Double>(); 
/* or new ArrayList<Double>(100), but that only an optimization 
(initial capacity), not the size. */
+9

myList ?

.

, .

:

ArrayList<Double> myList = new ArrayList<Double>(100);

Double[] myList = new Double[100];
+2

, , , , :

"=", x - myList. myList, foreach, . :

for (MyDouble x : myList) {
    x.setX( randomDouble() );
}
+2

Source: https://habr.com/ru/post/1749125/


All Articles