AllFreePapers.com - All Free Papers and Essays for All Students
Search

What Is the Difference Between Arrays and Arraylist?

Autor:   •  November 24, 2015  •  Essay  •  325 Words (2 Pages)  •  849 Views

Page 1 of 2

What is the difference between arrays and ArrayList? What are the advantages and disadvantages of an ArrayList, and when should you use an ArrayList over a simple array?

One of the major differences between an array and an array list is than an array is a fixed length data structure while ArrayList is a variable length collection class. You will not be able to change the length of Array once it has been created in Java, but with the ArrayList can resize itself when it gets full depending upon capacity and load factor. Since the ArrayList is internally backed by Array in java, any resize operation in ArrayList will slow down the performance as it involves creating new Array and copying content form an old array to new array.

Another difference between Array and ArrayList, is that you cannot use Generics along with Array. You cannot store primitives in ArrayList. The ArrayList can only contain Objects, while Array can contain both primitives and objects.

Arrays:

Advantages:

  • Used to represent multiple data items of same type by using a single name
  • Can be used to implement other data structures like linked lists, stacks, trees, graphs
  • 2D arrays are used to represent matrices

Disadvantages:

  • Elements stores in an array must be known in advance
  • Array is a static structure meaning, that the array is a fixed size. The memory which is allocate to array cannot be increased or reduced.
  • The elements of array are stored in consecutive memory locations. This causes insertions and deletions to be difficult and time consuming.

ArrayList:

Advantages:

  • ArrayList can grow in size
  • Provides several convenient methods to add and remove elements to the collection.

Disadvantages:

  • Vulnerable to runtime errors
  • All data in the Array-List must be stored sequentially in memory.

If you know you will need to change the count of objects frequently, the ArrayList should be used. The ArrayList can grow in size and can add and remove elements in the collection easier.

...

Download as:   txt (2 Kb)   pdf (62.1 Kb)   docx (8.9 Kb)  
Continue for 1 more page »