Arraylist without remove object

前端 未结 2 894
耶瑟儿~
耶瑟儿~ 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<Object> newObjectList = Collections.unmodifiableList(new ArrayList<Object>());
    

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

    0 讨论(0)
  • 2021-01-29 17:35

    Use the method newObjectList.get(index) instead

    Documentation

    0 讨论(0)
提交回复
热议问题