Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Friday, April 26, 2019

Java ArrayList add method examples

Java ArrayList add method examples:

Introduction:

We will learn Java ArrayList add method with examples. Add method is used to add the specified values at last position of arraylist and at the specified position.

java.util.ArrayList is a class in Java which is implemented based on array concepts. It has many inbuilt methods to simplify the common functionalities.

In this post, we will see examples of how to use add method in ArrayList. Add method is used to add elements to it.

add method 

ArrayList has two add methods.
1. public boolean add(E e) - Adds the value after the last value in the arraylist.
2. public void add(int index, E element) - Adds the values at specified index. 
add() method first ensures that there is sufficient space in the arraylist. If list does not have space, then it grows the list by adding more spaces in underlying array. Then it add the element to specific array index.
Java ArrayList add method examples

ArrayList Characterstics

API Methods:

  • public boolean     add(E e)
  • public void add(int index, E element)

add(E e) Example:

This is a basic method, we can add only specific type of objects to it. Type is declared at the time of ArrayList object creation. These elements are added at the end of list.

   Non Generic:

package com.adeepdrive.arraylist;
import java.util.ArrayList;
import java.util.List;

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

        List nonGenericList = new ArrayList();
        nonGenericList.add(123); // int primitive
        nonGenericList.add("String"); // String
        System.out.println("nonGenericList values :: " + nonGenericList);
    }
}

Output:

nonGenericList values :: [123, String]

Generic Example:

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

        List<String> genericList = new ArrayList();
        genericList.add("value one"); // int primitive
        genericList.add("value two"); // String
        System.out.println("genericList values :: " + genericList);
    }
}

Output:

genericList values :: [value one, value two]

add(int index, E element) Example:

This is used to add a element at a particular index or position in the arraylist. Index starts from ZERO(0) in list. After adding a new value at specific index, then all values from that index will be shifted by one position.

public class AddIndexExample {
    public static void main(String[] args) {
        List<String> addAtIndex = new ArrayList();
        addAtIndex.add("First");
        addAtIndex.add("Third");
        System.out.println("addAtIndex values before calling index mehtod :: " + addAtIndex);
        addAtIndex.add(1, "Second"); // Adding at second position. 
        System.out.println("addAtIndex values after calling index mehtod :: " + addAtIndex);
    }
}

Output:

addAtIndex values before calling index mehtod :: [First, Third]
addAtIndex values before calling index mehtod :: [First, Second, Third]

No comments:

Post a Comment

Please do not add any spam links in the comments section.