Array tab Syntax Buttons
An array must be
declared. When you declare an array variable, you suffix the
type with [ ]
to indicate that this variable is an array. This states the type of value the
array holds. Each [] represents one dimension of the array. So the array
int[][]
represents a 2
dimensional array of integers where the components of the first dimension are
of type
int[]
and the components
of these components are of type
int
The variable name of the array appears in an array declaration followed by a semicolon. Here are some examples:
int[ ] x;
float[ ] nt;
String[ ] names;
Use the new keyword to create an array. For example:
c = new int[3];
In the preceding example, c is the array variable. The number in the brackets specifies the number of components in the array, which is called the length of the array. This allocates memory for the array.
Button |
Description |
---|---|
new ?[] |
Creates a
new array. For example: ArrayVariableName = new string[]
For example: new int[3] creates an array of size 3 but initializes all components to their default value. For a list of variable default values based on type, see Initial Values of Variables. |
? [?] |
Enters an array component by array variable and index. For example: ArrayVariableName[array_index] |
?.length |
Enters an array length variable. For example: ArrayVariableName.length The length is the number of components in the array. |
?.iterator |
Enters an array iterator variable. For example: ArrayVariableName.iterator The Iterator is a class based on the Array. It provides methods to go through all the Array components, one at a time. |
Note | The .length and the .iterator variables do not require the Java license. |