package com.mindprod.example;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static java.lang.System.*;
/**
* Demonstrate use of Arrays.asList.
* <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.
*
* @author Roedy Green, Canadian Mind Products
* @version 1.2 2014-05-08 add test with Generics. Warning about use with int[]
* @since 2007
*/
public class TestArraysAsList
{
/**
* number of cards in a deck
*/
private static final int DECKSIZE = 52;
private static void testWithGenerics()
{
out.println( "Testing with Generics" );
String[] s = new String[] { "a", "b", "c" };
List<String> x = Arrays.asList( s );
out.println( x.size() );
out.println( x.getClass() );
out.println( x.get( 0 ) );
x.set( 1, "z" );
out.println( x.get( 1 ) );
out.println( s[ 1 ] );
s[ 2 ] = "q";
out.println( x.get( 2 ) );
List<String> constant = Collections.unmodifiableList( x );
out.println( constant.getClass() );
}
private static void testWithoutGenerics()
{
out.println( "Testing without Generics" );
Integer deck[] = new Integer[ DECKSIZE ];
for ( int i = 0; i < DECKSIZE; i++ )
{
deck[ i ] = i;
}
List list = Arrays.asList( deck );
out.println( list.getClass() );
out.println( deck.length );
out.println( list.size() );
Object elem = list.get( 0 );
out.println( elem.getClass() );
}
/**
* main method.
*
* @param args the command line argument, not used
*/
@SuppressWarnings( { "PrimitiveArrayArgumentToVariableArgMethod" } )
public static void main( String[] args )
{
testWithoutGenerics();
testWithGenerics();
}
}