I have following simple test case
@Test
public void testArraysAsList() {
    Character[] chars1 = new Character[]{'a', 'b'};
    System.out.println(Arrays.asList(chars1).size());
    char[] chars2 = new char[]{'a', 'b'};
    System.out.println(Arrays.asList(chars2).size());
}
The result is: 2 1
I don't understand Arrays.asList(chars2), why Arrays.asList(char[]) makes a one size list,with the element being char[].
As @Andy explains, generics only work with reference types. That means that List<char> is not allowed (so Arrays.asList cannot return List<char>). Instead Arrays.asList interprets its input as a single object and returns a list with that single element.
    Character[] chars1 = new Character[]{'a', 'b'};
    List<Character> list1 = Arrays.asList(chars1);
    char[] chars2 = new char[]{'a', 'b'};
    List<char[]> list2 = Arrays.asList(chars2);
Compare Arrays.asList(chars2) with this String example (where the input is also is a single element):
    String test = "test";
    List<String> asList = Arrays.asList(test);
Returns a list with size()==1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With