Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Monday, November 19, 2007

Use of java Arrays.asList(...) for Collections view of array

Make a list. The Arrays class has some useful methods for working with arrays, but there are a lot more if you make your array look like a List. The Collections class has many useful methods, eg, shuffle, reverse, max, .... See Collections Class.

import java.util.*;
. . .
List listabc = Arrays.asList(abc);
// The Collections methods can now be use with listabc.

View of underlying array. The list that is created is not a regular ArrayList. It is based only on the original array, so you can't do things like adding elements to the list because this would cause reallocation of a larger underlying array.

Uses the underlying array. One of the really nice things about this is that you can still use the underlying array, which means that creating this list doesn't prevent you from taking advantage of the efficiency of array access for example.

Objects only. The java.util.Arrays.asList(...) method only works for arrays of object types. If you try to use it with an array of primitives, you will get a compilation error.

No comments:

Post a Comment

Archives