JDK-6353471 : (coll) Arrays.asList() does not support primitive arrays
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util:collections
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2005-11-21
  • Updated: 2012-10-08
  • Resolved: 2005-11-21
Related Reports
Relates :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.5.0_05"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_05-b05)
Java HotSpot(TM) Client VM (build 1.5.0_05-b05, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Windows XP

A DESCRIPTION OF THE PROBLEM :
java.util.Arrays.asList() does not support primative arrays...
if the programmer passes a primative array to the asList() method, with J2SE 5.0 ( 1.5.0 ), it will accept that primative array and place it as the internal Object array's first element.
So a call to get(0) with an index of 0 will return an Object that is the actual primative array "object " not the value of that primative arrays first element...
and of course a call to get(1) with an index of 1 will result in an ArrayIndexOutOfBoundsException...
The toArray() method returns an Object [] array whose first element is the primative array that was passed into the asList() method...
and of course the inherited toString() method returns the hashcode too since the only element in the List ( Arrays.ArrayList ) is the primative array "object"...

i had found this bug report against 1.3 which is "Closed, and will not be fixed"...
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4348595
in that report, it is noted that if a primative array was passed, the compiler would generate an error stating that it's looking for an Object [] array and that a primative cannot be passed... but with J2SE 5.0, it apparently can... not sure what the mechanisms that allows this... possibly the boxing/autoboxing ? or the use of generic type array as the parameter for Arrays.ArrayList ?

while it was noted in the bug report that support was not going to be implemented for primative arrays due to performance and triviality reasons
if primative arrays are not going to be supported, then the class should prevent primative arrays from being passes to the asList() method or at least the documentation reflect that passing primative arrays can give unexpected results...

of course there is not issue of using Object [] arrays of any type, like anInteger [] array...



STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
create a primate array i.e. int [] intArray = { 1, 2, 3 };
and then pass the array as an argument to java.util.Arrays.asList() method...
i.e. List list = Arrays.asList(intArray);
then attempt to get element out of that List and the first element is the primative array object held in the list...
int num = list.get(0); // wrong datatype naturally
Integer numInt = ( Integer ) list.get(0); // class cast exception...
int [] temp = ( int [] ) list.get(0); // works
System.out.println( list ); // results in hashcode...

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
the expected result was either an error like
" expected asList(java.lang.Object[]) in java.util.Arrays cannot be
applied to (int[])"
or the primative array passed should be "autoboxed" into an array of it's wrapper type with the corresponding elements values ( as wrapper type holding said value )  matching the primatives array's element values...
ACTUAL -
no error passing the primative array to java.util.Arrays.asList() method
list.get(0) returns the primative array object ( as Object )

ERROR MESSAGES/STACK TRACES THAT OCCUR :
if trying to access a second element thinking that the returned List's number of elements matches the primative array elemnts will result in an error...
i.e. list.get(1); results in this type of errorr...

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
        at java.util.Arrays$ArrayList.get(Unknown Source)
        at PrintArray.main(PrintArray.java:20)


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.util.*;
 
public class PrintArray
{
	public static void main (String [] args)
	{
	 Integer[] integerArray = new Integer[] { new Integer(1), new Integer(2), new Integer(3) };
   List list = Arrays.asList(integerArray);
   System.out.println(" Contents of List: " + list);
   /* using a primative array as an argument to Arrays.asList() method */
	 int [] primativeIntArray = {1,2,3};
	 List intList = Arrays.asList(primativeIntArray);
	 Object[] objectArray = intList.toArray();
	 System.out.println(" Object Array Element:0 " + objectArray[0]);
	 /* determining data type */
	 Object elementObject = intList.get(0);
	 Class elementClass = elementObject.getClass();
	 System.out.println(" List Element: 0 " + intList.get(0));
	 System.out.println(" Data Type for Element: 0 " + elementClass.getSimpleName());
	 int [] temp = (int[]) intList.get(0);
	 System.out.println(" hashcode of int [] array " + temp );
	 System.out.println(" temp int [] array element: 0" + temp[0]);
	 /* trying to access the second element in the List */
	 elementObject = intList.get(1); // throws an ArrayIndexOutOfBoundsException
	 elementClass = elementObject.getClass();
	 System.out.println(" List Element: 1 " + intList.get(1));
	 System.out.println(" Data Type for Element: 1 " + elementClass.getSimpleName());
	 System.out.println(" Displays List " + intList);
	}
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
don't use a primative array type as an arguement to Arrays.asList() method...
if you do, get(0) does not get you the first element that were in the primative array but the array itself...
you can cast the returned object ( as Object ) from get(0) to it primative array type...

as in the previous bug report
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4348595

if the programmer need to create a "List" based on a primative, it would not be that difficult for the programmer to create their own implementation for it...

Comments
EVALUATION Arrays.asList is now a vararg method, and the behavior is as intended: asList(int[] ...) The Java generics implementation does not support non-reference type parameters. This is all standard Java 5.0 stuff.
21-11-2005