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<Integer> list = new ArrayList<>(); 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),
n
is 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<Integer> list = (array) .boxed() // Change to Integer type .collect((ArrayList::new)); (list); // Output: [1, 2, 3, 4, 5] } }
illustrate
- Suitable for Java 8 and above.
- use
boxed()
To basic typeint
Convert 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<Integer> list = new ArrayList<>(); (1); (2); (3); // Convert to int[] int[] array = new int[()]; for (int i = 0; i < (); 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<Integer> list = new ArrayList<>(); (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.
- use
mapToInt()
Method completed fromInteger
arriveint
Conversion.
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<Integer> list = (array) .boxed() // Convert to Integer .collect((ArrayList::new)); ("int[] -> ArrayList<Integer>: " + list); // ArrayList<Integer> to int[] int[] newArray = () .mapToInt(Integer::intValue) // Convert to int .toArray(); ("ArrayList<Integer> -> int[]: " + (newArray)); } }
Running results
int[] -> ArrayList<Integer>: [1, 2, 3, 4, 5] ArrayList<Integer> -> int[]: [1, 2, 3, 4, 5]
4. Notes: Automatic packing and unboxing:
- from
int
Convert toInteger
It is automatic packing. - from
Integer
Convert toint
It is automatic unboxing. - These operations are automatically done by the JVM when traversing manually.
- from
-
null:
- if
ArrayList<Integer>
Includenull
, in the conversion toint[]
Will be thrown whenNullPointerException
。 - Example:
ArrayList<Integer> list = new ArrayList<>((1, null, 3)); int[] array = ().mapToInt(Integer::intValue).toArray(); // Throw NullPointerException
- if
-
performance:
- Manual traversal method comparison
Stream
The method is slightly faster and is suitable for performance-sensitive scenarios. -
Stream
The method code is more concise and is recommended for use in modern Java projects.
- Manual traversal method comparison
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 first
Stream
, the code is more concise. - For scenarios with high performance requirements, you can choose to manually traverse.
- If you are using Java 8 or later, use it first
-
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!