
Why do we need Arrays?
It was simple when we had to store just 5 integer numbers in our computer memory. But wait, what if we have to store 5000 integer numbers? That's a pretty large value!
Is it possible to use 5000 variables?
The answer is, Yes! It might be possible. But how much time would it take to create these variables? We have no idea!!!
To handle these types of circumstances, in almost all programming languages we have a concept called Array.
An array is a type of contiguous memory that is used to store the collection of homogenous data elements(data of the same type) indexed by contiguous integers.
A typical representation of an array looks like this:

Syntax of an array
So now, If we have to store 5000 integer numbers we just need to write a single line of code.
The following line of code is used to declare an array in Java:
int[] numbers = new int[5000];
It will create an array of size 5000.
Let's understand this better:

What's special about Arrays?
The key point of an array is its random accessibility. That is, we have constant time access to any particular element in the array - constant time access to read, and constant time access to write. We can access individual elements of the array using their index value.
How does that actually work?
Let's consider that we have to find the address of an element at index 7 with array address 1000 and element size 8.
Mathematically, it can be calculated using the formula:
array_address + element_size * (i - first_index) = 1000 + 8 (7 - 0) = 1048
Thus the address of the element at index 7 is 1048.
This calculation is done by our compiler/interpreter we do not need to worry about it.
Multi-dimensional arrays
In addition to one-dimensional arrays, many programming languages support multi-dimensional arrays like two-dimensional arrays and three-dimensional arrays.
These are represented using the number of rows and columns.

For example, we can declare a two-dimensional array in Java using the following line of code:
int[][] arr = new int[4][5];
This creates an array consisting of 4 rows and 5 columns for a total of 20 elements in the array.
When should we use arrays?
The decision to use or not use arrays depends on the specific requirements and type of problem at hand.
We should use arrays when:
A fixed number of array elements need to be stored.
The order of elements needs to be maintained.
Faster processing and efficient access is required.
Elements of the same data type need to be stored.
We should not use arrays when:
The size of the data collection is not known in advance or varies significantly.
The data collection needs to be dynamically resized frequently.
The order of the elements is not important.
Elements of different data types need to be stored.
Applications of arrays
Arrays are used to implement data structures like stacks, queues, heaps, and hash tables.
They can also be used to store and manipulate collections of data. For example, it can be used in multimedia applications, game development, artificial intelligence, and machine learning to store individual objects, characters, images, items, etc.
They are used to implement searching and sorting algorithms.
Arrays can be used to store large amounts of data in databases.
Conclusion
To conclude, we can say that arrays are simple, efficient, and widely used data structures that are used to simplify complex algorithms and store and manipulate collections of similar elements.
Thank you so much for reading! I hope this article was helpful.
Let's get connected:
About the author
Passionate Learner!
Comments
Join the discussionNo comments yet. Be the first to comment.
Data Structures
In this series, I will include all the articles related to the Data Structures involved in Computer Science.




