Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How to store arrayList into an array in java?

share|improve this question
3  
Please describe in more detail what you want to achieve. – Joachim Sauer Mar 9 '11 at 10:16
why would you do this? – Tobiask Mar 9 '11 at 10:16

7 Answers

up vote 5 down vote accepted

That depends on what you want:

List<String> list = new ArrayList<String>();
// add items to the list

Now if you want to store the list in an array, you can do one of these:

Object[] arrOfObjects = new Object[]{list};
List<?>[] arrOfLists = new List<?>[]{list};

But if you want the list items in an array, do one of these:

Object[] arrayOfObjects = list.toArray();
String[] arrayOfStrings = list.toArray(new String[list.size()]);

Reference:

share|improve this answer
Better define the arrOfLists as List<String>[], or maybe List<?>[]. – Paŭlo Ebermann Mar 9 '11 at 12:35
@Paŭlo I know, but List<String>[] is a syntax error and List<?> is not a huge gain :-) – Sean Patrick Floyd Mar 9 '11 at 15:22
Ah, right, I forgot the stupid non-generic arrays ... At least, List<?>[] gives no warnings for using the raw type. – Paŭlo Ebermann Mar 9 '11 at 15:36
@Paŭlo ok, added that change, but if you are crazy enough to store a list in an array you probably don't care about compiler warnings either :-) – Sean Patrick Floyd Mar 9 '11 at 16:01

You mean you want to convert an ArrayList to an array?

Object[] array = new Object[list.size()];
array = list.toArray(array);

Choose the appropriate class.

share|improve this answer
List list = getList();
Object[] array = new Object[list.size()];
for (int i = 0; i < list.size(); i++)
{
  array[i] = list.get(i);
}

Or just use List#toArray()

share|improve this answer
List<Foo> fooList = new ArrayList<Foo>();
Foo[] fooArray = fooList.toArray(new Foo[0]);
share|improve this answer
List list = new ArrayList();

list.add("Blobbo");

list.add("Cracked");

list.add("Dumbo");

// Convert a collection to Object[], which can store objects    

Object[] ol = list.toArray();
share|improve this answer

If Type is known (aka not a generics parameter) and you want an Array of Type:

ArrayList<Type> list = ...;
Type[] arr = list.toArray(new Type[list.size()]);

Otherwise

Object[] arr = list.toArray();
share|improve this answer

Try the generic method List.toArray():

List<String> list = Arrays.asList("Foo", "Bar", "Gah");
String array[] = list.toArray(new String[list.size()]);
// array = ["Foo", "Bar", "Gah"]
share|improve this answer
The last line won't compile in Java (while it would be perfectly legal in Groovy). Javac says: Syntax error, insert "AssignmentOperator Expression" to complete Expression – Sean Patrick Floyd Mar 9 '11 at 10:28
@Sean: right, I just threw that in there for demonstration, I'll update the answer to make it clear... – maerics Mar 10 '11 at 19:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.