3

I wrote a function that would take variable arguments as object.

When I passed in an array of ints of size 1 eg {9}, it treated args[0] as and int array[] than an int so the valueOf did not produce 9.

But If passed in and array of 2 or more ints eg {9,11} then it treated args[0] as 9 and args[1] as 11. Why does it behave differently.

Note it is being written for Android.

protected String[] whereArgs(Object...args) {
    String[] argsStrings = new String[args.length];
    for (int i = 0; i < args.length; i++) {
        if (args[i] instanceof String){
            argsStrings[i] = (String)args[i];
        } else {
            argsStrings[i] = String.valueOf(args[i]);   
        }
    }
    return argsStrings;
}

EDIT Just had a look again I was actually passing them differently in the two ints scenario, one by one and not in an array, sorry.

Why doesn't it split the method(Object...args) into an array of objects when I pass in an array of ints, like what happens with method(int...args)

So now to get the string value I have to individually cast the type of array eg. for int[], double[]

if (args[0] instanceof int[]){
    argsStrings[0] =  String.valueOf(((int[])args[0])[0]);

Is there a way to write it for any type of object as this causes a crash

argsStrings[0] = String.valueOf(((Object[])args[0])[0]);

java.lang.ClassCastException: int[] cannot be cast to java.lang.Object[]

8
  • 1
    How did you pass the array of size 1? Commented Jun 16, 2013 at 8:34
  • 2
    When I call whereArgs(array) where array is an int[], then args inside whereArgs is an array containing the passed array as args[0] regardless of how long array is. What version of Java are you using? Commented Jun 16, 2013 at 8:42
  • 1
    Post the full code including the call to this method. Commented Jun 16, 2013 at 8:45
  • 1
    Just to make sure, did you pass your 'array' of 2 int's in a similar fashion as your array of 1 int or did you just call whereArgs(9, 11)? Commented Jun 16, 2013 at 8:46
  • 1
    Because an array is an Object in itself so the compiler wouldn't know if you actually meant to pass in array objects or the values inside the array. Commented Jun 16, 2013 at 9:23

1 Answer 1

2

If you want to pass an array and treat each item as a separate item of the 'args' parameter of your method.. you need to cast your array to an Object[]. e.g: If you pass in your array of integers like below it will do what you want.

whereArgs((Object[])(new Integer[]{1, 2}))

The reason for this is when the source is compiled var-arg methods are actually replaced by an array. all places where the method is being called is converted to an array. If you want to pass an array so that each item becomes a separate argument.. then you need to use the correct array type. in your scenario this will be Object[]. This lets the compiler know that it can leave the method call as it is (without putting the arguments inside a new object[])

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.