Java Collections Framework:ArrayList-4(ArrayList (Add/Remove)

 

Java ArrayList add() Method Example


Here we are discussing about add() method of Java.util.ArrayList class. This method is used for adding an element to the ArrayList. Below is the method signature:

public boolean add(Object element)

Example

package beginnersbook.com;
import java.util.ArrayList;
public class Details {
    public static void main(String[] args) {

        //ArrayList<String> Declaration
        ArrayList<String> al= new ArrayList<String>();
        //add method for String ArrayList
        al.add("Ram");
        al.add("Shyam");
        al.add("CPS");
        al.add("John");
        al.add("Steve");
        System.out.println("Elements of ArrayList of String Type: "+al);

        //ArrayList<Integer> Declaration 
        ArrayList<Integer> al2 = new ArrayList<Integer>();
        //add method for integer ArrayList
        al2.add(1);
        al2.add(34);
        al2.add(99);
        al2.add(99);
        al2.add(78);
        System.out.println("Elements of ArrayList of Integer Type: "+al2);
    }
}

Output:



Elements of ArrayList of String Type: [Ram, Shyam, CPS, John, Steve]
Elements of ArrayList of Integer Type: [1, 34, 99, 99, 78]

Reference:

http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html#add(E)

Java ArrayList add(int index, E element) example


Simple add() method is used for adding an element at the end of the list however there is another variant of add method which is used for adding an element to the specified index.

public void add(int index, Object element)

This method adds the element at the given index.

Example

package beginnersbook.com;
import java.util.ArrayList;
public class AddMethodExample {
   public static void main(String[] args) {
       // ArrayList of String type
       ArrayList<String> al = new ArrayList<String>();
       // simple add() methods for adding elements at the end
       al.add("Hi");
       al.add("hello");
       al.add("String");
       al.add("Test");
       //adding element to the 4th position
       //4th position = 3 index as index starts with 0
       al.add(3,"Howdy");

       System.out.println("Elements after adding string Howdy:"+ al);
       //adding string to 1st position
       al.add(0, "Bye");

       //Print
       System.out.println("Elements after adding string bye:"+ al);
   }
}

Output:



Elements after adding string Howdy:[Hi, hello, String, Howdy, Test]
Elements after adding string bye:[Bye, Hi, hello, String, Howdy, Test]

Reference

http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html#add(int, E)

Java ArrayList addAll(Collection c) Method example


In this tutorial we will see the usage of addAll() method of java.util.ArrayList class. This method is used for adding all the elements of a list to the another list.

public boolean addAll(Collection c)

It adds all the elements of specified Collection c to the current list.

Example

In this example we are adding all the elements of an arraylist to another arraylist by using addAll() method.



package beginnersbook.com;
import java.util.ArrayList;
public class ExampleOfaddAll {
   public static void main(String[] args) { 
        // ArrayList1 of String type
        ArrayList<String> al = new ArrayList<String>();
        al.add("Hi");
        al.add("hello");
        al.add("String");
        al.add("Test");
        System.out.println("ArrayList1 before addAll:"+al);

        //ArrayList2 of String Type
        ArrayList<String> al2 = new ArrayList<String>();
        al2.add("Text1");
        al2.add("Text2");
        al2.add("Text3");
        al2.add("Text4");

        //Adding ArrayList2 into ArrayList1
        al.addAll(al2);
        System.out.println("ArrayList1 after addAll:"+al);
   }
}

Output:

ArrayList1 before addAll:[Hi, hello, String, Test]
ArrayList1 after addAll:[Hi, hello, String, Test, Text1, Text2, Text3, Text4]

How to copy and add all list elements to ArrayList in Java


In this tutorial we will see how to copy and add all the elements of a list to ArrayList. In order to do that we will be using addAll method of ArrayList class.

public boolean addAll(Collection c)

It adds all the elements of specified Collection c to the end of the calling list. It throws NullPointerException if the specified Collection is null.

Complete Example of Copying List elements to ArrayList

package beginnersbook.com;
import java.util.ArrayList;
import java.util.List;

public class ListToArrayListExample {

   public static void main(String a[]){
      ArrayList<String> al = new ArrayList<String>();

      //Adding elements to the ArrayList
      al.add("Text 1");
      al.add("Text 2");
      al.add("Text 3");

      System.out.println("ArrayList Elements are: "+al);

      //Adding elements to a List
      List<String> list = new ArrayList<String>();
      list.add("Text 4");
      list.add("Text 5");
      list.add("Text 6");

      //Adding all lements of list to ArrayList using addAll
      al.addAll(list);
      System.out.println("Updated ArrayList Elements: "+al);
   }
}

Output:



ArrayList Elements are: [Text 1, Text 2, Text 3]
Updated ArrayList Elements: [Text 1, Text 2, Text 3, Text 4, Text 5, Text 6]

Java ArrayList addAll(int index, Collection c) Method example


In the last tutorial we have shared the example of addAll(Collection c) method which is used for adding all the elements of Collection c at the end of list. Here we will see another variant add(int index, Collection c) which adds all the elements of c at the specified index of a list.

public boolean addAll(int index, Collection c)

Example

In this example we have two ArrayList of String type and we are adding the element of second arraylist at the 3rd position(index =2) of first arraylist.

package beginnersbook.com;
import java.util.ArrayList;
public class ExampleOfaddAllMethod {
   public static void main(String[] args) {
       // ArrayList1 
       ArrayList<String> al = new ArrayList<String>();
       al.add("Apple");
       al.add("Orange");
       al.add("Grapes");
       al.add("Mango");
       System.out.println("ArrayList1 before addAll:"+al);

       //ArrayList2 
       ArrayList<String> al2 = new ArrayList<String>();
       al2.add("Fig");
       al2.add("Pear");
       al2.add("Banana");
       al2.add("Guava");
       System.out.println("ArrayList2 content:"+al2);

       //Adding ArrayList2 in ArrayList1 at 3rd position(index =2)
       al.addAll(2, al2);
       System.out.println("ArrayList1 after adding ArrayList2 at 3rd Pos:\n"+al);
   }
}

Output:



ArrayList1 before addAll:[Apple, Orange, Grapes, Mango]
ArrayList2 content:[Fig, Pear, Banana, Guava]
ArrayList1 after adding ArrayList2 at 3rd Pos:
[Apple, Orange, Fig, Pear, Banana, Guava, Grapes, Mango]

Java ArrayList remove(int index) Method example


Method remove(int index) is used for removing an element of the specified index from a list. It removes an element and returns the same. It throws IndexOutOfBoundsException if the specified index is less than zero or greater than the size of the list (index size of ArrayList).

public Object remove(int index)

Example

package beginnersbook.com;
import java.util.ArrayList;
public class RemoveExample {
   public static void main(String args[]) {
       //String ArrayList
       ArrayList<String> al = new ArrayList<String>();
       al.add("AB");
       al.add("CD");
       al.add("EF");
       al.add("GH");
       al.add("AB");
       al.add("YZ");
       System.out.println("ArrayList before remove:");
       for(String var: al){
            System.out.println(var);
       }
       //Removing 1st element
       al.remove(0);
       //Removing 3rd element from the remaining list
       al.remove(2);
       //Removing 4th element from the remaining list
       al.remove(2);
       System.out.println("ArrayList After remove:");
       for(String var2: al){
             System.out.println(var2);
       }
    }
}

Output:

ArrayList before remove:
AB
CD
EF
GH
AB
YZ
ArrayList After remove:
CD
EF
YZ

Java ArrayList remove(Object obj) Method example


The method remove(Object obj) removes the specified object from the list. It belongs to the java.util.ArrayList class.

public boolean remove(Object obj)

Note:

  • It returns false if the specified element doesn’t exist in the list.
  • If there are duplicate elements present in the list it removes the first occurrence of the specified element from the list.

Example

In this example we have an ArrayList<String> and we are removing few strings from the list.



package beginnersbook.com;
import java.util.ArrayList;
public class RemoveExample {
   public static void main(String args[]) {
       //String ArrayList
       ArrayList<String> al = new ArrayList<String>();
       al.add("AA");
       al.add("BB");
       al.add("CC");
       al.add("DD");
       al.add("EE");
       al.add("FF");
       System.out.println("ArrayList before remove:");
       for(String var: al){
           System.out.println(var);
       }
       //Removing element AA from the arraylist
       al.remove("AA");
       //Removing element FF from the arraylist
       al.remove("FF");
       //Removing element CC from the arraylist
       al.remove("CC");
       /*This element is not present in the list so
        * it should return false
        */
       boolean b=al.remove("GG");
       System.out.println("Element GG removed: "+b);
       System.out.println("ArrayList After remove:");
       for(String var2: al){
           System.out.println(var2);
       } 
   }
}

Output:

ArrayList before remove:
AA
BB
CC
DD
EE
FF
Element GG removed: false
ArrayList After remove:
BB
DD
EE


Comments

Popular posts from this blog

Java OOPS:OOPS Concepts Overview

Selenium-Java Contents

Java OOPS:Constructors in Java – A complete study!!