Unit 5 Lesson 5 Enhanced For Loop

Hi @kabbey! Thank you so much for bringing this to the forum! This is a topic that has gotten a lot of traffic recently and you can view this thread where they dive pretty deep. Long story short, yes, you can modify a 2D (and 1D array) with an enhanced for loop IF the array holds reference type variables. This has to do with how variables are stored in memory. Primitives and String variables cannot be modified with an enhanced loop but reference type can. Here is a sandbox link where I demonstrate this with 1D arrays.

I hope this helps and please reach out if you have any more questions.

Best,
-Sam

1 Like