![]() |
VOOZH | about |
In Java, arrays are of fixed size, and we can not change the size of an array dynamically. We have given an array of size n, and our task is to add an element x into the array.
There are two different approaches we can use to add an element to an Array. The approaches are listed below:
The first approach is that we can create a new array whose size is one element larger than the old size.
[10, 20, 30, 40, 50, 70]
The second approach is we can use ArrayList to add elements to an array because ArrayList handles dynamic resizing automatically. It eliminates the need to manually manage the array size.
[10, 20, 30, 40, 50, 70]