2

Is there anyway I could assign an arrayList to an array in Java?

3 Answers 3

11

Yes, use toArray() in List.

See:

  List a = new ArrayList();
  a.add("foo");
  a.add("bar");
  System.out.println(a); // prints [foo, bar]

  Object[] myArray = a.toArray();
  for (int i = 0; i < myArray.length; i++) {
   System.out.println(myArray[i]); // prints 'foo' and 'bar'
  }

See online, in ideone.

Sign up to request clarification or add additional context in comments.

Comments

2

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

2 Comments

They are different types, so you cannot simply do Object[] ar = myArrayList;
Maybe you're trying to say Object[] array = myArrayList.toArray(new Object[myArrayList.size()]);?
0

Call List.toArray()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.