Can you assign one ArrayList to another in Java?

Can you assign one ArrayList to another in Java?

Java ArrayList copy elements example shows how to copy ArrayList elements to another ArrayList. The example also shows how to copy all elements of ArrayList to another ArrayList using various approaches.

How to copy ArrayList elements to anotherArrayList in Java?

There are several ways to copy ArrayList elements to another ArrayList as given below.

1) Copy ArrayList using the copy method of Collections class

You can copy elements of one ArrayList to another using copy method of the Collections class.

public static <T> void copy(List<? super T> destinationList,List<? super T> sourceList)

This method copies all elements from the source list to the destination list. Index of the destination list elements will be the same after the copy operation.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

package com.javacodeexamples.collections.arraylist;

import java.util.ArrayList;

import java.util.Collections;

public class JavaArrayListCopyExample {

public static void main(String[] args) {

ArrayList<String> aListDays = new ArrayList<String>();

aListDays.add(“Monday”);

aListDays.add(“Tuesday”);

ArrayList<String> aListWeekend = new ArrayList<String>();

aListWeekend.add(“Sat”);

aListWeekend.add(“Sun”);

/*

* To copy elements of one ArrayList to another, use

* copy method of Collections class

*/

//copy aListWeekend to aListDays

Collections.copy(aListDays, aListWeekend);

System.out.println(aListDays);

}

}

Output

As you can see from the output, elements of the aListWeekend are copied to the aListDays at the identical index such that the first element of the source list becomes the first element of destination list and so on.

Note 1: If the destination list is largerthan the source list, all remaining elements of the destination list will be kept as is. i.e. if the source list contains 2 elements and destination contains 5, after the copy, the destination list will still retain its last 3 elements. Consider below given example.

ArrayList<String> aListNumbers1 = new ArrayList<String>();

aListNumbers1.add(“One”);

aListNumbers1.add(“Two”);

aListNumbers1.add(“Three”);

ArrayList<String> aListNumbers2 = new ArrayList<String>();

aListNumbers2.add(“Four”);

//copy aListNumbers2 to aListNumbers1

Collections.copy(aListNumbers1, aListNumbers2);

Output

As you can see from the output, Four is copied at first index in the destination list but it still retained Two and Three elements.

Note 2: If the destination list is not large enough to contain all the elements of the source list, copy method throws IndexOutOfBoundException. Consider below given example.

ArrayList<String> aListNumbers1 = new ArrayList<String>();

aListNumbers1.add(“One”);

ArrayList<String> aListNumbers2 = new ArrayList<String>();

aListNumbers2.add(“One”);

aListNumbers2.add(“Two”);

aListNumbers2.add(“Three”);

Collections.copy(aListNumbers1, aListNumbers2);

Output

Exception in thread “main” java.lang.IndexOutOfBoundsException: Source does not fit in dest

at java.util.Collections.copy(Collections.java:556)

at com.javacodeexamples.collections.arraylist.JavaArrayListCopyExample.main(JavaArrayListCopyExample.java:55)

2) Copy ArrayList using the addAll method

You can use theaddAll method to copy elements of ArrayList to another ArrayList.

public boolean addAll(Collection <? extends E> collection)

The addAll method appends all elements of the specified collection to the end of the ArrayList.

ArrayList<String> aListDays = new ArrayList<String>();

aListDays.add(“Monday”);

aListDays.add(“Tuesday”);

ArrayList<String> aListWeekend = new ArrayList<String>();

aListWeekend.addAll(aListDays);

System.out.println(aListWeekend);

Output

Kindly note that theaddAll method appends the elements of one List to another. So, if the destination List already has elements, the source lists elements will be copied after them.

3) Copy ArrayList using ArrayList constructor

You can use the ArrayList constructor to copy elements from another List.

public ArrayList(Collection<? extends E> collection)

This constructor creates a new ArrayList containing the elements of the specified collection (in the order they are returned from collections Iterator).

ArrayList<String> aListDays = new ArrayList<String>();

aListDays.add(“Monday”);

aListDays.add(“Tuesday”);

ArrayList<String> aListWeekDays = new ArrayList<String>(aListDays);

System.out.println(aListWeekDays);

Output

This example is a part of theJava ArrayList tutorial with examples.

Please let us know your views in the comments section below.

Video liên quan

admin