Create Arraylist in Java - How do i add element to ArrayList

Published March 25, 2022

An ArrayList class is used in the Java Collection framework to create resizable arrays. To create an Arraylist in java we need to do follow steps

Import  Java.util.ArrayList package

Once we add this package then we will create Arraylist using below syntax

ArrayList<Type> arrayList= new ArrayList<>();

 

Example of ArrayList in Java

In this example, we've created an ArrayList titled fruits

import java.util.ArrayList;
public class ArrayListExample {
  public static void main(String[] args) {
  // ArrayList Fruits
  ArrayList<String> fruits = new ArrayList<>();

  }
}

 

Add Elements to Java Arraylist

To add elements to ArrayList we will use add() method

import java.util.ArrayList;
public class ArrayListExample {
  public static void main(String[] args) {
  // ArrayList Fruits
  ArrayList<String> fruits = new ArrayList<>();

  // Add elements 
  fruits.add("Kiwi");
  fruits.add("Orange");
  fruits.add("Lemon");
  System.out.println("Fruit ArrayList: " + fruits);
  }
}

 

Output:

Fruit ArrayList: [Kiwi,Orange,Lemon]

 

Collection framework provided inbuilt method to implement Arraylist features
 
void add(int index, Object element) : This method will be used to add an element to a specified position. If the given position is not find then it throws IndexOutOfBoundsException (index < 0 || index > size()).


boolean add(Object o), this method will append the element to the end of the given list

 

boolean addAll(Collection c), this method will be used to add all elements of specified collection to the given list. If the given collection is null it throws NullPointerException

 

boolean addAll(int index, Collection c), this methos will be used to add elements of collection into given list from respected position. If the given collection is null it throws NullPointerException


void clear(), this method will be used to clear the elements from the list.


Object clone(), this method will be used to create a copy of an Arraylist. This copy will be a shallow copy


boolean contains(Object o), When we want to check the an element is exist in the array we will use this method. This method will return true if the element contains in list.


void ensureCapacity(int minCapacity) This method will be used to increase the capacity of Arraylist.


Object get(int index), to get an element from ArrayList we will use this get() method to pass a respected position. If the index is not available it throws IndexOutOfBoundsException (index < 0 || index >= size()).

 

int indexOf(Object o), this method will be used to find the position of an element inside ArrayList. If the list doesn't contain element it will returns -1


int lastIndexOf(Object o), this will return last occurrence position of an element in the given ArrayList.If the list doesn't contain element it will returns -1


Object remove(int index), this method will be used to remove the element from the ArrayList at given index.If the index is not available it throws IndexOutOfBoundsException (index < 0 || index >= size())


protected void removeRange(int fromIndex, int toIndex), this method will be used to remove the elements from an ArrayList in between given range. Here fromIndex is inclusive and toIndex is exclusive.

 

Object set(int index, Object element), this method will be used to replace an element at given index with new element from the ArrayList. If the index is not available it throws IndexOutOfBoundsException (index < 0 || index >= size())

 

int size(), this method will be used to get the size of an ArrayList.


Object[] toArray(), this method will convert given ArrayList to an Array.


Object[] toArray(Object[] a), this method will returns an Array containing all the elements of the list in correct order.


void trimToSize(), this method will trim the capacity of an ArrayList to the current size.

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

160 Views