Home Tutorial Java Collections Arrays Converting java arrays into list

 
 

Converting java arrays into list
Posted on: October 22, 2009 at 12:00 AM
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]
Advertisement

Related Tags for Converting java arrays into list:


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.

Posted on: October 22, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com