Hello,
I am writing a method that returns an array as an Object. Initially there is
a single dimension array Object X[]. However, I can insert other arrays as
Objects in X, thus I can end up with something like following:
------------------------
| A | B | C | Null | D |
------------------------
| R |
-----
| S |
-----
Now I have a method called
Object get(int i)
which is supposed to return the object at position i of the array X. So e.g.
if I call get(3) it should return Null. If I call get(2) it should return
the Object[] array at index 2.
Now, here is my code:
public Object get (int i) {
if (list[i] instanceof Object[]) {
Object[] temp;
temp = X[i];
return temp;
}
else
return X[i];
}
But when I compile this I get an error saying:
Error: Incompatible types
found: java.lang.Object
required: java.lang.Object[]
in this line: temp = list[i];
What I don't understand is that, the only reason it would enter that if
condition is when list[i] is an Object array. So then when I am trying to
store the list[i] in another Object array and return is, why does it say it
found an Object instead of an Object[]?
What am I doing wrong?
Thanks.