Why does Arrays.asList() return its own ArrayList implementation

后端 未结 6 487
小蘑菇
小蘑菇 2020-12-02 12:55

I recently found out that there are actually 2 different ArrayList implementations in Java (better late than never I guess...).

So I was wondering why d

6条回答
  •  失恋的感觉
    2020-12-02 13:59

    They are two different classes with different behaviours.

    The list returned when you called Arrays.asList is a thin wrapper over the array, not a copy. The list returned is fixed size: attempting to call add will throw an UnsupportedOperationException exception.

    The java.util.ArrayList on the other hand keeps its own internal copy of the data and is variable sized.

提交回复
热议问题