package com.mindprod.example;

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

/**
 * Explore Arrays.asList(int[]) backing feature.
 * <p/>
 * When you use Arrays.asList to create a fixed size List from an array, changes to the List are reflected in the
 * backing array.
 * <p/>
 * composed with IntelliJ IDEA
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0, 2007-10-03
 */
public class TestArraysAsListBacking
    {
    // ------------------------------ FIELDS ------------------------------

    private static final int DECKSIZE = 52;

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

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

        for ( int i = 0; i < DECKSIZE; i++ )
            {
            // autobox
            deck[ i ] = i;
            }
        List<Integer> 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:
        // 52
        // as expected

        Object elem = list.get( 0 );
        System.out.println( elem.getClass() );
        // class java.lang.Integer
        // as you would expect

        // test if backing array store truly tracks as advertised.
        list.set( 0, -1 );
        System.out.println( list.get( 0 ) );
        // output is:
        // -1
        // as expected

        System.out.println( deck[ 0 ] );
        // output is:
        // -1
        // as advertised!

        // try growing the List improperly.
        list.add( -2 );
        // Throws UnsupportedOperationException
        }
    }