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 LoopInitSumSquares.java. |
import javax.swing.*; public class LoopInitSumSquares {
} |
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
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.
Our next webpage will present issues related to passing arrays and specific elements in arrays to methods. |