Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thospfuller/d566d3534a86c55c95ef6dcbe7f17add to your computer and use it in GitHub Desktop.
Save thospfuller/d566d3534a86c55c95ef6dcbe7f17add to your computer and use it in GitHub Desktop.
Turn an array into an ArrayList in Java via the ArrayList constructor
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);
}
}
@thospfuller
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment