What is Array
The array is a data structure where values or items are placed in a linear order, which means the memory assigned to each item is contiguous. The data type of an array is the same for all the elements present in it.
With the contiguous memory allocation, it becomes easier to find out the memory location of any element in the array by just knowing the first memory location and adding the offset.
In the above example, we have an integer array, and the memory locations are contiguous, where each integer takes 4 bytes, and the starting address is 4000.
We cannot increase or decrease the size of an array dynamically once it is declared. The memory allocation is static in most of the languages, so we have to declare the array size at the time of array declaration.
Indexing in the array
We can identify and access each value with the help of an index number in an array.
For example, arr will be used to access the second element of the array in zero-based indexing.
The array indexing can be of the following types:
If the first value of the array gets the index 0, it is called 0-based indexing. Most of the programming languages follow 0-based indexing.
If the array index starts from 1, it is called 1-based indexing. It means the first value of the array will be assigned index 1.
When the index of the first element can be anything, even negative values, it is called N-based indexing.
Initialization Methods in an Array:
There are various methods by which we can declare and initialize the array in Java.
1. Using the 'new' keyword
With the help of a new keyword, we can allocate the specific size of memory to the array. In java, when we use the new keyword, it allocates the memory in a heap, so it allocates the memory as static.
In the above code, we have declared an array of size 12 of type integer. We have allocated the memory, which can store 12 integers using the new keyword.
2. By direct values
Another way to initialize and declare the array is to insert the values directly in the array without using a new keyword and specifying the array size.
In the above example, we have declared the array and initialized it at the same time.
Various operations on an Array
1. Insertion operation
If we have an array, we can insert any value to any index of the array, but the new value must have the same data type.
old array is : 1 2 3 4 5 6 new array is : 1 2 98 4 5 6
In the above code, we have declared an array of an integer data type with some values. Now, we have inserted a new value, 98, at index two and changed the array.
Time complexity: O(1) or constant time complexity.
2. Accessing any element
To access any element of the array, we can just use its index number to access it.
4th element in the array is 4
In the above code, we have declared and initialized an array then we access the fourth value of an array by using index number 3 (0-based indexing).
Time complexity: O(1)
3. Searching an element
We can search any element in the array. It may or may not be present in the array. We can use either linear or binary search for searching operations.
we found the target value
In the above code, we have an array and a target value. We put a loop on the array and search for the target element. Since we got the target value in the array, we came out of the loop and printed the statement.
O(N), where N is the number of elements in the array.
We can also use binary search methods to reduce the time complexity in a sorted array.
The time complexity of binary search: O(logN)
Binary Search Example:
target is not present
In the above code, we have used the binary search method to search the target element in the array.
4. Sorting the Array
Sorting means basically arranging or reordering the elements of the array in a specific order, either it would be ascending or descending. There are a lot of sorting techniques used in the array, like the bubble sort, insertion sort, merge sort, selection sort, radix sort, heap sort, etc.
array before sorting is: 5 3 7 8 4 6 1 array after sorting is: 1 3 4 5 6 7 8
In the above code, we have used the bubble sort method to sort the given array in ascending order. We have an integer array that is unsorted. We have compared each adjacent value and swapped them if they were not in the correct order.
Time complexity: O(n2)
O(n2) time complexity: bubble sort, selection sort, insertion sort
o(nlogn) time complexity: merge sort, quick sort, etc.
Types of the Array
There can be various types of arrays based on their dimensions, so we divide them mainly into two types:
1. One-dimensional Array
When elements of an array are arranged in a linear fashion, it is called a one-dimensional array. It is the basic array we generally use in the problems.
2. Multi-dimensional Arra
When there is more than one dimension of the array, it is called a multi-dimensional array. It can be 2D or 3D, or more dimensional.
2D array is : 1 2 3 4 5 6 7 8 9
In the above code, we have created a 2-dimensional array of integer data type.
Advantages of the Array
Disadvantages of the Array
Applications of the Array