//TestArrayToList.java
//Illustrates the conversion of an array to a list and
//(if you perform the actions suggested in the comments
//at the end) also shows why we should use the generic
//versions of containers such as lists (for safety reasons).
//Note use of the List interface as a type.
//Note also the default output format of a list of values.

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

public class TestArrayToList
{
    public static void main(String args[])
    {
        System.out.println();

        //Create an array of integers and convert it to a list
        Integer integerArray[] =
        {
            2, 4, 6, 8
        };
        List<Integer> integerList = Arrays.asList(integerArray);
        System.out.println("integerList contains these values: "
                           + integerList);
        System.out.println("And its type is: "
                           + integerList.getClass().getName());
        System.out.println();

        //Create an array of strings and convert it to a list
        String stringArray[] =
        {
            "klm", "abc", "xyz", "pqr"
        };
        List<String> stringList = Arrays.asList(stringArray);
        System.out.println
        (
            "stringList contains these values: "
            + stringList
        );
        System.out.println
        (
            "And its type is: "
            + stringList.getClass().getName()
        );
        System.out.println();
    }
}
/*  Output:

    integerList contains these values: [2, 4, 6, 8]
    And its type is: java.util.Arrays$ArrayList

    stringList contains these values: [klm, abc, xyz, pqr]
    And its type is: java.util.Arrays$ArrayList

*/

/*
    Try the following:
    First, insert the following assignment as the last line of code:
    integerList = stringList;
    Then try compiling to show that it does not compile.

    But now ...
    Use the "raw" List type for integerList and stringList.
    Then recompile and run to show that everything still works.
    Next, once again, insert the following assignment as the last line of code:
    integerList = stringList;
    Once more, try compiling to show that this time it does compile.
    Then add the following line of code, compile/run and explain the output:
    System.out.println("The list is: " + integerList);
*/
