What are the drawbacks of the arrays in Java?

Java 8Object Oriented ProgrammingProgramming

Array is a container which can hold a fix number of items and these items should be of the same type. Most of the data structures make use of arrays to implement their algorithms. Following are the important terms to understand the concept of Array.

  • Element − Each item stored in an array is called an element.
  • Index: Each location of an element in an array has a numerical index, which is used to identify the element.

The size of the array will be determined at the time of creation.

Disadvantages of arrays

Deleting or inserting − You cannot insert a new element at the middle of the array. In the same way you cannot delete elements from the middle of the array. You can only insert/delete from the end of the array.

Increasing size − You cannot increase the size of the arrays in Java, if you want to add new elements you need to create new array with extended size and assign to the array reference. This leaves the original object for garbage collection and thus wastage of memory occurs.

Storing Objects − You can store objects in an array but you cannot store objects of different types.

Processing Elements − Except some operations provided by the Array class, you cannot process the contents of an array.

Modifying elements − To delete or, change the elements of an array you need to traverse throughout the array which increases the time complexity.

To overcome these disadvantages, you can use collections instead of arrays.

raja
Published on 02-Aug-2019 14:16:04
Advertisements