Created
October 4, 2023 17:20
-
-
Save thospfuller/d566d3534a86c55c95ef6dcbe7f17add to your computer and use it in GitHub Desktop.
Turn an array into an ArrayList in Java via the ArrayList constructor
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 ()); | |
java.util.List<Integer> exampleArrayList = new java.util.ArrayList<> (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!