Convert ArrayList to Array
This is a basic Java conversion stuff. I want to convert an ArrayList to a normal Array.
Here is a sample I did and I would like to share it out to anyone who found its useful. 😉
The test driver…
..... List arrayList_ = new ArrayList(); arrayList_.add("alpha"); arrayList_.add(new String("beta")); arrayList_.add(new Integer(3)); arrayList_.add(new Float(4.00)); arrayList_.add(new Boolean(true)); ..... Object[] array_ = arrayListToArray(arrayList_); for (int i=0; i<array_.length; i++) { System.out.println("[" + i + "] " + array_[i]); } // $>[0] alpha // $>[1] beta // $>[2] 3 // $>[3] 4.0 // $>[4] true .....
The conversion happen here…
private Object[] arrayListToArray(List arrayList) { Object[] array = new Object[arrayList.size()]; arrayList.toArray(array); return array; }
Leave a Reply