Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Here's a simple program, calledArrayDemo
, that creates the array, puts some values in it, and displays the values.This section covers these topics:public class ArrayDemo { public static void main(String[] args) { int[] anArray; // declare an array of integers anArray = new int[10]; // create an array of integers // assign a value to each array element and print for (int i = 0; i < anArray.length; i++) { anArray[i] = i; System.out.print(anArray[i] + " "); } System.out.println(); } }
This line of code from the sample program declares an array variable:Like declarations for variables of other types, an array declaration has two components: the array's type and the array's name. An array's type is writtenint[] anArray; // declare an array of integerstype[]
, wheretype
is the data type of the elements contained within the array, and[]
indicates that this is an array. Remember that all of the elements within an array are of the same type. The sample program usesint[]
, so the array calledanArray
will be used to hold integer data. Here are declarations for arrays that hold other types of data:As with declarations for variables of other types, the declaration for an array variable does not allocate any memory to contain the array elements. The sample program must assign a value tofloat[] anArrayOfFloats; boolean[] anArrayOfBooleans; Object[] anArrayOfObjects; String[] anArrayOfStrings;anArray
before the name refers to an array.
You create an array explicitly using Java'snew
operator. The next statement in the sample program allocates an array with enough memory for ten integer elements and assigns the array to the variableanArray
declared earlier.In general, when creating an array, you use theanArray = new int[10]; // create an array of integersnew
operator, plus the data type of the array elements, plus the number of elements desired enclosed within square brackets ('[' and ']').If thenew elementType[arraySize]new
statement were omitted from the sample program, the compiler would print an error like the following one and compilation would fail.ArrayDemo.java:4: Variable anArray may not have been initialized.
Now that some memory has been allocated for the array, the program assign values to the array elements:This part of the code shows that to reference an array element, either to assign a value to it, or to access the value, you append square brackets to the array name. The value between the square brackets indicates (either with a variable or some other expression) the index of the element to access. Note that in Java, array indices begin at 0 and end at the array length minus 1.for (int i = 0; i < anArray.length; i++) { anArray[i] = i; System.out.print(anArray[i] + " "); }
To get the size of an array, you writeBe careful: Programmers new to the Java programming language are tempted to followarrayname.lengthlength
with an empty set of parenthesis. This doesn't work becauselength
is not a method.length
is a property provided by the Java platform for all arrays.The
for
loop in our sample program iterates over each element ofanArray
, assigning values to its elements. Thefor
loop usesanArray.length
to determine when to terminate the loop.
The Java programming language provides a shortcut syntax for creating and initializing an array. Here's an example of this syntax:The length of the array is determined by the number of values provided between { and }.boolean[] answers = { true, false, true, true, false };
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.