473,403 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

How to return an array as an Object?


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.
Jul 17 '05 #1
2 18649
"Asad Khan" <as*******@hotmail.com> wrote in message news:<22******************@news04.bloor.is.net.cab le.rogers.com>...
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];

}
Error is here: You are returning X[i] but X contains arrays (as u said u can
insert arrays.) So u shud return Object[] not Object.
Means: get(int i) shud have following prototype:

Object[] get(int i).
I hope it solves your problem
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.

Jul 17 '05 #2
The compiler is not complaining about the return, it is complaining
about the assignment.

public Object get (int i) {

if (list[i] instanceof Object[]) {
Object[] temp;

// X[i] is an Object
// Now that you have verified it is an array
// you need to cast it to an array like this:
temp = (Object[])X[i];
return temp;
}
else
return X[i];

}
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Andrew Poulos | last post by:
If I'm searching for an occurance of a value in a multi-dimensional array how can I get it's index returned as an array, if found? For example, if: foo = new Array(); foo = , 5, , 9, 10]; ...
8
by: M. Moennigmann | last post by:
Dear all: I would like to write a function that opens a file, reads and stores data into an 2d array, and passes that array back to the caller (=main). The size of the array is not known before...
0
by: JL_327 | last post by:
No one on any of the other boards seem to know In VB.Net this late binding works fine Dim Version As Integer Dim Revision As Integer Dim Wnum As Integer Dim o As System.Array =...
1
by: Pradeep Kumar | last post by:
I have a scenario where I am writing a property(using get and set) have to return a member of an object stored in an array of objects! The class user has to pass an index based on which a object i's ...
7
by: nafri | last post by:
hello all, I want to create a function that returns the first element of the Array that is input to it. However, the Input Array can be an Array of points, double, or anyother type, which means...
4
by: yxq | last post by:
In VB2005. Dim a() As Object = {10, 4} Dim b() As Byte = {10, 4} MessageBox.Show(a.Equals(b)) Why return False, but not Ture? Thank you
19
by: Andrew Gentile | last post by:
Hello, I have been working on a program where I need to have a function return an array. I found out that C doesn't do this, so now I am trying to get the function to return a pointer to an...
20
by: Andrew Morton | last post by:
Is it possible to have two function declarations which take the same parameters but return different types depending on how the function is used? function f(x) as string ' return a string end...
3
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
Hello, Newsgroupians: I have a simple question. I have a function that I want to return an array. I want the array to be other objects that were instantiated in the function itself. What's the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.