Name |
Array |
Examples |
int[] numbers = new int[3];
numbers[0] = 90;
numbers[1] = 150;
numbers[2] = 30;
int a = numbers[0] + numbers[1]; // Sets variable a to 240
int b = numbers[1] + numbers[2]; // Sets variable b to 180
int[] numbers = { 90, 150, 30 };
int a = numbers[0] + numbers[1]; // Sets variable a to 240
int b = numbers[1] + numbers[2]; // Sets variable b to 180
int degrees = 360;
float[] cos_vals = new float[degrees];
for(int i=0; i < degrees; i++) {
cos_vals[i] = cos(TWO_PI/degrees * i);
} |
Description |
An array is a list of data. It is possible to have an array of any type of data. Each piece of data in an array is identified by an index number representing its position in the array. The first element in the array is [0], the second element is [1], and so on. Arrays are similar to objects, so they must be created with the keyword new. Every array has a variable length which is an integer value for the total number of elements in the array. (People are often confused by the use of length() to get the size of a String and length to get the size of an array. Notice the absence of the parentheses when working with arrays.) |
Syntax |
datatype[] varvar[element] = valuevar.length |
Parameters |
datatype |
any primitive or compound datatype, including user defined classes |
var |
any valid variable name |
element |
int: must not exceed the length of the array - 1 |
value |
data to assign to the array element, must be the same datatype as the array |
|
Usage |
Web & Application |