Archive

Posts Tagged ‘arrays’

Be Careful When Converting Java Arrays to Lists

May 1, 2010 15 comments

Unfortunately not everything that should be trivial actually is. One example is converting Java arrays to lists. Of course, there is Arrays.toList, but using this method carelessly will almost certainly lead to nasty surprises. To see what I mean consider the following program and try to predict its output:

package com.wordpress.mlangc.arrays;

import java.util.Arrays;

public class ArraysToList
{
    public static void main(final String[] args)
    {
        System.out.println(
                Arrays.asList(new String[] { "a", "b" }));
        
        System.out.println(
                Arrays.asList(new Integer[] { 1, 2 }));
        
        System.out.println(
                Arrays.asList(new int[] { 1, 2 }));
        
        System.out.println(
                Arrays.asList(new String[] { "a", "b" }, "c"));
    }
}

As the Javadocs for Arrays.asList are quite vague, I can’t blame you for having some difficulties coming to a conclusion, so here is the answer step by step:

  • Line 9 prints “[a, b]” to our shiny little console which is pretty much what one would expect from a sane API, so we are happy.
  • The same is true for line 12 which results in “[1, 2]”.
  • Line 15 however is different, not only because 15 is not 12, but also because an int is not an Integer, and therefore prints “[[I@39172e08]” to our console, that is not shiny anymore. Instead of a list containing two Integer objects, we got a list containing the array as its sole element.
  • After what we have seen above, it should not take you by surprise that line 18 results in another mess that looks like “[[Ljava.lang.String;@20cf2c80, c]”.

So, what happened? The first two print statements worked as expected, because the Java Language Specification states that calling a method with signature foo(T… t) like foo(new T[] { bar, baz }) is semantically equivalent to foo(bar, baz). In Arrays.asList T is a type parameter, so it has to be an Object, and this is not true for int, but for int[]. Thats why the statement in line 16 is equivalent to

Arrays.asList(new Object[] { new int[] { 1, 2 } })

Last but not least, the statement in line 19 called for trouble from the very beginning. We told the compiler that we want a list containing an array of strings and a string, which is exactly what we got.

So far for the explanation, but there is something else we can learn from that: The real source of confusion is not that varargs feature is badly designed; I would rather say that the opposite is true. The problem is that Arrays.asList violates EffJava2 Item 42, which explains clearly, in fact giving Arrays.asList as a bad example, why you should be quite careful when designing APIs that use Java varargs. I won’t repeat the reasoning of the book here, as you really should read it yourself, but for the sake of completeness I have to point out that the problematic statements from above would have been rejected by the compiler back in the old days of Java 1.4, which was a good thing. We can still use Arrays.asList today, but doing so safely requires us to be aware of the subtillities we are facing. Here are a few rules for converting arrays to lists, that guarantee nothing unexpected happens:

  • If you convert to a list just to convert to a string, use Arrays.toString instead. It does what you want all the time and also works for arrays of primitives.
  • If you want to convert an array of primitives to a list of boxed primitives, take advantage of Apache Commons Lang, which most likely is a dependency of your project already anyway, and use ArrayUtils.toObject like this:

    List<Integer> list = Arrays.asList(ArrayUtils.toObject(new int[] { 1, 2 }));
    

    Note however that lists of boxed primitives should not generally be preferred over arrays containing primitives.

  • If you want to convert an array of object references, use Arrays.asList directly:

     List<String> list = 
               Arrays.asList(new String[] { "a", "b" });
    

    Don’t forget to make sure that the people you work with won’t emulate you carelessly.

Of course, you can also choose to just remember that Arrays.asList might behave unexpectedly and use plain for loops instead, but that clutters your code and comes with a performance penalty.

Categories: Programming Tags: , ,