Java Arraylist add() Function

arraylist_add

What is an ArrayList?

Arraylist is a part of collection framework in java. It contains some inbuilt function like add() function which can be found in the java.util.ArrayList Package.
It implements the List interface of the collections framework.

Here, in the page we will discuss about the ArrayList’s add() Method in java.

ArrayList add Function:

Arraylist contains several inbuilt functions that are used to perform several operations in of  arraylist in Java. add() function is also an inbuilt method of class ArrayList.
add() method is used to add elements in an arraylist with the location.

Syntax:

arraylist_name.add(index,element)

Definition of Parameters:

  • arrayList_name : This contains the name of the arraylist in which the functions have to be performed.
  • index : it contains the index of the element at which it is inserted.
  • element : It contains the element that has to be inserted.

Example Without the Specified Index:

Run
// Importing all the required packages
import java.util.*;

public class Main{
  public static void main(String[] args) {

    // Initializing the arraylist
    ArrayList PrepInsta = new ArrayList<>();

    // Adding parameters to Arraylist
    PrepInsta.add("Lakshit");
    PrepInsta.add("Rishikesh");
    PrepInsta.add("Rishik");


    System.out.println("ArrayList: " + PrepInsta);
  }
}
Output:

ArrayList: [Lakshit, Rishikesh, Rishik]

Explanation:

In the above code we haven’t specified the index of the element to be added in the arraylist so by default, It will add the element at the end of the arraylist.

Inserting Element at a Specified Position:

We can also insert an element in the existing arraylist by specifying the location of the element in the add() function.

arraylist add function
Run
// Importing all the required packages
import java.util.*;

public class Main{
  public static void main(String[] args) {

    // Initializing the arraylist
    ArrayList PrepInsta = new ArrayList<>();

    // Adding elements to the arraylist
    PrepInsta.add("Lakshit");
    PrepInsta.add("Rishikesh");
    PrepInsta.add("Rishik");
    System.out.println("Before Adding an Element: " + PrepInsta);

    // Adding element at the specified index
    PrepInsta.add(1,"Nitin");

    System.out.println("After adding an Element: " + PrepInsta);
  }
}
Output:

Before Adding the Element: [Lakshit, Rishikesh, Rishik]
After adding the Element: [Lakshit, Nitin, Rishikesh, Rishik]

Explanation:

In the above Example. we add 3 elements to an arraylist without specifying the index and then we added an element with to the 1st index.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription