Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
Arrays in Java are homogeneous data structures implemented in Java as objects. Arrays store one or more values of a specific data type and provide indexed access to store the same. A specific element in an array is accessed by its index. In this article, we will discuss Dynamic Array in Java in the following sequence:
The dynamic array is such a type of an array with a huge improvement for automatic resizing. The only limitation of arrays is that it is a fixed size. This translates into a meaning that you can only specify the number of elements that your array can hold ahead of time. On the other hand, the dynamic arrays can expand as we add more elements in real-time. Therefore, the coder does not need to determine the size of the array ahead of time. It does have a few more strengths as well:
Quick lookup. Same as just like arrays, when retrieving the element at a given index, takes O (1) time.
Variable size. We can insert as many elements as we want and a dynamic array will accordingly expand to hold them.
Cache-friendly. Similar to arrays, dynamic arrays can put items next to each other in memory, thus making efficient utilization of caches.
There are some downsides to using dynamic arrays in our code. Although, we do use dynamic arrays more than anything does in most applications there are some cases where they do not become the most preferred choice due to its limitations.
Slow worst-case appends. Normally, while the addition of a new element at the end of a dynamic array, it takes O (1) at one instance. However, if the dynamic array does not have any more indices for a new item, then it will need to expand, which takes O (n) at a time.
Costly inserts and deletes. Similar to arrays, the elements are stored adjacent to each other. So while adding or removing an item in the center of an array it requires pushing other elements, which takes O (n) at a time.
The below diagrams show how the arrays work in real-time and depict how the elements are stacked up. It also shows how the instructions change for an average case and the worst case of array functions.
When we initialize a dynamic array, the dynamic array implementation creates an understood fixed-size array. The initial size corresponds to the implementation. For example, let us make our implementation array to use 10 indices. Now we append four items to our dynamic array. Now, our dynamic array has a length of four. However, our underlying array has a length of 10. Hence, we could say that dynamic array size is four and its capacity is 10. A dynamic array stores a specific end Index to keep track of the ending point of the dynamic array and the starting point from where the extra capacity begins.
There can be cases where we try to append an item to an array where the capacity is already full. Hence, to create room dynamic arrays automatically create new, bigger and underlying array. Usually, it becomes twice as big to handle any new additions, which it did not anticipate earlier. Hence, copying each item consumes no time. Whenever appending an item to our dynamic array automatically makes a new double-size underlying array, which append takes no time.
While deleting an element from an array, the default “remove()” method removes an element from the end and automatically stores zero at the last index. It will also delete elements at a specific index by calling removeAt(i) method where “I” is index. The removeAt(i) method shifts all the right elements in the left side from the given index.
When arrays have no data at the right side of the array which than take unnecessary memory, the method srinkSize() frees extra memory. When all slots are consumed and additional elements are added, the underlying fixed-size array has to increase size. The actual resizing is expensive, as we have to allocate a bigger array and copy forward all elements from an array you have overgrown before it can finally append a new item.
Below is an example of a program where the array size becomes full and new elements are copied to a new double size array. The element which is a string element called “Mahavir” is an addition to the already full array of size 3.
import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class AddingItemsDynamically { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter the size of the array :: "); int size = sc.nextInt(); String myArray[] = new String[size]; System.out.println("Enter elements of the array (Strings) :: "); for(int i=0; i<size; i++) { myArray[i] = sc.next(); } System.out.println(Arrays.toString(myArray)); ArrayList<String> myList = new ArrayList<String>(Arrays.asList(myArray)); System.out.println("Enter the element that is to be added:"); String element = sc.next(); myList.add(element); myArray = myList.toArray(myArray); System.out.println(Arrays.toString(myArray)); } }
Output:
With this, we come to the end of Dynamic Array in Java article. I hope you got an idea of how to work with dynamic arrays.
Check out the Java Online Course by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
Got a question for us? Please mention it in the comments section of this “Dynamic Array in Java” blog and we will get back to you as soon as possible.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 5th October,2024 5th October SAT&SUN (Weekend Batch) | View Details |
edureka.co