Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic Type toArray and asList

Tags:

java

generics

I am trying to understand generics and I purposely want to generate a classcastexception, however, i instead get an arraystoreexception on the first attempt.

    static <E> E reduce(List<E> list, Function<E> f, E initVal) {

    E[] snapshot = (E[]) list.toArray();

    Object[] o = snapshot;
    o[0] = new Long(1);

    E result =  initVal;
    for (E e : snapshot)
        result = f.apply(result, e);


    return result;
}


private static final Function<Integer> SUM = new Function<Integer>(){
    public Integer apply(Integer i1, Integer i2) {
        return i1 + i2;
    }
};

public static void main(String[] args) {
    List<Integer> intList = Arrays.asList(2, 7);
    System.out.println(reduce(intList, SUM, 0));
}

On the second attempt.. I correctly get a ClassCastException using this...

public static void main(String[] args) {
    List<Integer> intList1 = Arrays.asList(2, 7);
    List<Integer> intList = new ArrayList<Integer>(intList1);
    System.out.println(reduce(intList, SUM, 0));
}

What is the difference?

like image 895
Tom Avatar asked Aug 04 '11 00:08

Tom


People also ask

What is generic array type?

A generic type is a generic class or interface that is parameterized over types. The following Box class will be modified to demonstrate the concept.

What is array asList?

The asList() method of java. util. Arrays class is used to return a fixed-size list backed by the specified array. This method acts as a bridge between array-based and collection-based APIs, in combination with Collection.

How is array asList () different?

asList method returns a type of ArrayList that is different from java. util. ArrayList. The main difference is that the returned ArrayList only wraps an existing array — it doesn't implement the add and remove methods.

Does array copy asList?

We can use Arrays#asList method to create a List instance from an Array. Before we move ahead, the next snippet shows how to create a list from array using asList method. However, the asList method does not convert array, neither it copies elements.


1 Answers

It appears to me that the List instance produced by Arrays.asList() does not properly implement the contract of the toArray() method. From List.toArray() javadoc:

Note that toArray(new Object[0]) is identical in function to toArray(). 

However note the following test:

public static void main(String... args) {
  System.out.println(Arrays.asList(2, 7).toArray());
  System.out.println(Arrays.asList(2, 7).toArray(new Object[0]));
}

and output:

[Ljava.lang.Integer;@3e25a5
[Ljava.lang.Object;@19821f

Note that according to the javadoc, toArray() should produce an array of type Object[], but Arrays.<E>asList().toArray() instead produces an array of type E[].

The reason you get an ArrayStoreException is because your array is of type Integer[] when it should be of type Object[].

like image 179
JimN Avatar answered Oct 14 '22 05:10

JimN