Sagewire Logo

returning a generic array

4 Message(s) by 4 Author(s) originally posted in java programming


From: ndac Date:   Wednesday, March 21, 2007
Hello,

assume I've 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<String> x = new X<String>();
...
String[] a = x.toArray();
(or String[] a = (String[])x.toArray();)

what am I doing wrong here?

Regards


From: Lew Date:   Friday, March 23, 2007
wrote in message:
Hello,
assume I've 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<String> x = new X<String>();
...
String[] a = x.toArray();
(or String[] a = (String[])x.toArray();)
what am I doing wrong here?



Arrays and generics do not play well together.

-- Lew


From: Alex Gout Date:   Saturday, March 24, 2007
ndac schreef:
Hello,
assume I've 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<String> x = 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.


From: jupiter Date:   Monday, May 28, 2007
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<String> x = new X<String>();
...
String[] a = x.toArray();
(or String[] a = (String[])x.toArray();)
what am I doing wrong here?



Do not forget: .toArray() must return Object[]. It'd be nice to
be able to cast from Object[] to String[] but you can not.

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've to make
sure that all the objects in obj can be represented by String.

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



Next Message: function as an argument


Blogs related to returning a generic array

Parametric testing show down
@Parameters public static Collection hiearchyValues() { return Arrays.asList(new Object[][] { {Vector.class, new String[] { "java.util.AbstractList", "java.util.AbstractCollection" } }, {String.class, new String[] {} } }); } ...

Ruby Class Tutorial
I’ll leave that as an exercise to the reader return both David Thomas and David Black. As a hint I’ll just say that you can push matched objects into an array. Inheritance Inheritance is one of the three pillars of Object Oriented ...

Java Generics basics
return elementData[index]; } The syntax is same as generic class. Two methods can not have same name and argument types if they have then the compiler will give an error. Generics and Arrays You can not create a array of generic type ...

java faqs
Casting between object references is used to refer to an object by a compatible class, interface, or array type reference. 136. What is the return type of a program's main() method? A program's main() method has a void return type. ...

Developer - Artefact API (updated)
p> * @param artefactType The type of artefact to retrieve, ie "Task" * @return An array of GrailsClasses which may empty by not null * @since 0.5 */ public GrailsClass[] getArtefacts(String artefactType); /** *

Get an artefact ...

Scala for Java programmers - Part 2
object Main { def func(s: String) : boolean = { return s.startsWith("J"); } def print(s: String): unit = { Console.println(s); } def main(args: Array[String]) : unit = { var list = List("hello", "world", "Java"); // pass method ...


Programming | Sports | Autos

copyright 2006
Valid XHTML 1.0 Transitional