Thursday 19 December 2013

How to convert int[] into List in Java?

public List<Integer> asList(final int[] is)
    {
            return new AbstractList<Integer>() {
                    public Integer get(int i) { return is[i]; }
                    public int size() { return is.length; }
            };
    }
The smallest piece of code would be
public List<Integer> myWork(int[] array) {
        return Arrays.asList(ArrayUtils.toObject(array));
}
where ArrayUtils comes from commons-lang :)

No comments: