Solved

converting Arraylist to array

Posted on 2002-03-21
7
286 Views
Last Modified: 2010-03-31
How to convert Array List to Array?Can anyone explain with an example?


Thanks in advance
0
Comment
Question by:Delphine_victorus
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 6886254
ArrayList al = new ArrayList();

al.add("hello");
al.add("world");

String[] myArray = al.toArray();

0
 
LVL 18

Expert Comment

by:bobbit31
ID: 6886257
from javadocs:

toArray
public Object[] toArray()
Returns an array containing all of the elements in this list in the correct order.
Specified by:
toArray in interface List
Overrides:
toArray in class AbstractCollection
Returns:
an array containing all of the elements in this list in the correct order.

--------------------------------------------------------------------------------

toArray
public Object[] toArray(Object[] a)
Returns an array containing all of the elements in this list in the correct order. The runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.
If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the collection is set to null. This is useful in determining the length of the list only if the caller knows that the list does not contain any null elements.

Specified by:
toArray in interface List
Overrides:
toArray in class AbstractCollection
Parameters:
a - the array into which the elements of the list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
Returns:
an array containing the elements of the list.
Throws:
ArrayStoreException - if the runtime type of a is not a supertype of the runtime type of every element in this list.
0
 
LVL 9

Accepted Solution

by:
Venci75 earned 10 total points
ID: 6886273
Actually - toArray() returns Object[], which you can't cast to String[]. You should use:

java.util.ArrayList al = new java.util.ArrayList();

al.add("hello");
al.add("world");

Object[] temp = al.toArray();
String myArray[] = new String[temp.length];
System.arraycopy(temp, 0, myArray, 0, temp.length);
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Expert Comment

by:bobbit31
ID: 6886281
the mistakes one makes when they try to rush to be first to comment ;)
0
 

Author Comment

by:Delphine_victorus
ID: 6886296
I tried......
it gives me an error.......
saying..incompatible types


found : java.lang.Object[]
required : java .lang.String[]
0
 

Author Comment

by:Delphine_victorus
ID: 6886306
I tried......
it gives me an error.......
saying..incompatible types


found : java.lang.Object[]
required : java .lang.String[]
0
 
LVL 92

Expert Comment

by:objects
ID: 6886983
Even easier:

java.util.ArrayList al = new java.util.ArrayList();

al.add("hello");
al.add("world");

String myArray[] = (String[]) al.toArray(new String[al.size()]);
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question