Why Doesn't My Varargs Work with ArrayList in Java?

Question

Why doesn't my varargs work with ArrayList in Java?

public void doSomething(int... args) {
    List<Integer> broken = new ArrayList<Integer>(Arrays.asList(args));
}

Answer

In Java, when you use varargs (variable arguments) with primitive types like `int`, the compiler converts them into an array. However, due to type erasure and the specific way `Arrays.asList` works, when you attempt to convert a primitive `int[]` into a `List<Integer>`, the compiler throws an error because it cannot handle the conversion directly.

public void doSomething(int... args) {
    List<Integer> numbers = new ArrayList<>();
    for (int arg : args) {
        numbers.add(arg);
    }
}

Causes

  • Using `int...` creates an array of primitives, not a list of objects.
  • `Arrays.asList` does not directly support primitive arrays; it expects an array of objects.

Solutions

  • Convert the primitive array to an array of `Integer` objects before passing it to `Arrays.asList`.
  • Use a loop to add each element of the `int` array to the `ArrayList`.

Common Mistakes

Mistake: Assuming `Arrays.asList(args)` works directly with primitive varargs.

Solution: Explicitly convert varargs to `Integer` before creating the `List` using a loop.

Mistake: Not recognizing that ArrayList cannot take a primitive array directly.

Solution: Use `Integer[]` instead of `int[]` to utilize `Arrays.asList`.

Helpers

  • Java varargs
  • ArrayList issue Java
  • Arrays.asList with varargs
  • Java List<Integer>

Related Questions

⦿How to Convert a Java int[] Array to a HashSet<Integer>

Learn how to convert a primitive int array to a HashSet of Integers in Java effortlessly and correctly.

⦿How to Fix 'Basic Attribute Type Should Not Be a Container' Error in JPA @Entity Class

Learn how to resolve the Basic attribute type should not be a container error in JPA when declaring a List in your Entity class.

⦿Should You Use @Override Annotation When Implementing Abstract Methods in Java?

Explore whether to use the Override annotation for implementing abstract methods in Java and discover best practices for its usage.

⦿How to Import a Multi-Language Project in IntelliJ IDEA

Learn how to import a multilanguage project in IntelliJ IDEA including Java HTML and JavaScript support.

⦿How to Suppress Java Compiler Warnings About Deprecated APIs?

Learn how to suppress javac warnings related to deprecated APIs and customize compiler options effectively.

⦿How to Configure Log4j to Log Different Log Levels to Different Files for the Same Logger

Learn how to configure Log4j to log INFO and ERROR levels to separate files using the same logger. Stepbystep guide with code snippets.

⦿How to Suppress the "...is internal proprietary API" Warning in Java Compilation?

Learn how to suppress the internal proprietary API warnings in Java using the SuppressWarnings annotation effectively.

⦿Why is BigDecimal.equals Defined to Compare Value and Scale Individually?

Explore the rationale behind BigDecimal.equals comparing value and scale separately instead of adhering to typical equality comparisons.

⦿Why Does a NullPointerException Not Provide a Stack Trace Without a Debugger?

Explore why a NullPointerException lacks a stack trace when a debugger is not attached and how to resolve it using detailed explanations and examples.

⦿Understanding String.format() Behavior with Hexadecimal Numbers in Java

Learn why String.format produces unexpected hex output for negative numbers and how to correctly format hex values in Java.

© Copyright 2025 - CodingTechRoom.com

close