/* * [TestArraysAsList.java] * * Summary: Demonstrate use of Arrays.asList. * * Copyright: (c) 2009-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.1 2007-10-04 minor changes * 1.2 2014-05-08 add test with Generics. Warning about use with int[] */ 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. *

* int[] becomes the one and only element of a List of 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. *

* 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 x = Arrays.asList( s ); out.println( x.size() ); // Find out what class the List generated is // prints java.util.Arrays$ArrayList out.println( x.getClass() ); // Verify List is modifiable. out.println( x.get( 0 ) ); x.set( 1, "z" ); // prints "z" out.println( x.get( 1 ) ); // changes to List change underlying array. // prints "z" out.println( s[ 1 ] ); // direct changes to underlying array are reflected in the List s[ 2 ] = "q"; // prints "q" out.println( x.get( 2 ) ); List constant = Collections.unmodifiableList( x ); // prints java.util.Collections.UnmodifiableRandomAccessList out.println( constant.getClass() ); } private static void testWithoutGenerics() { // does not work with int[]. You must use Integer[]. // It would be nice if asList threw an exception if you fed it an array of ( primitives ) 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() ); // output is: // class java.util.Arrays$ArrayList // as expected if you look at the asList source code. out.println( deck.length ); out.println( list.size() ); // output is: // 52 // 52 Object elem = list.get( 0 ); out.println( elem.getClass() ); // output is: // class java.lang.Integer. } /** * main method. * * @param args the command line argument, not used */ @SuppressWarnings( { "PrimitiveArrayArgumentToVariableArgMethod" } ) public static void main( String[] args ) { testWithoutGenerics(); testWithGenerics(); } }