You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
An array is a special type of object that can hold an ordered collection of elements.
The type of the elements of the array is called the base type of the array.
The number of elements it hold is a fixed attribute called its length.
Java supports arrays of all primitive and reference types.
We create an array of a specified length and access the elements with the index operator, []
An array is an instance of a special Java array class and has a corresponding type in the type system. This means that to use an array, as with any other object, we first declare a variable of the appropriate type and then use the new operator to create an instance of it.
ARRAY TYPES
An array type variable is denoted by a base type followed by the empty brackets, [].
int [] arrayOfInts; // preferredintarrayOfInts []; // C-style
In each case, arrayOfInts is declared as an array of integers.
The size of the array is not yet an issue because we are declaring only the array type variable.
We have not yet created an actual instance of the array class, with its associated storage.
It’s not even possible to specify the length of an array when declaring an array type variable.
The size is strictly a function of the array object itself, not the reference to it.
// An array of reference types can be created in the same way:String [] someStrings;
Button [] someButtons;
ARRAY CREATION AND INITIALIZATION
The new operator is used to create an instance of an array.
After the new operator, we specify the base type of the array and its length with a bracketed integer expression: