JDK-4348595 : missing Arrays.asList() for arrays of primitive types
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.util:collections
  • Affected Version: 1.3.0
  • Priority: P4
  • Status: Closed
  • Resolution: Won't Fix
  • OS: linux
  • CPU: x86
  • Submitted: 2000-06-27
  • Updated: 2021-03-03
  • Resolved: 2000-06-28
Related Reports
Relates :  
Description

Name: stC104175			Date: 06/27/2000


java version "1.3.0beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0beta-b07)
Java HotSpot(TM) Client VM (build 1.3.0beta-b04, mixed mode)


public class AsListBug {
    public static void main (String[] args) {
        System.out.println (java.util.Arrays.asList (new int [] { 1, 2, 3}));
    }
}

AsListBug.java:3: asList(java.lang.Object[]) in java.util.Arrays cannot be
applied to (int[])
        System.out.println (java.util.Arrays.asList (new int [] { 1, 2, 3}));
                                            ^
1 error
(Review ID: 106584) 
======================================================================

Comments
EVALUATION We made a concious decision NOT to put this functionality into the Collections framework, as its performance would be unacceptable, due to the cost of boxing and unboxing the primitives. That said, it is truly trivial to implement this functionality. In fact, I use in my Collections presentation, because it fits on a single slide! (It really is easy to do collections implementations atop the "skeletal" AbstractXxxxx classes.) Here's a working implementation: // List adapter for primitive int array public static List asList(final int[] a) { return new AbstractList() { public Object get(int i) { return new Integer(a[i]); } public Object set(int i, Object o) { int oldVal = a[i]; a[i] = ((Integer)o).intValue(); return new Integer(oldVal); } public int size() { return a.length; } }; } Cool, huh? joshua.bloch@Eng 2000-06-28
28-06-2000