Using a For Loop to Loop Trough an ArrayΒΆ

You can also use a for loop to loop through all the elements of an array. Just start the index at 0 and loop until the index is equal to the length of the array.

public void multAll(int amt)
{
   for (int i = 0;
        i < amt.length;
        i++)
   {
      a[i] = a[i] * amt;
   } // end for loop
} // end method

Mixed up programs

7-3-1: The following method has the correct code to subtract amt from all the values in the array a (a field of the current object), but the code is mixed up. Drag the blocks from the left into the correct order on the right. You will be told if any of the blocks are in the wrong order or not indented correctly.