Last active
November 16, 2023 16:38
-
-
Save thospfuller/e7a0fc34e84d28c11989822d7f1f85c8 to your computer and use it in GitHub Desktop.
How To Convert An Array To An ArrayList In Java using the ArrayList addAll method (example two).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Main { | |
public static void main(String[] args) { | |
Integer[] numbers = {1, 2, 3, 4, 5}; | |
java.util.List<Integer> exampleList = java.util.Arrays.asList (numbers); | |
System.out.println ("exampleList.class.name: " + exampleList.getClass ().getName ()); | |
exampleList.forEach (item -> System.out.println ("item: " + item)); | |
int initialCapacity = exampleList.size (); | |
java.util.List<Integer> exampleArrayList = new java.util.ArrayList<> (initialCapacity); | |
exampleArrayList.addAll (exampleList); | |
System.out.println ("exampleArrayList.class.name: " + exampleArrayList.getClass ().getName ()); | |
exampleArrayList.forEach (item -> System.out.println ("item: " + item)); | |
System.exit (0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See the following article: How To Convert An Array To An ArrayList In Java!
Run this in your browser using Try It Online.