Add Integer array to ArrayList Java

Java Collections.addAll: Add Array to ArrayListAdd arrays to ArrayLists with the Collections.addAll method. See common errors in appending arrays.

Collections.addAll. An array has many elements. These can be added to an ArrayList. With Collections.addAll we can add an array of elements to an ArrayList.

Some limitations. With addAll, we must have element types that match. An Integer ArrayList is incompatible with an int array. Often we must use a for-loop to add an array.

First example. This program uses a String array and an ArrayList of Strings. It combines a one-element ArrayList with a String array of 3 elements.

Collections.addAll This method receives 2 arguments: the collection [ArrayList] we are adding to, and the values we are adding.

Java program that uses Collections.addAll

import java.util.ArrayList; import java.util.Collections; public class Program { public static void main[String[] args] { String[] values = { "cat", "dog", "bird" }; // Create a one-element ArrayList. ArrayListlist = new ArrayList[]; list.add["elephant"]; System.out.println[list]; // Add all elements to the ArrayList from an array. Collections.addAll[list, values]; // Display our result. System.out.println[list]; } }[elephant] [elephant, cat, dog, bird]
Integer, int conversion. Here we add an int array to an ArrayList with Integer elements. We use a for-loop, as the array cannot be directly added.

Result We add all three elements from "sizes" to the ArrayList. We do not need an index value in this loop.

Java program that uses Integer ArrayList, int array

import java.util.ArrayList; public class Program { public static void main[String[] args] { // An int array. int[] sizes = { 100, 200, 300 }; // Create an Integer ArrayList. ArrayListlist = new ArrayList[]; list.add[500]; System.out.println[list]; // Loop over our array and add each element separately. for [int element : sizes] { list.add[element]; } // The final list. System.out.println[list]; } }[500] [500, 100, 200, 300]
Collections.addAll error. An int array cannot added to an ArrayList of Integers. An int is not the same thing as an Integeran Integer is an object that contains an int.

Java program that shows Integer, int error

import java.util.ArrayList; import java.util.Collections; public class Program { public static void main[String[] args] { // Has int elements. int[] sizes = { 100, 200, 300 }; // This has Integer elements. ArrayListlist = new ArrayList[]; // Cannot add an int array to Integer ArrayList. Collections.addAll[list, sizes]; } }Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method addAll[Collection, T...] in the type Collections is not applicable for the arguments [ArrayList, int[]] at Program.main[Program.java:14]

Casting issue. A cast does not help with the problems of Collections.addAll. We cannot cast an array of ints to Integers. Here is the compilation problem.

Java program that shows casting error

import java.util.ArrayList; import java.util.Collections; public class Program { public static void main[String[] args] { int[] sizes = { 100, 200, 300 }; ArrayListlist = new ArrayList[]; // This cast does not work. Collections.addAll[list, [Integer[]] sizes]; } }Exception in thread "main" java.lang.Error: Unresolved compilation problem: Cannot cast from int[] to Integer[] at Program.main[Program.java:14]
Benchmark. Does the addAll method have a performance advantage over a for-loop that calls add? I tested this in a benchmark. I measured both constructs.

Version 1 This version of the code uses Collections.addAll to add an array to an ArrayList.

Version 2 Here we use a for-loop and the add[] method on the ArrayList. It has 2 nested loops.

Result I found that using addAll on a short String array was consistently faster.

Java program that benchmarks Collections.addAll and add

import java.util.Collections; import java.util.ArrayList; public class Program { public static void main[String[] args] { String[] elements = { "cat", "dog", "fish" }; long t1 = System.currentTimeMillis[]; // Version 1: use addAll method. for [int i = 0; i < 10000000; i++] { ArrayListlist = new ArrayList[]; Collections.addAll[list, elements]; if [list.size[] != 3] { return; } } long t2 = System.currentTimeMillis[]; // Version 2: use for-loop and add. for [int i = 0; i < 10000000; i++] { ArrayListlist = new ArrayList[]; for [String element : elements] { list.add[element]; } if [list.size[] != 3] { return; } } long t3 = System.currentTimeMillis[]; // ... Result. System.out.println[t2 - t1]; System.out.println[t3 - t2]; } }423 ms, Collections.addAll 463 ms, ArrayList add

Some advantages. There are benefits to Collections.addAll: it reduces lines of code and avoids loops. It is probably faster than a for-loop.

However, incompatible arrays [where int does not match Integer] are not supported. Often to add arrays to ArrayLists, a for-loop is needed.

Video liên quan

Chủ Đề