An array is a data structure that holds a fixed number of elements of the same type. It is a non-primitive data type. It is a group of like-typed variables that are referred to by a same name (i.e. all elements are stored under one name).Element present in an array are accessed by the indexes. They can be created with one (single) or more (multiple) dimensions. Elements in the array are stored at adjacent positions which makes easy for used to find locations of elements.
Characteristics:
- Fixed Size: Once created, the size of an array cannot be changed. It is determined at the time of declaration and initialisation.
- Ordered Collection: Arrays maintain the order of elements, meaning the position of each element in the array is fixed.
- Homogeneous Elements: All elements in an array must be of the same data type. For example, an array of integers can only store integer values. Arrays can hold elements of primitive data types or objects (non-primitive).
- In Java, arrays are zero-indexed, meaning the index of the first element is 0, the index of the second element is 1, and so on.
- Array elements can be accessed using square brackets (
[]
) with the index of the element.
Array Declaration and Initialization
- Declaration without initialisation:
dataType[] arrayName;
ordataType arrayName[];
- The
arrayName
can either be assigned another array or new array can be createdarrayName = new dataType[arraySize]
- The
- Declaration with initialisation and size:
dataType[] arrayName = new dataType[arraySize];
ordataType arrayName[] = new dataType[arraySize];
After an array is created with the defined size, values can be assigned using its index. Un-assigned elements automatically take the default value of the array’s data type. In this case,0
for the integer data type. - Declaration with initialisation and values:
dataType[] arrayName = {value1, value2, ...};
ordataType arrayName[] = {value1, value2, ...};
Initialisers like{....}
can only be used while declaring.
Components
-
dataType
: It specifies the type of elements the array will hold. It can be a primitive data type or a reference to an object type. -
arrayName
: It is the identifier used to refer to the array. It contains the reference to the array. -
arraySize
: It represents the number of elements the array can hold. It must be a non-negative integer. -
value1, value2, ...
: These are optional initial values for the elements of the array. The number of initial values should match the size of the array.
Accessing Array
- Using Indexing: Array elements are accessed using zero-based indexing, where the index represents the position of the element in the array. You use square brackets (
[]
) with the index inside to access an element.
int[] numbers = {10, 20, 30, 40, 50};
int firstElement = numbers[0]; // Accesses the first element (10)
int thirdElement = numbers[2]; // Accesses the third element (30)
- Index Range: The index should be within the range of the array. Accessing elements outside the range will result in an
ArrayIndexOutOfBoundsException
. - Length Property: the
length
property of the array can be used to determine the number of elements it contains. It returns an integer representing the length of the array. - Iteration: Arrays are commonly iterated using loops such as
for
orforeach
. This allows you to access and process each element of the array sequentially.
int[] numbers = {1, 2, 3, 4, 5};
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]); // Accesses and prints each element
}
for(int number : numbers) {
System.out.println(number); // Accesses and prints each element
}
Modifying Array
It means changing the values of existing elements.
Value of an array elements can be updated using the index.
int[] numbers = {10, 20, 30, 40, 50};
numbers[2] = 35; // Modifies the value of the third element to 35
Can array be resized?
No. Arrays in Java have a fixed size once they are initialized. If you need to change the size of an array, you typically create a new array with the desired size and copy the elements from the old array to the new one.
int[] oldArray = {1, 2, 3};
int[] newArray = new int[5];
System.arraycopy(oldArray, 0, newArray, 0, oldArray.length); // Copies elements from oldArray to newArray
int[] oldArray = {1, 2, 3};
int[] newArray = Arrays.copyOf(oldArray, 5); // Creates a new array with a length of 5 and copies elements from oldArray
Single Dimensional Arrays
Also called one-dimensional array is simplest form of array. It consists of single row or sequence of elements. Each element in a single-dimensional array is accessed using single index.