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/e7a0fc34e84d28c11989822d7f1f85c8 to your computer and use it in GitHub Desktop.
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).
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);
}
}
@thospfuller
Copy link
Author

thospfuller commented Sep 29, 2023

See the following article: How To Convert An Array To An ArrayList In Java!

Run this in your browser using Try It Online.

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