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

You asked:

Also what do you gain with the
java.util.Arrays.ArrayList
implementation ?

It is because the Arrays$ArrayList returned by Arrays.asList is just a view on the original array. So when the original array is changed then the view is changed too.

If one would use an real ArrayList then the elements will be copied, and a change on the orignal array would not infuence the ArrayList.

The reasons to do this are quite simple:

  • performance: no need to copy anyting
  • memory efficent: no second array is needed

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)