ERROR ON PREV
Arrays
  1. "Hello, World!"
  2. Variables and Types
  3. Arrays
  4. While, If, For
  5. ...Problem Set 0
  6. Static Methods
  7. Static Fields
  8. String Conversion
  9. Objects
  10. Threading
  11. Strings
  12. ...Problem Set 1.5
  13. Packages
  14. Complex Numbers
  15. Abstract classes
  16. Interfaces
  17. Autoboxing
  18. ...Problem Set 1
  19. enum
  20. Inner Classes
  21. Polymorphism
  22. Tanks!
  23. Callbacks
  24. Exceptions
  25. File I/O
  26. ...Problem Set 2
  27. Regular Expressions

Arrays

Previously 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."

Array1.java
1public class Array1 {
2    public static void main(String[] args) {
3        int[] shelf = new int[10]; // 10 shelves numbered 0 to 9
4        shelf[0] = 3; // put 3 on shelf 0
5        shelf[5] = 2; // put 2 on shelf 5
6        System.out.println("shelf 5 has: "+shelf[5]);
7        shelf[10] = 9; // uh, oh....
8    }
9}
$ javac Array1.java
$ java Array1
shelf 5 has: 2
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
	at Array1.main(Array1.java:7)

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.

Array2.java
1public class Array2 {
2    public static void main(String[] args) {
3        System.out.println("length is: "+args.length);
4        System.out.println("the first arg: "+args[0]);
5        System.out.println("the second arg: "+args[1]);
6    }
7}
$ javac Array2.java
$ java Array2
length is: 0
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
	at Array2.main(Array2.java:4)
$ java Array2 hello
length is: 1
the first arg: hello
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
	at Array2.main(Array2.java:5)
$ java Array2 hello world
length is: 2
the first arg: hello
the second arg: world

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.