473,396 Members | 1,918 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,396 software developers and data experts.

returning a generic array

Hello,

assume I have the following:

public interface X<E{
...
E[] toArray();
}
public class XImp<E{
E[] toArray(){
E[] a = (E[])new Object[N];
...
return a;
}
}

using this like this gives me a bad cast exception?
X<Stringx = new X<String>();
....
String[] a = x.toArray();
(or String[] a = (String[])x.toArray();)

what am I doing wrong here?

Regards

Mar 21 '07 #1
3 7286
Lew
ndac wrote:
Hello,

assume I have the following:

public interface X<E{
...
E[] toArray();
}
public class XImp<E{
E[] toArray(){
E[] a = (E[])new Object[N];
...
return a;
}
}

using this like this gives me a bad cast exception?
X<Stringx = new X<String>();
...
String[] a = x.toArray();
(or String[] a = (String[])x.toArray();)

what am I doing wrong here?
Arrays and generics don't play well together.

-- Lew
Mar 24 '07 #2
ndac schreef:
Hello,

assume I have the following:

public interface X<E{
...
E[] toArray();
}
public class XImp<E{
E[] toArray(){
E[] a = (E[])new Object[N];
...
return a;
}
}

using this like this gives me a bad cast exception?
X<Stringx = new X<String>();
...
String[] a = x.toArray();
(or String[] a = (String[])x.toArray();)

what am I doing wrong here?

Regards
You cannot just cast an object array to another type array. It's not
just because you use generics, it's just the way arrays work.
Mar 24 '07 #3

"ndac" <nd**@ndac.comwrote in message
news:46***********************@news.sunsite.dk...
Hello,

assume I have the following:

public interface X<E{
...
E[] toArray();
}
public class XImp<E{
E[] toArray(){
E[] a = (E[])new Object[N];
...
return a;
}
}

using this like this gives me a bad cast exception?
X<Stringx = new X<String>();
...
String[] a = x.toArray();
(or String[] a = (String[])x.toArray();)

what am I doing wrong here?
Don't forget: .toArray() must return Object[]. It would be nice to
be able to cast from Object[] to String[] but you can't.

You can cast the items though after storing them in Object[]
though.

Object[] obj = x.toArray();
String[] str = null;
str[0] = (String)obj[0];

Of course the cast could be wrong at runtime. You have to make
sure that all the objects in obj can be represented by String.

Seems to me that it would be nice to have .toArray() return the
correct type someday. But even if the cast you tried to do would
work, you'd still have the same problem at runtime if one of the
objects was not capable of being represented by String. I don't
think it's a trivial fix.

May 28 '07 #4

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

Similar topics

66
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
10
by: Fraser Ross | last post by:
I need to know the syntax for writing a reference of an array. I haven't seen it done often. I have a class with a member array and I want a member function to return an reference to it. ...
3
by: Faustino Dina | last post by:
Hi, The following code is from an article published in Informit.com at http://www.informit.com/guides/content.asp?g=dotnet&seqNum=142. The problem is the author says it is not a good idea to...
4
by: rsa_net_newbie | last post by:
Hi there, I have a Managed C++ object (in a DLL) which has a method that is defined like ... Generic::List<String^>^ buildList(String^ inParm) Now, when I compile it, I get "warning C4172:...
2
by: =?Utf-8?B?aGVyYmVydA==?= | last post by:
how do I code generic functions to return the next item in an enumeration a) sorted by name, b) sorted by value c) sorted by declaration in a round-robin style ? for example the enum is Enum...
1
by: interX | last post by:
Hi I'm new in VC++ and have a question to generics. I have a generic class, which contains an array of the generic type. This array I can pin and then I would like to get an unmanaged pointer to...
17
by: kleary00 | last post by:
Hi, I am writing a function that needs to return an array of strings and I am having some trouble getting it right. I need some help. Here is what I consider an array of 100 strings: char...
2
by: piter | last post by:
Hi Can generics be returned by the WebMethod? Iam thinking about <List>. Anyways where can i find list of all values that can be actually passed o returned within webservice connection? Thanks...
0
by: jobs | last post by:
re: ObjectDataSource SelectMethod TypeName returning a Dataset? is this really a DAL? Pardon my misguided email on the subject... I'm working in asp.net 2.0 and vb.net 2005. Say I don't...
2
by: SimonDotException | last post by:
I am trying to use reflection in a property of a base type to inspect the properties of an instance of a type which is derived from that base type, when the properties can themselves be instances of...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.