Append To ArrayList Java

Append To ArrayList Java | In this post, we will discuss how to append an element to the list and how to append ArrayList to another ArrayList in Java. To append an element to the list we have to insert an element to the end of the ArrayList. Let us see some examples of appending to an ArrayList in Java:-

Example-1:
ArrayList:- [1, 2, 3, 4, 5]
Element:- 9
After appending element to the ArrayList:- [1, 2, 3, 4, 5, 9]

Example-2:
ArrayList1:- [1, 2, 3, 4, 5]
ArrayList2:- [0, 9, 8, 7, 6]
After appending ArrayList2 to ArrayList1:- [1, 2, 3, 4, 5, 0, 9, 8, 7, 6]

Program To Append To ArrayList Java

Let us see how to append an element to ArrayList in Java. To append an element we can use add() method of the List interface which will add the element to the end of the ArrayList.

import java.util.ArrayList;
import java.util.List;

public class Main {
   public static void main(String[] args) {
      // initializing List
      List<Integer> list = new ArrayList<>();
      list.add(1);
      list.add(2);
      list.add(3);
      System.out.println("List before appending: " + list);

      // append an element
      list.add(9);
      System.out.println("List after appending: " + list);
   }
}

Output:-

List before appending: [1, 2, 3]
List after appending: [1, 2, 3, 9]

Java Append ArrayList To Another ArrayList

Let us see how to append ArrayList to another ArrayList. To append ArrayList to another ArrayList in Java we can take the help of the addAll() method. The addAll() method is used to insert a list into another list.

import java.util.ArrayList;
import java.util.List;

public class Main {
   public static void main(String[] args) {
      List<String> list = new ArrayList<>();
      list.add("Jonny");
      list.add("Annie");
      System.out.println("ArrayList1: " + list);

      List<String> list1 = new ArrayList<String>();
      list1.add("Jhonson");
      list1.add("Joseph");
      System.out.println("ArrayList2: " + list1);

      // append ArrayList to ArrayList
      list.addAll(list1);
      System.out.println("ArrayList1 after appending: " + list);
   }
}

Output:-

ArrayList1: [Jonny, Annie]
ArrayList2: [Jhonson, Joseph]
ArrayList1 after appending: [Jonny, Annie, Jhonson, Joseph]

Java Append ArrayList To Another List

Using the addAll() method of the List interface also can be used to append ArrayList To Another List like Vector, LinkedList, and e.t.c. The below program appends ArrayList to Vector.

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

public class Main {
   public static void main(String[] args) {
      List<String> list = new ArrayList<>();
      list.add("Jonny");
      list.add("Annie");
      list.add("Potter");
      list.add("Temgire");
      list.add("Jay");
      System.out.println("ArrayList: " + list);

      Vector<String> vector = new Vector<String>();
      vector.add("Jhonson");
      vector.add("Joseph");
      vector.add("Petter");
      System.out.println("Vector: " + vector);

      // append ArrayList to vector
      vector.addAll(list);
      System.out.println("Vector after appending: " + list);
   }
}

Output:-

ArrayList: [Jonny, Annie, Potter, Temgire, Jay]
Vector: [Jhonson, Joseph, Petter]
Vector after appending: [Jonny, Annie, Potter, Temgire, Jay]

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *