|
(For Array.java, Array2.java, Array3.java)
Structure of Java Arrays The Java array depicted above has 7 elements. Each element in an array holds a distinct value. In the figure, the number above each element shows that element's index. Elements are always referenced by their indices. The first element is always index 0. This is an important point, so I will repeat it! The first element is always index 0. Given this zero-based numbering,
the index of the last element in the array is always the array's length
minus one. So in the array pictured above, the last element would have
index 6 because 7 minus 1 equals 6.
Java Array Declaration An array variable is declared the same way that any Java variable is
declared. It has a type and a valid Java identifier. The type is the type
of the elements contained in the array. The [] notation is used to denote
that the variable is an array. Some examples:
Java Array Initialization Once an array variable has been declared, memory can be allocated to it. This is done with the new operator, which allocates memory for objects. (Remember arrays are implicit objects.) The new operator is followed by the type, and finally, the number of elements to allocate. The number of elements to allocate is placed within the [] operator.
Do you have a Java Problem?
Java Books
Return to : Java Programming Hints and Tips All the site contents are Copyright © www.erpgreat.com
and the content authors. All rights reserved.
|