Switch to full style
Java Collections classes examples .
Post a reply

Convert normal array to a list

Mon Oct 22, 2012 12:33 pm

Convert array to a list object using asList function
Code:

import java
.util.Arrays;
import java.util.List;

public class ArraysWork {

    public static void main(String[] args) {
         
        
// Strings array
        String cityStrings[] = {"NewYork", "Toronto", "Delhi", "Cairo"};
        // Integers array
        Integer numbers[] = {4, 3, 11};

        // List references
        List listStrings = null;
        List listIntegers = null;

        // Convert strings array to list object
        listStrings = Arrays.asList(cityStrings);
       
        System
.out.println("Size of the strings list: "+listStrings.size());
        System.out.println("Value at index 1->"+ listStrings.get(1));
        System.out.println("Calling toString function :"+listStrings);

        // Convert numbers array to list object
         listIntegers = Arrays.asList(numbers);
        System.out.println("Size of the numbers list: " + listIntegers.size());
        System.out.println("Value at index 1->"+listIntegers.get(1));
        System.out.println("Calling toString function :"+ listIntegers);

    }
}
 


The output is :

Code:
Size of the strings list: 4
Value at index 1->Toronto
Calling toString function :[NewYork, Toronto, Delhi, Cairo]
Size of the numbers list: 3
Value at index 1->3
Calling toString function :[4, 3, 11]




Post a reply
  Related Posts  to : Convert normal array to a list
 Convert TreeSet content to array     -  
 Removing elements from array list with the iterator     -  
 Normal Distrubtion     -  
 Array difference for associate array     -  
 compare an array with another array?     -  
 How to Convert WMV to AVI on Mac     -  
 convert hexadecimal to decimal     -  
 Convert outputstream to inputstream     -  
 convert to binary number     -