Using jdb to evaluate 'java.util.Arrays.asList(array)' shows inconsistent behaviour. (1) Java runtime says: Arrays.asList(array) : [testArrays$1@cac736f, testArrays$2@5e265ba4] Arrays.asList(array).size() : 2 (2) Jdb says: eval java.util.Arrays.asList(array); java.util.Arrays.asList(array); = "[[Ljava.awt.event.MouseListener;@156643d4]" eval java.util.Arrays.asList(array).size(); java.util.Arrays.asList(array).size(); = 1 TO REPRODUCE: (1) src ------- % more testArrays.java import java.awt.event.MouseAdapter; import java.util.Arrays; import java.util.List; public class testArrays { public static void main(String[] args) { MouseAdapter mouseAdapter1 = new MouseAdapter(){}; MouseAdapter mouseAdapter2 = new MouseAdapter(){}; MouseAdapter[] array = new MouseAdapter[]{mouseAdapter1, mouseAdapter2}; List<MouseAdapter> list = Arrays.asList(array); System.out.println("list : " + list); System.out.println("list.size() : " + list.size()); System.out.println("Arrays.asList(array) : " + Arrays.asList(array)); System.out.println("Arrays.asList(array).size() : " + Arrays.asList(array).size()); // breakpoint here, evaluate Arrays.asList(array).size() } } (2) compile and run ------------------- % /jdk1.8.0_40/bin/javac -g testArrays.java % /jdk1.8.0_40/bin/java testArrays list : [testArrays$1@15db9742, testArrays$2@6d06d69c] list.size() : 2 Arrays.asList(array) : [testArrays$1@15db9742, testArrays$2@6d06d69c] Arrays.asList(array).size() : 2 % (3) debug --------- % /jdk1.8.0_40/bin/jdb testArrays Initializing jdb ... > stop at testArrays:16 Deferring breakpoint testArrays:16. It will be set after the class is loaded. > run run testArrays Set uncaught java.lang.Throwable Set deferred uncaught java.lang.Throwable > VM Started: Set deferred breakpoint testArrays:16 list : [testArrays$1@cac736f, testArrays$2@5e265ba4] list.size() : 2 Arrays.asList(array) : [testArrays$1@cac736f, testArrays$2@5e265ba4] Breakpoint hit: "thread=main", testArrays.main(), line=16 bci=120 16 System.out.println("Arrays.asList(array).size() : " + Arrays.asList(array).size()); main[1] eval list list = "[testArrays$1@cac736f, testArrays$2@5e265ba4]" main[1] eval list.size() list.size() = 2 main[1] eval java.util.Arrays.asList(array) java.util.Arrays.asList(array) = "[[Ljava.awt.event.MouseListener;@156643d4]" main[1] eval java.util.Arrays.asList(array).size() java.util.Arrays.asList(array).size() = 1 main[1] exit Arrays.asList(array).size() : 2 % Workaround ---------- The following code works as expected: substitute the following lines MouseListener[] array = new MouseListener[]{mouseAdapter1, mouseAdapter2}; List<MouseListener> list = Arrays.asList(array); by MouseAdapter[] array = new MouseAdapter[]{mouseAdapter1, mouseAdapter2}; List<MouseAdapter> list = Arrays.asList(array);
|