๐Ÿ“š

All Subjects

ย >ย 

๐Ÿ’ปย 

AP Comp Sci A

ย >ย 

โŒš๏ธ

Unit 6

6.2 Traversing Arrays

2 min readโ€ขjanuary 29, 2021

peter-cao

Peter Cao

caroline49234295

Caroline Koffke


AP Computer Science Aย ๐Ÿ’ป

Bookmarkedย 2.4kย โ€ขย 93ย resources
See Units

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?

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
๐Ÿ“ฑ Stressed or struggling and need to talk to someone?
Talk to a trained counselor for free. It's 100% anonymous.
Text FIVEABLE to 741741 to get started.
ยฉ 2021 Fiveable, Inc.