Initializing Arrays by a List
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.
public class ListInitSum
|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
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.
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.