473,654 Members | 3,108 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Objec t
required: java.lang.Objec t[]
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 18664
"Asad Khan" <as*******@hotm ail.com> wrote in message news:<22******* ***********@new s04.bloor.is.ne t.cable.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.Objec t
required: java.lang.Objec t[]
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
2928
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]; Array.prototype.findValue = function(val) { // blah }
8
3264
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 opening to the file. I fail to write a function that allocates memory for a 2d array and returns it to main. I was trying to pass a pointer to the array back to main, but main cannot access the data. The simplified code (no opening of file, but...
0
1520
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 = System.Array.CreateInstance (GetType(Single), 1)
1
2482
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 member is returned from the array. How will the property be declared in C#?
7
1802
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 the return type of the function depends on the type of the objects the Input array holds. I can have the return type as Object, but it has problems like late binding and more importantly the client has to always Cast it. For example. class...
4
1720
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
1939
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 array. It seems easy, but I keep running into the same problem. The function appears to execute properly, but when I print out the array values, only the first one is correct. The other are all wrong. A snippet of code is below. Please take a...
20
8976
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 function function f(x) as string() ' return an array of strings end function
3
1691
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 correct way to return the array? Here's what I would like to happen... public object MyFunc() { object obj1 = SomeOtherFunc1();
0
8376
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8290
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8815
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8708
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8594
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5622
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1596
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.