How to add elements in List when used Arrays.asList()
Arrays.asList(),generates a list which is actually backed by an array and it is an array which is morphed as a list. You can use it as a list but you can't do certain operations on it such as adding new elements. So the best option is to pass it to a constructor of another list obj like this:
List<T> list = new ArrayList<T>(Arrays.asList(...));
Create a new ArrayList
using the constructor:
List<String> list = new ArrayList<String>(Arrays.asList("a", "b"));
One way is to construct a new ArrayList
:
List<T> list = new ArrayList<T>(Arrays.asList(...));
Having done that, you can modify list
as you please.