Skip to content
Advertisement

Generic method to print arrays in java

I am trying to write a generic method printAll which prints an array of integer or character. Here’s the code:

JavaScript

}

Error: Exception in thread “main” java.lang.RuntimeException: Uncompilable source code – Erroneous tree type: <.any>

Advertisement

Answer

printAll(T[] t) will not accept primitive type arrays. You need to pass arrays of the respective wrapper types:

JavaScript

But, you don’t need to frame your own method. Just use the already existing – Arrays.toString() method, which is overloaded for different types of primitive arrays, and Object[] array.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement