Initializing Arrays with a Loop


Introduction.  Now we will work a somewhat similar sort of problem, but we will initialize the elements in the array with a loop.  We will also allow the user to determine how large the array needs to be at runtime and not before.

This program we will work with is somewhat less artificial than the one in the last webpage, but it illustrates some important points.  You should call this Java application


import javax.swing.*;

public class LoopInitSumSquares

public static void main( String args[])

String output = "";
int total = 0;
String strUpperLimit;
int upperLimit;

strUpperLimit = JOptionPane.showInputDialog("Please enter the number of terms");
upperLimit = Integer.parseInt(strUpperLimit);

// declare reference to an array
int a[];

// dynamically allocate array
a = new int[upperLimit];

output = output + "Subscript\tSquare\n";

// using a loop to move through the array
// displaying each element with its index
// finding the total of all the numbers

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

// assign each i-th entry of the array
// to i-square for initialization

a[i] = i*i;
output = output + i + "\t" + a[i] + "\n";
total = total + a[i];


// adding the total to the output display
// with appropriate alignment

output = output + "\n\n"+ "Sum" + "\t" + total;

// putting the output into a JTextArea
JTextArea outputArea = new JTextArea(15,10);

// displaying the JTextArea in a showMessageDialog
JOptionPane.showMessageDialog(null, outputArea,
"Using a Loop to Initialize an Array",





Before we discuss this you should run it and get the output in the following image if you enter 13.



Notice how each element is next to its identifier within the array and the sum is at the bottom.  Thus

a[0] = 0
a[1] = 1
a[2] = 4
a[3] = 9


a[12] = 144

You should also notice that the subscript/index never repeats, but particular values can appear more than once.

Code Discussion.  We will use our usual outline form to discuss the code.

  • import javax.swing for output
  • declare the overall inclusive class LoopInitSumSqaures
  • declare method main( )
    • declare and initialize a string for output
    • declare and initialize an integer to total elements in the array
    • declare a string to get user input
    • declare an integer upperLimit to be used as the size of the array
    • obtain the user input
    • parse the input to an integer
    • declare and initialize an array of integers
      • the array is called a
      • square brackets are used to indicate it is an array
    • dynamically allocate memory for the array to wait for user input
    • a header is set up for the output using tabbing and a new line
    • a loop is used to go through the array
      • notice it starts at 0
      • notice its upper limit is indicated by  < a.length  so that in order to go through the entire array
        • you do not need to know before hand the number of elements in the array
        • because the index starts at zero the last entry is actually at one less than the number of elements in the array
        • the .length  is essentially the same operator used for string lengths
      • the square of the index = i2 is assigned to element a[i]
      • each element in the array is added to the output string one tab over from its index
      • an overall total is computed
    • then the total is put onto the output string in its own line with appropriate tabbed alignment
    • a JTextArea is declared and initialized
    • the contents of this JTextArea are set to the output string
    • this JTextArea is displayed using the showMessageDialog( ) method of the JOptionPane class
  • System.exit(0) causes the program to terminate when the user click on the OK button in the showMessageDialog( )

Our next webpage will present issues related to passing arrays and specific elements in arrays to methods.