Home Tutorial Java Collections Arrays Converting java arrays into list

 
 

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

Related Tags for Converting java arrays into list:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.