Share on Google+Share on Google+

Converting java arrays into list

Advertisement
Learn How java arrays is converted into list.
  • Arrays can be converted by the asList() method of the Arrays class.
  • asList() It converts the object array into the fixed sized list


Example

import java.util.*;
public class array4 {

public static void main(String[] args) {
        String ar[]={"aaa","bbb","ccc","ddd"};
        Integer  ar1[]={111,222,333,444};
        List list=new ArrayList();
        List list1=new ArrayList();
        list=Arrays.asList(ar);
        list1=Arrays.asList(ar1);
        System.out.println(list.size());
        System.out.println(list.get(2));
        System.out.println(list);
        System.out.println("------------------\n"+list1.size());
        System.out.println(list1.get(2));
        System.out.println(list1);
    }
}

Output
4
ccc
[aaa, bbb, ccc, ddd]
------------------
4
333
[111, 222, 333, 444]

Advertisements

Posted on: October 22, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.