Home > Allgemein > Playing with arrays

Playing with arrays

Presenting today: One of “took me two hours” issues. This codes seems to be ok:

1
2
3
Object[] strArr = new String[] { "whatever" };
Object[] newArr = ArrayUtils.addAll(strArr, null);
ArrayUtils.addAll(newArr, new Object[] { Boolean.TRUE });

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.

KategorienAllgemein Tags:
  1. Bisher keine Kommentare
  1. Bisher keine Trackbacks