In the Java system, arrays are first-class objects. That means that everything a program can do with any kind of object can be done with an array. Perhaps more importantly to the beginner, the subtleties about objects and references to them that give you problems also apply to arrays. We will also point out that arrays keep track of their own lengths.
Incidentally, the length of an array, which is specified in the new operation that allocated the array, cannot be changed afterward. It you want the array to become bigger or smaller later, exactly what you want is impossible in Java. The practical alternative is to allocate a bigger array, copy the contents of the too-small array into the beginning elements of the bigger array, and then make sure your program henceforth uses the new bigger array in place of the too-small array. It is educational to design and program your own class to demonstrate this idea. The already programmed and perfected classes Vector and ArrayList in the java.util package provide such so-called "extendable arrays." But they are less efficient than plain Java arrays if the resize feature is not needed.
First, a conclusion resulting from the facts explained in another chapter is that it is only useful to use array elements with the help of a variable that refers to the array. (You can have an "array of arrays". Then, the arrays in the array are still referred by variables because the elements in the array that contains the arrays are each variables. That is because every array element, no matter what its type, is a variable.)
Example:
int[] A = new int[7]; A[0] = 100; System.out.println(A[0]);
What is the alternative? What does the following code do?
(new int[7])[0] = 100;
First, the new operation makes the JVM allocate a new array of 7 int variables. By the Java rules, all those 7 ints are initialized to have value 0. Second, our code stores the value 100 into the first int in that array, the one located at index 0. This is legal Java code (try to compile it) but it is completely useless, except perhaps to use computer time and make a temporary demand for a small amount of memory. You might want to put code like this into a long running loop to investigate how fast or slow is your combination of computer and version of the Java system. It is otherwise completely useless because the is no way for additional code to access that 7 element array of ints! The data (six zeros and one one) is in the computer memory, but there is no way in the Java language for you to program any more changes to it or any other uses.
The easiest way to understand exactly what is in the computer memory as a result of the following code is the data structure diagram drawn below.
Example code:
int[] A = new int[7]; A[0] = 100;
(Data structure diagram showing (1) a box labeled A, (2) a box containing two boxes and (3) a row of 7 boxes, the first containing 100 and the rest containing zero. The box labelled A contains the tail of an arrow whose head touches the second box. One box within the second contains the tail of an arrow whose head touches the first box in the row. The other box within the second contains a 7.)
There has been error in communication with booki server. Not sure right now where is the problem.
You should refresh this page.