SoFunction
Updated on 2025-04-08

Detailed explanation of the array conversion method of int[] and ArrayList <> in Java

Preface

In Java,int[]andArrayList<Integer>They are two commonly used data structures. becauseint[]is an array of basic data types, andArrayList<Integer>It is an object collection type. The conversion of the two requires attention to the details of the type conversion. The following is a complete explanation and implementation method.

1. int[] convert to ArrayList

Method 1: Use manual traversal

By traversing the array, add each element toArrayList<Integer>middle.

Code implementation

import ;

public class Main {
    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 5};

        // Convert to ArrayList        ArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;();
        for (int num : array) {
            (num);
        }

        (list); // Output: [1, 2, 3, 4, 5]    }
}

illustrate

  • Manual traversal is suitable for any version of Java.
  • Time complexity: O ( n ) O(n)O(n),nis the length of the array.

Method 2: Use Java 8 Stream

pass()andboxed()Method,int[]Convert toStream<Integer>, collect it againArrayList<Integer>

Code implementation

import ;
import ;
import ;

public class Main {
    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 5};

        // Convert to ArrayList using Stream        ArrayList&lt;Integer&gt; list = (array)
                                        .boxed() // Change to Integer type                                        .collect((ArrayList::new));

        (list); // Output: [1, 2, 3, 4, 5]    }
}

illustrate

  • Suitable for Java 8 and above.
  • useboxed()To basic typeintConvert to packaging typeInteger

2. ArrayList is converted to int[]

Method 1: Use manual traversal

By traversalArrayList<Integer>, add elements toint[]middle.

Code implementation

import ;

public class Main {
    public static void main(String[] args) {
        ArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;();
        (1);
        (2);
        (3);

        // Convert to int[]        int[] array = new int[()];
        for (int i = 0; i &lt; (); i++) {
            array[i] = (i); // Unboxing Integer -> int        }

        ((array)); // Output: [1, 2, 3]    }
}

illustrate

  • Manual traversal is suitable for any version of Java.
  • pass(i)Get elements and unbox them automaticallyint

Method 2: Use Java 8 Stream

pass()WillArrayList<Integer>Convert toint[]

Code implementation

import ;

public class Main {
    public static void main(String[] args) {
        ArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;();
        (1);
        (2);
        (3);

        // Use Stream to convert to int[]        int[] array = ()
                          .mapToInt(Integer::intValue) // Convert to int type                          .toArray();

        ((array)); // Output: [1, 2, 3]    }
}

illustrate

  • Suitable for Java 8 and above.
  • usemapToInt()Method completed fromIntegerarriveintConversion.

3. Comprehensive examples

The following code is implementedint[]andArrayList<Integer>Two-way conversion.

import ;
import ;
import ;

public class Main {
    public static void main(String[] args) {
        // int[] to ArrayList<Integer>        int[] array = {1, 2, 3, 4, 5};
        ArrayList&lt;Integer&gt; list = (array)
                                        .boxed() // Convert to Integer                                        .collect((ArrayList::new));
        ("int[] -&gt; ArrayList&lt;Integer&gt;: " + list);

        // ArrayList<Integer> to int[]        int[] newArray = ()
                             .mapToInt(Integer::intValue) // Convert to int                             .toArray();
        ("ArrayList&lt;Integer&gt; -&gt; int[]: " + (newArray));
    }
}

Running results

int[] -&gt; ArrayList&lt;Integer&gt;: [1, 2, 3, 4, 5]
ArrayList&lt;Integer&gt; -&gt; int[]: [1, 2, 3, 4, 5]

4. Notes: Automatic packing and unboxing:

    • fromintConvert toIntegerIt is automatic packing.
    • fromIntegerConvert tointIt is automatic unboxing.
    • These operations are automatically done by the JVM when traversing manually.
  • null

    • ifArrayList<Integer>Includenull, in the conversion toint[]Will be thrown whenNullPointerException
    • Example:
      ArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;((1, null, 3));
      int[] array = ().mapToInt(Integer::intValue).toArray(); // Throw NullPointerException
  • performance

    • Manual traversal method comparisonStreamThe method is slightly faster and is suitable for performance-sensitive scenarios.
    • StreamThe method code is more concise and is recommended for use in modern Java projects.

5. Method comparison

Change direction method advantage shortcoming
int[] -> ArrayList Manual traversal Simple and efficient The code is slightly longer
() Concise code, modern style Java 8 and above support
ArrayList -> int[] Manual traversal Simple and efficient The code is slightly longer
() Concise code, modern style Java 8 and above support

6. Summary

  • Recommended plan
    • If you are using Java 8 or later, use it firstStream, the code is more concise.
    • For scenarios with high performance requirements, you can choose to manually traverse.
  • Core operations
    • boxed(): Convert the primitive type to the wrapper type.
    • mapToInt(): Convert the wrapper type to the primitive type.

Summarize

This is the article about int[] and ArrayList<> array conversion method in Java. For more related int[] and ArrayList<> array conversion content, please search for my previous articles or continue browsing the related articles below. I hope you will support me in the future!