Unit 5 Lesson 5 Enhanced For Loop

The anticipation guide for Unit 5 Lesson 5 states: It is possible to “modify the elements of a 2D array using an enhanced for loop”.
That question is marked True. (Most AP CSA resources say this is false and to use a regular for loop instead.)

In the Teaching Guide for that lesson, it says “When the array stores object references, this works a little differently.” This is in the Lesson Plan - Teaching Guide after “Activity (35 minutes)” and the last line in the Remarks.

The lesson slides show it to be True, which it is.

** My issue is that I don’t think “Modifying 2D arrays with an Enhanced For Loop” is part of the CED, so I told my kids not to worry about it. I cannot find it in other curriculums, but I only checked ProjectSTEM and CSAwesome.

Can someone confirm this please? Thank you.

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