Introduction to Arrays
The Array is the most primitive data structure in the field of computer science. In fact, as Wikipedia claims, it has become the inspiration to several data structures such as lists and strings. In this post, we are going to learn everything that you need to know about arrays and see how arrays are used in Java.
Definition
An array is a collection of items stored at contiguous memory locations.The main idea here is to store multiple items of the same datatype together.
We identify each item in the array using the position of the element in the array. We use indexes to denote the position of an item. In arrays the indexes start with 0.
For example, Lets have 10 chairs arranged in a straight line with people sitting on them. All the chairs are numbered from 0–9. Thus the chair numbers are the indexes of the array. Now if we want someone to sit on chair with number 4( 5th chair) we can simply do that by specifying the index(4). Thus the chair numbers are the index numbers of an array. Thus accessing a particular element is easier in this, whereas functions like shifting can be costly.
Arrays can be 1 dimensional, 2 dimensional, n dimensional. 2 dimensional arrays are nothing but an array of an array. Similarly n dimensional array is nothing but an array of (n-1) dimensional arrays.
When to Use
Imagine we want to store roll numbers of 1000 students. Now we can create 1000 integer variables and then assign them values. But this is a tedious task and it will become difficult to remember the variable names when we want a value. Such a situation is perfect for using arrays. We can create an Integer array of size 1000 and store the values in it. This way remembering the variable name becomes simple and it also becomes easy to access data.
Lets understand how to use arrays with examples. We can declare and initialize an array with null values by using the following code.
String[] colors = new string[3]; // This creates an empty array of size 3
We can also directly initialize an array with the choice of our values by using the following code.
String[] colors = {“Red”,”Orange”,”Blue”}; // This creates an array with the values given in the RHS.
We can access a particular item in the array above as follows.
System.out.println(colors[0]); //Prints Red
The length method gives the size of the array. We can find the length of the above array as follows.
System.out.println(colors.length) //Prints 3
Example of a 2 Dimensional Array:
int[4][4] multi = { {0,2,4,6}, {4, 7, 8,9}, {2,4,3,7} } // a 2D Array
We can access a particular item in the array above as follows.
System.out.println(multi[1][2]); //Prints 8
Array can contains primitives (int, char, etc) as well as object (or non-primitives like Integer, String, etc) references of a class depending on the definition of array. In case of primitives data types, the actual values are stored in contiguous memory locations. In case of objects of a class, the actual objects are stored in heap segment.
Advantages
- Arrays represent multiple data items of the same type using a single name.
- Arrays allow random access to elements. This makes accessing elements by position faster.
- Arrays have better cache locality that can make a pretty big difference in performance.
Disadvantages
- You can’t change the size i.e. once you have declared the array you can’t change its size because of static memory allocated to it.
- Here Insertion and deletion are difficult as the elements are stored in consecutive memory locations and the shifting operation is costly too.
Thus we have discussed the fundamentals of array data structure. Thank you for going through this article by sparing your valuable time. Since this is my first blog, there maybe errors. Therefore, your feedback is always highly appreciated.