ERROR ON PREV
| ArraysPreviously we introduced variables, which we said were like boxes designed to hold various types of data. The array is like a shelf, designed to hold a finite set of boxes of the same type. Think of the shelves in the array as being numbered. The shelf closest to the ground is shelf 0, the next one up is shelf 1, and so on. The numeric label for the shelf is called the "index."
The ten shelves in the array we created were numbered 0 to 9, so attempting to put something on shelf 10 created a "runtime exception." When this error occurred the java program stopped running and we were told that we attempted to index element 10 of our shelf, and that this is not a valid index. We were also told what line number of our program caused the error. Very helpful! You may notice that every program we've written so far has an array of Strings at the top. These strings give you access to the command line arguments.
The above example is run three times. The first time, no command line arguments are supplied. In this example, we see the length of the array is zero and we attempted to access element zero. This error is on line four. We can fix that problem simply by supplying a command line argument. The second attempt at running the program prints the length of "args" (which is now 1 and not 0 since the command line argument "hello" was supplied. Now the code dies on line 5. The third time is the charm. We run the program with "hello" and "world" and it succeeds.
|