Solved

converting Arraylist to array

Posted on 2002-03-21
7
257 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
7 Comments
 
LVL 18

Expert Comment

by:bobbit31
Comment Utility
ArrayList al = new ArrayList();

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

String[] myArray = al.toArray();

0
 
LVL 18

Expert Comment

by:bobbit31
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 18

Expert Comment

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

Author Comment

by:Delphine_victorus
Comment Utility
I tried......
it gives me an error.......
saying..incompatible types


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

Author Comment

by:Delphine_victorus
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now