Arrays.asList() learning

Arrays.asList() is a [generic] is a [generic] method, the incoming object must be an array of objects

int[] myArray = { 1, 2, 3 };
List myList = Arrays.asList(myArray);System.out .println(myList.size()); //1 System.out .println (myList.get ( 0 )); //Array address value 
System 
. out .println (myList. get 
( 1 ) ) ; / /Error: ArrayIndexOutOfBoundsException

This can be solved using an array of wrapper classes:

Integer[] myArray = { 1, 2, 3 };

or

List<String> name = new ArrayList<>(Arrays.asList("xxx","yyy","zzz"));

Arrays.asList() uses collection modification methods: add(), remove(), clear() will throw an exception

The Arrays.asList() method returns not java.util.ArrayList, but an inner class of java.util.Arrays. This inner class does not implement the modification methods of the collection or does not override these methods.

Leave a Comment

Your email address will not be published. Required fields are marked *