contador Saltar al contenido

Difference between list and ArrayList in Java

List and ArrayList are members of the Collection framework. List a collection of elements in a sequence in which each element an object and the elements are accessible from the position (index). ArrayList creates a dynamic array of objects that increases or decreases in size when required. The main difference between List and ArrayList is that list an interface e ArrayList a classroom. Let us study the difference between List and ArrayList with the help of the comparison chart shown below.

Comparative graph

Basis for comparisonList of list of arrays
Basic List an interface ArrayList a standard collection class.
Syntax List of interfaces class ArrayList
Extend / Implement The list interface extends the collection panel. ArrayList extends AbstractList and implements the list interface.
Namespace System.Collections.Generic. System.Collections.
Work used to create a list of elements (objects) associated with their index numbers. ArrayList used to create a dynamic matrix that contains objects.

Definition of a list

list interface which extends the framework Collection . The list interface describes the collection of elements arranged in sequence. The list interface implemented by the following standard collection classes such as ArrayList, LinkedList, CopyOnWriteArrayList, Vector, Stack . The list interface contains items associated with their index numbers. You can access an item in the list by its location (index) in the list. A list created using the List interface starts with a zero-based index.

In addition to the methods inherited from the Collection framework, the List interface also defines a separate method. The methods added by the List interface are, add (int, E) is addAll (int, Collection) . These methods add an item to the list via their index. The methods within the list can generate an exception like UnsupportedOperationException if the method cannot change the list. When an object in a list is not compatible with another object in the list, it is also generated ClassCastException. Null elements are not allowed in the list if you try to insert a null object in the list, it is generated NullPointerException .

You can get an item from the list using the method get () . You can set the value of an item in the list using the method set () . You can also get the sublist from the list using a method Sublist () . It is convenient to operate on the sublist instead of on a list.

Definition of ArrayList

One of the standard ArrayList Collection classes that extends the class AbstractList and also implements the interface list . The ArrayList class is used to create dynamic arrays that grow and shrink when required. The list created using the ArrayList class is nothing but the array of objects. In Java, the standard array has the fixed length, so it is necessary to know the size of the array in advance. However, it may not be possible to know the length of the array required until the time of execution. Then, the Collection framework introduced the ArrayList class to overcome this problem.

ArrayList has constructors that create the array with its initial capacity. Although the capacity of the ArrayList class object increases automatically when elements are added to the array, you can still manually increase the capacity of the ArrayList object using the ensureCapacity () . it is preferable to initially increase the capacity of the array instead of reallocating the memory at a later time. Because the more expensive reallocation of memory allocation at once.

Key differences between List and ArrayList

  1. One of the most important differences between List and ArrayList is the list interface and ArrayList one class Standard collection.
  2. The list interface extends the framework Collection while, ArrayList extends the class AbstractList and implements interfaces list .
  3. The namespace for the List interface System.Collection.Generic while, the namespace for ArrayList System.Collection .
  4. The list interface creates a collection of items that are stored in a sequence and are identified or accessible by their index number. On the other hand, ArrayList creates a series of objects in which the matrix can grow dynamically when required.

Conclusion:

ArrayList overcomes the problem of a static array in standard Java, which means that the array cannot grow in size once created. When an array is created using ArrayList, a dynamic array is created that can grow and shrink in size when needed. The standard ArrayList collection class extends the List interface.