-5

I need to convert an array list of object type into a normal arrays.

ArrayList<object> list = new ArrayList<Object>();
list.add(4);
list.add(56);
list.add("two");

What is the easiest way to do this?

how can we change an existing array to an arraylist?

   String st[]=new  String[];
    Integer in[]=new Integer[];

how can i convert this array into an array list of Object type so i can have both this arrays in one arraylist?

Lijo
  • 6,498
  • 5
  • 49
  • 60
  • What is the easiest way? Read the [documentation](http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#toArray%28T%5b%5d%29). – Justin Dec 10 '13 at 07:18
  • sorry for the inconvenience actually my question was not correct.in my old project there i used some normal arrays string and integer arrays i want to merge all thisnormal arrays into an arraylist – Lijo Dec 10 '13 at 18:45

5 Answers5

1

Try,

Integer[] arr= list.toArray(new Integer[list.size()]);
Masudul
  • 21,823
  • 5
  • 43
  • 58
1

Suppose arrlist is an ArrayList. To convert it into array,try the follwing code.

Integer list[] = new Integer[arrlist.size()]; //arrlist is an ArrayList
list = arrlist.toArray(list2);

FOr more detailed example try this tutorial :

toArray method usage

Nishant Lakhara
  • 2,295
  • 4
  • 23
  • 46
  • [Link only answers will be deleted soon](http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers). Add some description please. – Suresh Atta Dec 10 '13 at 07:21
  • thanks sorry for the inconvenience i have edited y question please check it – Lijo Dec 10 '13 at 18:43
1

Try this:

import org.apache.commons.lang.ArrayUtils;
int[] intArray = ArrayUtils.toPrimitive(list.toArray(new Integer[0]))

This approach will make two complete copies of the sequence: one Integer[] created by toArray, and one int[] created inside toPrimitive

Linga
  • 10,379
  • 10
  • 52
  • 104
1

You can use toArray()

    ArrayList<Integer>  list=new ArrayList<Integer>();
    list.add(4);
    list.add(56);

    Integer[] arr=list.toArray(new Integer[list.size()]);
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

arraylist can be converted into array using toArray() mehod in java.

Check http://coderspoint.com/index.php?topic=7.msg10#msg10