📚

All Subjects

 > 

💻 

AP Comp Sci A

 > 

⌚️

Unit 6

6.3 Enhanced For Loop For Arrays

2 min readnovember 16, 2020

peter-cao

Peter Cao

caroline49234295

Caroline Koffke


AP Computer Science A 💻

Bookmarked 2.4k • 93 resources
See Units

Now, we will introduce a special kind of for loop called the enhanced for loop. The enhanced for loop can be used to traverse through most data structures. Note that the enhanced for loop can only be used to traverse in the forward direction. To use it, we use this structure:

Structure for Enhanced 'For Loops'

for (dataType i: arrayName) { do something with i }
This basically says that for every element i in the array, we do something to i. We can only access i, but we cannot change or set new values to i. Thus, we cannot do this:
/** Doubles each element of the array */ public static void doubleArray(int[] array) { for (int i: array) { i *= 2; // doubles each individual element } }
Why? Remember that Java is a pass-by-value programming language. The variable i is only a copy of the actual element in the array and changing i only changes the copy, not the actual element itself. If you want to actually change the array elements, use regular for loops as in 6.2.
However, we can use mutator methods on objects on the array to set the value of their instance variables. This is because iis a copy of the object reference, which means that i refers to the same object as the array element, so calling methods on i is the same as calling methods on the individual array elements themselves. Here is an example of how we can do so:
/** Represents a student */ public class Student { private String name; /** Sets the name of the Student */ public void setName(String name) { this.name = name; } /** Other instance variables, methods, and constructors not shown */ } // IN ANOTHER CLASS /** Resets all students' names */ public static void doubleArray(Student[] array, String defaultName) { for (Student student: array) { student.setName(defaultName); // Sets each student's name to a default name } }

Was this guide helpful?

Join us on Discord
Thousands of students are studying with us for the AP Computer Science A exam.
join now
Hours Logo
Studying with Hours = the ultimate focus mode
Start a free study session
🔍 Are you ready for college apps?
Take this quiz and find out!
Start Quiz
Browse Study Guides By Unit
🙏Exam Reviews
🖱Unit 10: Recursion
Unit 1: Primitive Types
📱Unit 2: Using Objects
🖥Unit 3: Boolean Expressions and if Statements
🕹Unit 4: Iteration
⚙️Unit 5: Writing Classes
⌚️Unit 6: Array
💾Unit 7: ArrayList
💻Unit 8: 2D Array
🖲Unit 9: Inheritance
FREE AP comp sci a Survival Pack + Cram Chart PDF
Sign up now for instant access to 2 amazing downloads to help you get a 5
Join us on Discord
Thousands of students are studying with us for the AP Computer Science A exam.
join now
💪🏽 Are you ready for the Comp Sci exam?
Take this quiz for a progress check on what you’ve learned this year and get a personalized study plan to grab that 5!
START QUIZ