Before we proceed further, let's understand why we need an array. Suppose we need to enter the roll number of a student. We can do it in the following way:
int rolno = 23;
Now, suppose we need to enter 5 roll numbers. We can use the same concept as in the example above.
int rolno1 = 23;
int rolno2 = 93;
int rolno3 = 63;
int rolno4 = 33;
int rolno5 = 3;
But this method is very time-consuming and less efficient. To address this issue, we use arrays as a solution.
Arrays
An array is a data structure that stores a collection of items, all of the same type, in a contiguous block of memory. Arrays are used to organize data so that it can be accessed and manipulated easily.
Syntax of an Array
datatype[] variable_name = new datatype[size of an array];
Note: The datatype
specifies the type of data that the array stores.
For example:
int[] rolnos = new int[5];
Now, let's dive deeper into the details of the above code snippet.
int[] rolnos; //Here, the array is declared, and the rolnos are defined in the stack. This happens at the compile time.
rolnos = new int[5]; //Here, the array is being initialized, and an object is created in memory (heap). This happens at the runtime and dynamic memory allocation (DMA) takes place.
Note: Arrays are a form of continuous memory allocation, but this statement is true only in C/C++. In Java, it does not hold true because Java does not have the concept of pointers. In Java, memory management depends entirely on the Java Virtual Machine (JVM).
In Java, array objects are stored in the heap, and objects in the heap are not stored continuously. Hence, array objects in Java may not be in a continuous form.
Other Important Points
The
new
keyword is used to create an object.Strings are immutable, while arrays are mutable.
int[] arr = new int[3]; String[] names = new String[3];
In the above examples, if we don't store any values in the array, they will automatically contain
arr = {0, 0, 0}
andnames = {null, null, null}
.null
is a special literal and can only be assigned to non-primitives.Each element in the array is a separate object in the heap.
Multidimensional Array
You can think of this as an array of arrays.
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
OR
[
[1, 2, 3, 4],
[5, 6],
[7, 8, 9]
]
The above two are examples of 2-dimensional arrays containing rows and columns. We can also have three-dimensional arrays and beyond, but after three dimensions, it becomes difficult for us to visualize.
ArrayLists
ArrayLists are used when the size of an array is unknown. They are similar to the concept of vectors in C++.
Syntax of an ArrayLists
ArrayList<Integer> list = new ArrayList<> (10);
You may or may not write 10; it's your choice. Writing 10 has no effect on the ArrayList, as we can add as many elements as we want.
In the above example, we used Integer
, which is a wrapper class, instead of int
, which is a primitive data type. Remember, in ArrayLists, we use wrapper classes.
Note: Internally, the size of the array is fixed. Suppose the array has the capacity to store 50 elements. As we input elements, when the ArrayList reaches half its capacity, something happens internally: a new ArrayList of approximately double the size of the previous one is created.
When we input the 25th element, Java creates a new ArrayList with a capacity of about 100 elements, copies all the elements from the old ArrayList into the new one, deletes the old ArrayList, and continues storing data in the new ArrayList. This process repeats whenever the new ArrayList reaches its capacity (e.g., at the 50th element for the updated ArrayList).