What is ArrayThe 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. For Example: 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 arrayWe can identify and access each value with the help of an index number in an array. For example, arr[1] will be used to access the second element of the array in zerobased indexing. The array indexing can be of the following types: 0based indexingIf the first value of the array gets the index 0, it is called 0based indexing. Most of the programming languages follow 0based indexing. 1based indexingIf the array index starts from 1, it is called 1based indexing. It means the first value of the array will be assigned index 1. Nbased indexingWhen the index of the first element can be anything, even negative values, it is called Nbased 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' keywordWith 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. For Example: 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 valuesAnother 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. For Example: In the above example, we have declared the array and initialized it at the same time. Various operations on an Array1. Insertion operationIf 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. Example: Output: old array is : 1 2 3 4 5 6 new array is : 1 2 98 4 5 6 Explanation: 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 elementTo access any element of the array, we can just use its index number to access it. Example: Output: 4th element in the array is 4 Explanation: 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 (0based indexing). Time complexity: O(1) 3. Searching an elementWe 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. Example: Output: we found the target value Explanation: 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. Time complexity: 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:Output: target is not present Explanation: In the above code, we have used the binary search method to search the target element in the array.
4. Sorting the ArraySorting 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. Example: Output: array before sorting is: 5 3 7 8 4 6 1 array after sorting is: 1 3 4 5 6 7 8 Explanation: 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(n^{2}) O(n^{2}) time complexity: bubble sort, selection sort, insertion sort o(nlogn) time complexity: merge sort, quick sort, etc. Types of the ArrayThere can be various types of arrays based on their dimensions, so we divide them mainly into two types: 1. Onedimensional ArrayWhen elements of an array are arranged in a linear fashion, it is called a onedimensional array. It is the basic array we generally use in the problems. 2. Multidimensional ArraWhen there is more than one dimension of the array, it is called a multidimensional array. It can be 2D or 3D, or more dimensional. Example: Output: 2D array is : 1 2 3 4 5 6 7 8 9 Explanation: In the above code, we have created a 2dimensional array of integer data type. Advantages of the Array
Disadvantages of the Array
Applications of the Array
