9

How can I write a method that will accept any array of any type (including primitives) as a parameter?

For instance, I would like both of the following calls to work:

int[] intArray = {1, 2, 3};
String[] strArray = {"1", "2"};

hasSize(intArray, 3);
hasSize(strArray, 2);

The closest I've gotten so far is:

public static <T> boolean hasSize(T[] array, int expectedSize)
{
    return (array.length == expectedSize);
}

...but this doesn't work for primitives.

4
  • make it an array of objects? Commented Nov 23, 2015 at 12:29
  • I was thinking the same, but Object [] won't accept primitives.. Commented Nov 23, 2015 at 12:30
  • Possible duplicate of Passing a string array as a parameter to a function java Commented Nov 23, 2015 at 12:34
  • No, it's not a duplicate. This question is clearly for multiple different types, including primitives. Commented Nov 23, 2015 at 12:37

4 Answers 4

8

A primitive array and an object array don't share are base class except Object.

So the only possibility would be to accept an object and inside the method check if it is an array

public static <T> boolean hasSize(Object x, int expectedSize)
{
    return (x != null) && x.getClass().isArray() ?
        java.lang.reflect.Array.getLength(x) == expectedSize :
        false;
}

Of course this also accepts non-arrays, probably not the solution you want.

For this reason the JDK mostly provides identical methods for object arrays and primitive arrays.

Sign up to request clarification or add additional context in comments.

1 Comment

Fair enough. I'll have to write a whole battery of primitive array methods to make it completely supporting of any array then. That's a shame since the implementation of every method is going to be identical except for the one parameter type.
1

If I recall correctly, you can't create a generic array that you expect to have take primitives, as the primitives themselves are not classes and not available to the Java generics system.

Rather, your incoming array would have to be an Integer[], Double[], etc.

1 Comment

Correction: "have to be an Integer[], Double[], etc." should be "int[], double[], etc". The wrapped Integer, Double types will match against Object[], the problem only exists for the un-wrapped primitive types.
0

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 generic mechanism by passing the array to list, and the autoboxing do the work for you.

If this approach is good for you then ok, but if is not a good approach unfortunately the generic mechanism does not support primitive types, so you have to override the method for each array type.

1 Comment

No, this is useless. It forces the user to call {{Arrays.asList()}}, which if they do that, they may as well call {{Arrays.asList().size()}} and use a straight up int == int comparison without writing a method.
0

As mention above you can't mix primitive array and Object array, you have to use wrapper class . If function is going to except variable arguments then one the way to write that is using vargs notion

public static <T> void process(T...args) {
    System.out.println(Arrays.toString(args));
}

this notion is used in java internal API

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.