Arraylist without remove object

前端 未结 2 896
耶瑟儿~
耶瑟儿~ 2021-01-29 16:40

I have an Arraylist, for example

ArrayList newObjectList = new ArrayList<>();

I want to use an object from

2条回答
  •  借酒劲吻你
    2021-01-29 17:32

    You can disable the remove method using the Collections.unmodifiableList() construct:

    List newObjectList = Collections.unmodifiableList(new ArrayList());
    
    
    

    This will throw an UnsupportedOperationException when trying to add or remove an object.

    提交回复
    热议问题