Array (Java)

An array in Java is a fixed-size, ordered collection of values that all share the same type. Once you set an array's length, it can't grow or shrink.

Java

You access elements by index, starting at 0, using square brackets like nums[0]. The array's length is available through the .length field (no parentheses).

Because arrays have a fixed size, many programs use an ArrayList instead when the number of elements can change.

Examples

Java

int[] nums = {10, 20, 30};
System.out.println(nums[0]);
System.out.println(nums.length);
nums[1] = 99;
System.out.println(nums[1]);

Output:

10
3
99

Learn it in a lesson

See also

Back to the Dictionary