Initializing Arrays by a List

 

Introduction.  As you well know, syntax is a tyranny in computer programming.  It won't be any different when dealing with arrays. How does a developer get the entries into an array initially?  This can be a major problem, particularly as arrays grow larger.

I will work examples to demonstrate some different approaches to declaring, initializing and working with arrays.  I'm of the opinion that most people learn more from examples.

But in general you should keep in mind that when working with arrays, it is very common to make use of loops to move through and access its elements.

Our first example puts a bunch of integers into an array using a list.  Then it operates on these numbers.  It also displays the initial values in the array and the result of the operations.

This first program we will work with is really quite artificial, but it illustrates some important points.  You should call this Java application ListInitSum.java.

 

import javax.swing.*;

public class ListInitSum
{

public static void main( String args[])
{

String output = "";
int total = 0;

// Using an initializing list to specify the number of elements and values in an array
int a[] = { 1, 100, 15, 16, 115, 9, 49, 71, 31, 100};

// putting a header row on the output
// use control characters for spacing and lines

output = output + "Subscript\tValue\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++)
{

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"+ "Total" + "\t" + total;

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

// displaying the JTextArea in a showMessageDialog
JOptionPane.showMessageDialog(null, outputArea,
"Using a List to Initialize and Array",
JOptionPane.INFORMATION_MESSAGE);

System.exit(0);

}

}

 

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

 

 

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

a[0] = 1
a[1] = 100
a[2] = 15

...

a[9] = 100

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 ListInitSum
  • declare method main( )
    • declare and initialize a string for output
    • declare and initialize a integer to total elements in the array
    • declare and initialize an array of integers
      • the array is called a
      • square brackets are used to indicate it is an array
      • the array is assigned a bunch of numbers listed out between braces delimited by commas
      • notice that there doesn't need to be a numbering scheme
    • 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
      • 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( )

There isn't a lot more to say at this point.

Our next webpage will make use of a loop to initialize and work with the array.