The Artima Developer Community
Sponsored Link

Java Answers Forum
Converting ArrayList to String Array

3 replies on 1 page. Most recent reply: Jul 22, 2004 5:38 PM by RajeshKr

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 3 replies on 1 page
Srilatha

Posts: 3
Nickname: psr
Registered: Jun, 2004

Converting ArrayList to String Array Posted: Jun 29, 2004 3:53 PM
Reply to this message Reply
Advertisement
Hi,

I'm Trying to convert the ArrayList to String[].
I'm Getting Run time Exception.

Here is My code
ArrayList uList;
String[] sList = (String[])uList.toArray(new String[0]);

I'm getting the java.lang.ArrayStoreException.

Please, any one can help me.
I Appreciate Your help.
Thx in advance.


Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: Converting ArrayList to String Array Posted: Jun 30, 2004 3:18 PM
Reply to this message Reply
I guess when you are populating uList arraylist then all of your elements are not strings. Can you please post the code where you are populating the uList?

vijaya

Posts: 5
Nickname: vijji
Registered: Dec, 2002

Re: Converting ArrayList to String Array Posted: Jul 20, 2004 5:46 AM
Reply to this message Reply
You can do like this...

import java.util.ArrayList;
public class ArrtoStr
{
public static void main(String[] args)
{
ArrayList ar = new ArrayList();
for(int i=0;i<5;i++){
ar.add("Vijaya"+i);//arraylist data
}
Object obj[]=(Object[])ar.toArray();//converting into an object array.
for(int j=0;j<obj.length;j++)
{
System.out.println("string value is "+obj[j].toString());//converting object into string
}

}
}

Thanks & Regards
Vijaya.

RajeshKr

Posts: 1
Nickname: rajeshkr
Registered: Jul, 2004

Re: Converting ArrayList to String Array Posted: Jul 22, 2004 5:38 PM
Reply to this message Reply
> ArrayList uList;
> String[] sList = (String[])uList.toArray(new String[0]);
> ;
> I'm getting the java.lang.ArrayStoreException.


In your above code the "new String[0]" is the problem.
You can try toArray(new String[uList.size()]);

Flat View: This topic has 3 replies on 1 page
Topic: write a java application Previous Topic   Next Topic Topic: calculating easter sunday

Sponsored Links



Google
  Web Artima.com   

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