Counting Loops and Arrays

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

Answer:

This sounds like a good place for a counting loop.

Counting Loops and Arrays

In Java, the index of an array starts at 0 and counts up to one less than the number of elements in the array. This is, of course, what counting loops do. Here is a program that does that, except for a blank or two:

class CountArray
{

  public static void main ( String[] args )
  {
    int[] egArray = { 2, 4, 6, 8, 10, 1, 3, 5, 7, 9 };

    for ( int index =  ;  ;  )
    {
      System.out.println(  );
    }
  }
}

The variable egArray refers to an array object that has been constructed using an initializer list.

QUESTION 2:

Fill in the blanks so that the program prints out every element, in order. You may wish to copy and paste from the following:

0
index++
egArray[index]
index < 10

(Highlight a string and then use use "copy" and "paste" commands from the "edit" menu of your browser to paste the string into a blank. Or use control-c and control-v as shortcuts for copy and paste.)