Arrays
suggest changeVersions
Java SE 1.0 through Java SE 9 (Early Access).
Introduction
Arrays allow for the storage and retrieval of an arbitrary quantity of values. They are analogous to vectors in mathematics. Arrays of arrays are analogous to matrices, and act as multidimensional arrays. Arrays can store any data of any type: primitives such as int or reference types such as Object.
Syntax
ArrayType[] myArray;// Declaring arraysArrayType myArray[];// Another valid syntax (less commonly used and discouraged)ArrayType[][][] myArray;// Declaring multi-dimensional jagged arrays (repeat []s)ArrayType myVar = myArray[index];// Accessing (reading) element at indexmyArray[index] = value;// Assign value to positionindexof arrayArrayType[] myArray = new ArrayType[arrayLength];// Array initialization syntaxint[] ints = {1, 2, 3};// Array initialization syntax with values provided, length is inferred from the number of provided values: {[value1[, value2]*]}new int[]{4, -5, 6} // Can be used as argument, without a local variableint[] ints = new int[3]; // same as {0, 0, 0}int[][] ints = {{1, 2}, {3}, null};// Multi-dimensional array initialization. int[] extends Object (and so does anyType[]) so null is a valid value.
Parameters
ArrayTypeis the element type. It can be a primitive (int,long,byte) or a reference type (String,MyObject, and so on).indexis the position of an element. The first element is at index0.lengthis fixed when the array is created, either explicitly (new int[3]) or from the number of values in an initializer ({1, 2, 3}).
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents