Arraylist Java

 List Category:


ArrayList

  • ArrayList is the implementation class of the List interface. which is used to store the group of individual objects where duplicate values are allowed.
  • ArrayList internally follows an array structure which means in ArrayList all the elements are stored in continuous memory locations same as an array but the ArrayList size is not fixed.
  • ArrayList is not a synchronized class.

  • If any object is synchronized, we can access only one thread at a time, but if an object is not synchronized, we can access multiple threads.
List


Creation of ArrayList:

ArrayList<E> al= new ArrayList<E>();

this constructor is used to create the new ArrayList with default capacity 10 and the size of ArrayList is 0.

ArrayList<E> al= new ArrayList<E>(int capacity);

this constructor is used to create the new ArrayList with specified capacity and the size of the arraylist is 0.

Note: 

size means the number of elements that are stored in the Collection.

capacity means the memory allocated for elements.

ArrayList<E> al= new ArrayList<>(Collection obj);

 this constructor is used to create the new ArrayList by copying the elements of any existing Collection (List, Set).

here

<E>:  is called generic parameter type.

E: Element type which must be reference type but not any primitive type.

Methods of ArrayList

 boolean add(E obj)

this method adds new element to the end of the List.

void add(int position, E obj)

this method insert new element into the List at specified position.

 boolean remove(E obj)

this method removes specified elements from the List(manual boxing is needed).

E remove(int position)

this method removes the particular element from the List at the specified position.

int size()

this method returns the count of the number of elements available in the List.

void clear()

this method removes all the elements from the List.

boolean isEmpty()

this method returns true if List does not contain any element otherwise it returns false 

boolean contains(E obj)

 this method returns true if a specified element is available in the list otherwise it returns false.

E get(int position)

this method returns the element from the List at the specified position. 

this method is especially used to access the Elements of the List. 

E set(int position, E obj)

this method replaces the new Element at the specified position in the List.

Note:

1. In add(), remove(), get() and set() methods if we write any wrong index which is not available then it will throw a runtime exception called IndexOutOfBoundsException.

2. ArrayList supports storing multiple null values.


  • difference ArrayList Vs LinkedList Vs Vector Vs Stack.


ArrayList Vs LinkedList

What is LinkedList

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.