Convert an Array to an ArrayList in Java
Sometimes you have an array but would rather use an ArrayList. This post talks about the differences between these. It’s not super straightforward to do this conversion but not too hard either. Here’s how you would do it for strings.
public static ArrayList<String> CreateStringList(String ... values) { ArrayList<String> results = new ArrayList<String>(); Collections.addAll(results, values); return results; }
You would call this in this way:
String[] array = new String[] {"abc","def","ghi"}; ArrayList<String> list = CreateStringList(array);
This is essentially the same as what’s done in this post.