Excelent reply. To simplify your code a bit, you might look at something
like:
int j=0;
String[] strings = new String[list.size()];
Iterator i = list.iterator();
while(i.hasNext()) {
strings[j++] = String.valueOf(i.next());
}
String.valueOf() will automatically convert everything (including Strings)
to Strings (if you don't mind "null" for null values).
(*Chris*)
On 11/10/06, Christopher Schultz <[EMAIL PROTECTED]> wrote:
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1
Miro,
1. Please use your real name in your email address when posting to the
list. "temp temp" is a poor moniker.
2. While some of us will be happy to answer "purely" Java questions,
please mark such posts as [OT] (for "off-topic") and apologize
in advance. Otherwise, post to a generic Java mailing list.
In an effort to kill this question before it gets too far, I offer this
response:
temp temp wrote:
> I want contents of java.util.list as string array
>
> Object aObject[]= list.toArray();
> How can I cast this to string []?
Casting is easy. That's just not what you're asking... what you really
want to know is how to convert a List of objects into a String array.
The answer depends on what you already have in your List. For example,
this might work if all your list items are already strings.
String[] strings = (String[])list.toArray(new String[list.size()]);
But if not all your objects are Strings already, you'll get an
ArrayStorageException or something like that.
If you have non-string data in there, you'll have to do something more
intricate:
int j=0;
String[] strings = new String[list.size()];
for(Iterator i=list.iterator(); i.hasNext(); ++j)
{
Object item = i.next();
if(null == item)
strings[j] = null;
else if(item instanceof String)
strings[j] = (String)item;
else
strings[j] = item.toString(); // simple conversion; you might
// require something more exotic
}
Hope that helps,
- -chris
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.5 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFFVP5s9CaO5/Lv0PARAjXjAJ9rB/HEYfQCbCXegh7jFRxDg4KA3QCeN9Fv
dWayxrTmCHEys5Y269bRXco=
=lX7i
-END PGP SIGNATURE-
-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]