java - Make a list and initialize it to include all the values of an Enum -
i have enum direction
, want make temporary list of values of enum , able remove or add values list.
public enum direction { top, right, bottom, left } list<direction> directions = arrays.aslist(direction.values()); directions.remove(0); // error directions.add(direction.bottom); // error
with current code, when remove element @ 0, or if add element, run time exception java.lang.unsupportedoperationexception
. suppose way list immutable ? how can ?
arrays#aslist
returns list
doesn't support add
or remove
elements since wraps array. instead, create new arraylist
, pass result of arrays#aslist
parameter:
list<direction> directions = new arraylist<>(arrays.aslist(direction.values()));
Comments
Post a Comment