Machine-translated by Google. Questions are authored in English.
Top

Discussion

What Arrays.asList produces when handed a single int[] - which of the following is true?

  • A.An empty list, because a primitive array cannot supply elements.
  • B.A compile-time error, because asList requires a reference array.
  • C.A list with one element, the array itself.
  • D.A list of Integer values, each element boxed automatically.

Answer: C

asList is a varargs generic method, and a type argument cannot be primitive, so the int[] is treated as one object rather than a sequence of values. The result is a List<int[]> of size 1. An Integer[] behaves as expected, which makes the difference easy to miss.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.