The Artima Developer Community
Sponsored Link

Java Answers Forum
Implement Collection toArray

7 replies on 1 page. Most recent reply: Jun 17, 2003 8:00 AM by stuart skinner

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 7 replies on 1 page
CharlesS

Posts: 4
Nickname: csant
Registered: Jun, 2003

Implement Collection toArray Posted: Jun 16, 2003 2:19 PM
Reply to this message Reply
Advertisement
I am writing a class that implements the Collection interface.
I have to, of course, implement the method
public Object[] toArray(Object[] dest){}

However I could get an array of type Class1 or Class2 or .....
So in the event that dest is too small to load in my collection, how do I create an array through reflection?
Do I even need to do this?
-CSant


Jaycee

Posts: 26
Nickname: jaycee
Registered: Apr, 2003

Re: Implement Collection toArray Posted: Jun 16, 2003 2:28 PM
Reply to this message Reply
Create your input array with same number of elements as your collection.

Vector strings = new Vector();
strings.add("first");
strings.add("second");
strings.add("third");
 
String[] dest = (String[])strings.toArray(new String[strings.size()]);

CharlesS

Posts: 4
Nickname: csant
Registered: Jun, 2003

Re: Implement Collection toArray Posted: Jun 16, 2003 2:34 PM
Reply to this message Reply
Thankx for your reply!

But what if sometimes dest is of type String and other times it is of type Integer and other times it is of type ComboBox?

Vector objects = new Vector();
objects.add("first");
objects.add("second");
objects.add("third");
or
objects.add(1);
objects.add(2);
objects.add(3);
or
objects.add(new ComboBox());
objects.add(new ComboBox());
objects.add(new ComboBox());

Without HardCoding how do I do the next line????
String[] dest = (String[])objects.toArray(new String[strings.size()]);


The key is that I have several objects that derive from a BaseCollection and I do not want to hardcode?

Jaycee

Posts: 26
Nickname: jaycee
Registered: Apr, 2003

Re: Implement Collection toArray Posted: Jun 16, 2003 3:26 PM
Reply to this message Reply
Then make your dest array an Object[], and cast it when needed.

Object[] dest = objects.toArray();
 
//if objects contained Strings, do this.
String[] destString = (String[])dest;
 
//if object contained Integers, do this.
Integer[] destInteger = (Integer[])dest;

Jaycee

Posts: 26
Nickname: jaycee
Registered: Apr, 2003

Re: Implement Collection toArray Posted: Jun 16, 2003 3:29 PM
Reply to this message Reply
oops, no can do. I'll follow with another post if someone else doesn't come up with the answer.

Jaycee

Posts: 26
Nickname: jaycee
Registered: Apr, 2003

Re: Implement Collection toArray Posted: Jun 16, 2003 3:52 PM
Reply to this message Reply
This is a bit hacky, but it'll work as long as all the elements in your collection are the same type as the first one.

	Vector objects = new Vector();
	objects.add("hello");
	
	Object[] objectArray = null;
	if(objects!=null && objects.size()>0){
		objectArray = (Object[])Array.newInstance(objects.get(0).getClass(), objects.size());
	}
	
	Object[] dest = objects.toArray(objectArray);
	
	//if objects contained Strings, cast to String[].
	String[] destString = (String[])dest;
	
	//if objects contained ComboBox, cast to ComboBox[].
	//ComboBox[] destString = (ComboBox[])dest;	
 

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Implement Collection toArray Posted: Jun 17, 2003 12:49 AM
Reply to this message Reply
From looking at the documentation of toArray(object[]) in the Collection interface, you will want to return an array of Objects which match the type of the array passed in. That means if you have a heterogeneous collection of objects, only those which match that type should be returned in the array. You will probably use instanceof to determine this. If you want to follow the spec closely, you need to have a look at the documentation regarding whether to use the array passed in or create a new one (it depends on whether it can accommodate all the objects you want to return). You might first count all the objects in your collection that are an instance of the element type of the array passed in, then either build a new array of that size, or use the one passed in. Then you could go through your collection and insert those items into the array (if this two-pass method is too slow for your needs, then the next step will be to do the performance tuning).

stuart skinner

Posts: 1
Nickname: stuski
Registered: Jun, 2003

Re: Implement Collection toArray Posted: Jun 17, 2003 8:00 AM
Reply to this message Reply
You can use the following code to dynamically create an array based on the type of the parameter array : This gets round the problem of not having a first element whose type can be queried. The built in collections (or at least ArrayList) don't seem to do the type check when populating the array.

Object[] toArray(Object[] arg)
{
if(arg.length != size)
{
arg = (Object[])Array.newInstance(
arg.getClass().getComponentType(), size);
}
// populate the array
 
return arg;
}
 

Flat View: This topic has 7 replies on 1 page
Topic: JTable - How To Enable Cell Editor Component Previous Topic   Next Topic Topic: i need help...

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use