The AP Computer Science A 5-hour live stream review is here!Β πŸ’»

Join us on May 5, 2021 for the 🌢️ AP Computer Science A Cram Finale for a last minute review to get all your questions answered!

πŸ“š

All Subjects

Β >Β 

πŸ’»Β 

AP Comp Sci A

Β >Β 

⌚️

Unit 6

6.2 Traversing Arrays

2 min readβ€’january 29, 2021

Peter Cao

Caroline Koffke


What is Traversing?

TraversingΒ an array means to access every value in the array. To do this, we need to use a loop, and we most often use a for loop. To do so, we use the following:
for (int i = 0; i < array.length; i++) { do something with array[i] }
Here is an example where we use a constructor to make a copy of arrayOne from the previous section:
int[] arrayTwo = new int[10] { for (int i = 0; i < array.length; i++) { arrayTwo[i] = i + 1; } }
Using regular for loops, we can either access array elements or manipulate them as above.
Sometimes, we want to go in reverse, from the end of the array to the beginning. This requires a change to the for loop condition to the following:
for (int i = array.length - 1; i >= 0; i--) { do something with array[i] }
Other times we don't want to traverse through all elements in the array, but only starting from the second element to the end. From this we start at index i = 1 and end at i = array.length - 1. Perhaps we want to traverse only through the first n elements (assume n is smaller than array.length - 1). For this, we start at index i = 0 and end at i = n - 1. Using this, we can combine these two to traverse through any subsection of this array.
Using this for loop traversal, we can modify every value in the array. For example, this is a method that doubles every element in the array.
/** Doubles each element of the array */ public static void doubleArray(int[] array) { for (int i = 0; i < array.length; i++) { array[i] *= 2; // doubles each individual element } }
This can be read as "for all indices i between 0 to array.length - 1, we find the array element at index i and double it." This is the same as what is done with other for loop traversals of this type.

Was this guide helpful?

πŸ’ͺ🏽 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
Hours Logo
Studying with Hours = the ultimate focus mode
Start a free study session
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
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 7: ArrayList
πŸ’»
Unit 8: 2D Array
πŸ–²
Unit 9: Inheritance
Join us on Discord
Thousands of students are studying with us for the AP Computer Science A exam.
join now
πŸ“± Stressed or struggling and need to talk to someone?
Talk to a trained counselor for free. It's 100% anonymous.
Text FIVEABLE to 741741
Β© 2021 Fiveable, Inc.