Fiveable
Fiveable
pep
Fiveable
Fiveable

or

Log in

Find what you need to study


Light

6.2 Traversing Arrays

3 min readdecember 29, 2022

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

What is 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.

Forward Traversal

To do so, we use the following:

for (int i = 0; i < 

Here is an example where we use a to make a copy of arrayOne from the previous section:

int[] arrayTwo = new int[10] {
  for (int i = 0; i < 

Using regular for loops, we can either access array elements or manipulate them as above.

Reverse Traversal

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 = 

Limited Traversal

Different Start Index

Other times we don't want to traverse through all elements in the array, but only starting from the second element to the end. For this we start at index i = 1 and end at i = - 1. Our for loop would be written as:

for (int i = 1; i < 

Different End Index

Perhaps we want to traverse only through the first n elements (assume n is smaller than - 1). For this, we start at index i = 0 and end at i = n - 1. Our for loop would be written as:

for (int i = 0; i < n; i++) {
  do something with array[i]
}

Subsection

We can combine the two concepts above to traverse through any of this array. For example, if we only want to go from the third to seventh element of array, inclusive, we could start at index i = 2 and end at i = 6. Our for loop would be written as:

for (int i = 2; i < 7; i++) {
  do something with array[i]
}

Examples

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 < 

This can be read as "for all indices i between 0 to - 1, we find the array element at index i and double it."

It is less common, but while loops can also be used to traverse an array. For example, we can rewrite the method above using a :

/** Doubles each element of the array
*/
public static void doubleArray(int[] array) {
  int i = 0;
  while (i < 

As you can see, the takes more lines to write and it's not as easy to just look at one line and figure out how many times the loop will occur. That's one of the reasons why for loops are more common for array traversals.

When arrays, it's easy to mess up the index by 1. If that happens and you try to access an index that does not exist (for example, index 10 in an array that only has indices 0 to 9), you'll get an .

Key Terms to Review (8)

array.length

: The property "length" in an array refers to the number of elements it contains.

ArrayIndexOutOfBoundsException

: ArrayIndexOutOfBoundsException is an exception that occurs when trying to access an invalid index position in an array. It is thrown to indicate that the index used to access an array is either negative or greater than or equal to the size of the array.

Constructor

: A constructor is a special method within a class that is used to initialize objects of that class. It is called automatically when an object is created and helps set initial values for its attributes.

Limited Traversal

: Limited traversal refers to the process of accessing only a portion of a data structure, such as an array or list, instead of traversing through the entire structure. It allows for efficient retrieval and manipulation of specific elements within the data structure.

Reverse Traversal

: Reverse traversal involves accessing elements in reverse order, starting from the last element and moving towards the first element in a collection or data structure.

Subsection

: A subsection is a smaller part or division of a larger section. In programming, it refers to breaking down a complex problem into smaller, more manageable parts.

Traversing

: Traversing refers to the process of accessing each element in a data structure, such as an array or linked list, and performing some operation on it.

while loop

: A while loop is a control flow statement that allows a block of code to be executed repeatedly as long as a specified condition is true.

6.2 Traversing Arrays

3 min readdecember 29, 2022

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

What is 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.

Forward Traversal

To do so, we use the following:

for (int i = 0; i < 

Here is an example where we use a to make a copy of arrayOne from the previous section:

int[] arrayTwo = new int[10] {
  for (int i = 0; i < 

Using regular for loops, we can either access array elements or manipulate them as above.

Reverse Traversal

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 = 

Limited Traversal

Different Start Index

Other times we don't want to traverse through all elements in the array, but only starting from the second element to the end. For this we start at index i = 1 and end at i = - 1. Our for loop would be written as:

for (int i = 1; i < 

Different End Index

Perhaps we want to traverse only through the first n elements (assume n is smaller than - 1). For this, we start at index i = 0 and end at i = n - 1. Our for loop would be written as:

for (int i = 0; i < n; i++) {
  do something with array[i]
}

Subsection

We can combine the two concepts above to traverse through any of this array. For example, if we only want to go from the third to seventh element of array, inclusive, we could start at index i = 2 and end at i = 6. Our for loop would be written as:

for (int i = 2; i < 7; i++) {
  do something with array[i]
}

Examples

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 < 

This can be read as "for all indices i between 0 to - 1, we find the array element at index i and double it."

It is less common, but while loops can also be used to traverse an array. For example, we can rewrite the method above using a :

/** Doubles each element of the array
*/
public static void doubleArray(int[] array) {
  int i = 0;
  while (i < 

As you can see, the takes more lines to write and it's not as easy to just look at one line and figure out how many times the loop will occur. That's one of the reasons why for loops are more common for array traversals.

When arrays, it's easy to mess up the index by 1. If that happens and you try to access an index that does not exist (for example, index 10 in an array that only has indices 0 to 9), you'll get an .

Key Terms to Review (8)

array.length

: The property "length" in an array refers to the number of elements it contains.

ArrayIndexOutOfBoundsException

: ArrayIndexOutOfBoundsException is an exception that occurs when trying to access an invalid index position in an array. It is thrown to indicate that the index used to access an array is either negative or greater than or equal to the size of the array.

Constructor

: A constructor is a special method within a class that is used to initialize objects of that class. It is called automatically when an object is created and helps set initial values for its attributes.

Limited Traversal

: Limited traversal refers to the process of accessing only a portion of a data structure, such as an array or list, instead of traversing through the entire structure. It allows for efficient retrieval and manipulation of specific elements within the data structure.

Reverse Traversal

: Reverse traversal involves accessing elements in reverse order, starting from the last element and moving towards the first element in a collection or data structure.

Subsection

: A subsection is a smaller part or division of a larger section. In programming, it refers to breaking down a complex problem into smaller, more manageable parts.

Traversing

: Traversing refers to the process of accessing each element in a data structure, such as an array or linked list, and performing some operation on it.

while loop

: A while loop is a control flow statement that allows a block of code to be executed repeatedly as long as a specified condition is true.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.