package com.mindprod.example;

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

/**
 * Demonstrate Arrays.asList( int[] ) gotcha.
 * <p/>
 * See the goofy thing happening with Arrays.asList( int[] ).
 * <p/>
 * int[] becomes the one and only element of a List of <int[]> objects!! Arrays.asList is advertised to support the
 * original array as a backing array so that when you change the List (actually an ArrayList) produced by asList, the
 * original array is also supposed to change.
 * <p/>
 * However, if you attempt to do Arrays.asList( int[] ), this won't work. There is no invisible backing int[] or
 * Integer[] created either. All I can say is don't feed an int[] to asList. Use Arrays.asList(Integer[]) boxed
 * manually.
 * <p/>
 * composed with IntelliJ IDEA
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.1, 2007-10-04
 */
public class TestArraysAsList
    {
// ------------------------------ FIELDS ------------------------------

    /**
     * number of cards in a deck
     */
    private static final int DECKSIZE = 52;

// --------------------------- main() method ---------------------------

    /**
     * main method.
     *
     * @param args the command line argument, not used
     */
    @SuppressWarnings( { "PrimitiveArrayArgumentToVariableArgMethod" } )
    public static void main( String[] args )
        {
        int deck[] = new int[DECKSIZE];

        for ( int i = 0; i < DECKSIZE; i++ )
            {
            deck[ i ] = i;
            }

        List list = Arrays.asList( deck );
        System.out.println( list.getClass() );
        // output is:
        // class java.util.Arrays$ArrayList
        // as expected if you look at the asList source code.

        System.out.println( list.size() );
        // output is:
        // 1
        // not 52!!

        Object elem = list.get( 0 );
        System.out.println( elem.getClass() );
        // output is:
        // class [I
        // ie. int[], not int nor Integer as you might expect.
        }
    }