You can do something like this
public class A { public static void main (String[] args) { int[] intArray = {1, 2, 3}; String[] strArray = {"1", "2"}; hasSize(Arrays.asList(intArray), 3); hasSize(Arrays.asList(strArray), 2); } public static <T> boolean hasSize(List<T> array, int expectedSize) { return (array.size() == expectedSize); } }
This is not exactly what you want. But you can take advantage of the universal mechanism by passing a list of arrays, and autoboxing will do the job for you.
If this approach is good for you, then good, but if this is not a good approach, unfortunately, the general mechanism does not support primitive types, so you need to override the method for each type of array.
source share