Playing with arrays
Presenting today: One of “took me two hours” issues. This codes seems to be ok:
1 2 3 |
But executing it leads to the following exception:
Exception in thread "main" java.lang.ArrayStoreException at java.lang.System.arraycopy(Native Method) at org.apache.commons.lang.ArrayUtils.addAll(ArrayUtils.java:2972)
This is because ArrayUtils.addAll takes as type for the returned array the componentType of the first array. Although strArr is defined as Object[], strArr.getClass().getComponentType() returns String, resulting in a String-array.