1

I am new to Java and trying to learn about collections. When I try to copy one ArrayList to another, I run into exceptions. Please find below the code for the same.

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class ArrayListTest {

    public static void main(String[] args) {

        try(Scanner scanner = new Scanner(System.in) ){
            System.out.println("Enter the number of elements to enter into an array");
            int userCount = scanner.nextInt();
            System.out.println("Enter the number one by one");
            ArrayList<Integer> arrayList = new ArrayList<>();
            ArrayList<Integer> destinationList = new ArrayList<>(arrayList);
            for(int i=0; i<userCount; i++){
                arrayList.add(scanner.nextInt());
            }

            Collections.copy(destinationList, arrayList);           
            //Print elements in the Array List
            for(Integer number: destinationList){
                System.out.println("The numbers are");
                System.out.println(number);
            }

        }

        }
}

Exception: Exception in thread "main" java.lang.IndexOutOfBoundsException: Source does not fit in dest at java.util.Collections.copy(Unknown Source) at ArrayListTest.main(ArrayListTest.java:19)

Please let me know, if I am doing something wrong.

gowthamjs23
  • 342
  • 5
  • 17
  • What is the exception? – Kamesh Jul 21 '17 at 05:22
  • Exception in thread "main" java.lang.IndexOutOfBoundsException: Source does not fit in dest at java.util.Collections.copy(Unknown Source) at ArrayListTest.main(ArrayListTest.java:19) – gowthamjs23 Jul 21 '17 at 05:22
  • This is already posted on stackoverflow. Please refer https://stackoverflow.com/questions/6536094/java-arraylist-copy. Thanks. – bugfix Jul 21 '17 at 05:34
  • Don't use `Collections.copy()` for this; just use the list's constructor which accepts another list. – Tim Biegeleisen Jul 21 '17 at 05:40

2 Answers2

2

i fixing your code, try this :

try (Scanner scanner = new Scanner(System.in)) {
        System.out.println("Enter the number of elements to enter into an array");
        int userCount = scanner.nextInt();
        System.out.println("Enter the number one by one");
        ArrayList<Integer> arrayList = new ArrayList<>();
        for (int i = 0; i < userCount; i++) {
            arrayList.add(scanner.nextInt());
        }

        ArrayList<Integer> destinationList = new ArrayList<>(arrayList);
        // Print elements in the Array List
        for (Integer number : destinationList) {
            System.out.println("The numbers are");
            System.out.println(number);
        }

}

this line copy arrayList into your destinationlist:

ArrayList<Integer> destinationList = new ArrayList<>(arrayList);
Gusti Arya
  • 1,281
  • 3
  • 15
  • 32
0

The copy method needs the size of the destination to be same or greater than list to be copied. By default, the size of arrayList increases as more elements are added to it. You need to check for size of your arraylist and then create the destination list with same or bigger size to avoid this exception.

Rishi Goel
  • 670
  • 4
  • 10