-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?

2
  • What is the easiest way? Read the documentation. Commented Dec 10, 2013 at 7: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 Commented Dec 10, 2013 at 18:45

5 Answers 5

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

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

2 Comments

Link only answers will be deleted soon. Add some description please.
thanks sorry for the inconvenience i have edited y question please check it
1

Try,

Integer[] arr= list.toArray(new Integer[list.size()]);

1 Comment

thanks sorry for the inconvenience i have edited y question please check it
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()]);

Comments

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

Comments

0

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.