Traversing Arrays


Remember when we were explaining the for loop, we said that it is married to arrays. Imagine you have an array with 1000 cell and we want to sum all the elements in that array. If you are crazy, you can add the elements manually from A[0] to A[999] but since we know you're not, we use a for loop.

Let's look at a program that stores muliples of two in an array.

public class Multiples
{
   public static void main(String args[])
   {
      int multiples[] = new int[11];

      for(int i = 0; i < 11; i++)
      {
         multiples[i] = 2*i;
      }

      System.out.println("The multiples of 2 up to 20 are:");

      for(int i = 0; i < 11; i++)
      {
         System.out.print(multiples[i]+" ");
      }
   }
}

Output:

The multiples of 2 up to 20 are:
0 2 4 6 8 10 12 14 16 18 20

The for loops in the previous program are used to store the multiples of two in the array and to print the array cells to the console.

Using .length

Instead of specifying for the for loop the number of elements in an array, we can use the .length operator. This operator returns the length of the array. Using this may help reduce errors if you write a larger numbers than the size of the array which will cause a runtime error.

For example, in the previous program, we could have written

for(int i = 0; i < multiples.length; i++)

instead of

for(int i = 0; i < 11; i++)

Now let's consider if we want to take the length of the array from the user. This is a simple procedure. Let's do this by writing a code that takes an integer from the user, call it n, and prints the nth Fibonacci number.

Let's explain what is a Fibonacci number first. The Fibonacci sequence is as follows

0,1,1,2,3,5,8,13,...

The first Fibonacci number is one, the second is also zero. Then, every number is the sum of the two numbers before it. Thus, the third Fibonacci number is 1 + 1 = 3. Now, let's get to coding.

import java.util.Scanner;

public class Fibonacci
{
   public static void main(String args[])
   {
      Scanner input = new Scanner(System.in);

      int n = input.nextInt();

      int fib[] = new int[n];

      fib[0] = 0;
      fib[1] = 1;

      for(int i = 2; i < fib.length; i++)
      {
         fib[i] = fib[i-1] + fib[i-2];
      }

      System.out.println("Fibnacci of "+n+" is "+fib[n-1]);
   }
}

Output:

5
Fibonacci of 5 is 3

Let's explain this code. After we created the array, we assigned the values of the first two Fibonacci numbers. Then, we used a for loop starting from two which assigns to fib[i] the sum of the previous two values. Therefore, in the end fib[n-1], which is the last element, will contain the Fibonacci number of n.

Congratulations

You now know how to traverse arrays using for loops whether to print or to manipulate values. Move on to the next tutorial but check the important notes first.

Important Notes

for and Array

We say this again, for and arrays are married so whenever you want to traverse an array, use a for loop.

for Counter

Always make sure that the counter of the for loop doesn't cross the array's length or you will have a runtime error.