Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Lets say I pass my arguments into my function like this.

Seems the Object outside arguments knows it's a Object[] array.
But the Object[] inside it are not Object Array's but plain Object's

Object[] arguments = {
    new Object[] {"command", "value"},  
    new Object[] {"List_test", Arrays.asList("1", "2", "3", "4")},
    new Object[] {"single command"}
};
myFunc(arguments);

public void myFunc(Object[] args) {
    if(args == null || args.length == 0) {
        System.out.println("Error!");
        return; 
    }

    //How I do this below?
    for(Object objArray : args) { //this one is good.
        if(objArray == null) continue;
            //objArray = (Object[]) objArray; //can't figure this out.
            for(Object eachObject : objArray) { //this don't work.
                System.out.println("eachObject reference = " + eachObject);
            }
    }

    for(Object[] objArray : args) { //this don't work.
        if(objArray == null) continue;
            for(Object eachObject : objArray) { //this don't work.
                System.out.println("eachObject reference = " + eachObject);
            }
    }
}

I tried this in Java Console

> for(Object[] a : arguments) { System.out.println(a); }

Got this error

Static Error: Bad types in assignment: from Object to Object[]

This looks much worse.

Object[][] arguments = {
    { new Object[]{"command", "value"}},  
    { new Object[]{"List_test", Arrays.asList("1", "2", "3", "4")}},
    { new Object[]{"single command"}}
};
share|improve this question

3 Answers 3

up vote 2 down vote accepted

As you are declaring the arguements as OBJect[] so the compiler knows the arguements has some Object as elements. But it doesn't know if the element objects are again Object arrays or not. So you are not allowed to do these.

But if you are sure that the elements will be only Object arrays you can cast to Object[] like following

            for(Object objArray : args) { 
                if(objArray == null) continue;
                    Object[] ob = (Object[])objArray ;
                    for(Object eachObject : ob) { 
                        System.out.println("eachObject reference = " + eachObject);
                    }
            }
share|improve this answer

Your variable is declared as

Object[] arguments ...

In other words, it's an array of Object. Regardless of what the elements are inside it, the compiler can only guarantee that each element is at least an Object.

That's why you can do

for(Object objArray : args) { //this one is good.

but not

for(Object[] objArray : args) { //this don't work.

since the Object[] referenced by args might contain something that isn't an Object[].

For example, you might have had

Object[] arguments = {
    new Object[] {"command", "value"},  
    new Object[] {"List_test", Arrays.asList("1", "2", "3", "4")},
    new Object[] {"single command"},
    new Foo(),
    new Bar()
};

The compiler can't know what will be in the Object[] at run time.

share|improve this answer
    
How do I make it trust me? since I know it's only going to be Object[]'s doesn't it use something like instanceof to figure out if it's wrong? –  SSpoke Mar 3 at 6:46
    
@SSpoke Declare your variable as an Object[][] and instantiate it accordingly. No, you can't force the enhanced for loop to use instanceof. –  Sotirios Delimanolis Mar 3 at 6:47
    
Oh wow so all I needed is to use Object[][] –  SSpoke Mar 3 at 6:49
    
@SSpoke In this case, probably. But that might not be appropriate elsewhere. Consider some sort of encapsulation for your objects. –  Sotirios Delimanolis Mar 3 at 6:49
    
Haha I just got over-excited there is no reason to actually use Object[][] the solutions StinePike and Keppil posted work wonderfully. Just tested.. the Object[][] solution would just make the loop look better but the arguments will have lots of extra brackets in them. Ya... I don't want to create a class for the arguments better like this could be anything.. that's how i like it. –  SSpoke Mar 3 at 7:01

You cant change an existing variable and give it a new type. You can however cast it and assign it to a new variable of the type you want:

for(Object obj : args) { //this one is good.
    if (obj == null) continue;
    Object[] objArray = (Object[]) obj; //Assigning to new variable instead
    for(Object eachObject : objArray) { //Now this works!
        System.out.println("eachObject reference = " + eachObject);
    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.