by BehindJava
What are Arrays in Java
In this tutorial, we are going to learn in detail about Arrays in Java.
Arrays in Java
- Arrays are not a dynamic data structure which means that once you’ve created an array instance you cannot change it size.
- Arrays store multiple data of similar types with the same name.
- It allows random access to elements. As the array is of fixed size and stored in contiguous memory locations there is no memory shortage or overflow.
- Since the elements in the array are stored in a serial memory locations it is easy to iterate and time required to access an element if the index is known.
- Insertion and deletion operations are difficult in an array as elements are stored in contiguous memory locations and the shifting operations are costly.
Here is an example, An array size of 8 is declared and initialized with the index number and using a for loop we can iterate or traverse the elements of the array.
public class Arrays {
public static void main(String[] args) {
int[] intArray = new int[8];
intArray[0] = 22;
intArray[1] = 27;
intArray[2] = -15;
intArray[3] = 7;
intArray[4] = 35;
intArray[5] = 10;
intArray[6] = -22;
intArray[7] = 1;
for (int i = 0; i < intArray.length; i++) {
System.out.print(intArray[i]+ " ");
}
}
}
Output:
22 27 -15 7 35 10 -22 1