Convert an array into an ArrayList [duplicate]

2019-01-31 05:12发布

This question already has an answer here:

I'm having a lot of trouble turning an array into an ArrayList in Java. This is my array right now:

Card[] hand = new Card[2];

"hand" holds an array of "Cards". How this would look like as an ArrayList?

5条回答
祖国的老花朵
2楼-- · 2019-01-31 05:44

This will give you a list.

List<Card> cardsList = Arrays.asList(hand);

If you want an arraylist, you can do

ArrayList<Card> cardsList = new ArrayList<Card>(Arrays.asList(hand));
查看更多
虎瘦雄心在
3楼-- · 2019-01-31 05:55
List<Card> list = new ArrayList<Card>(Arrays.asList(hand));
查看更多
Anthone
4楼-- · 2019-01-31 05:59

If You Can, Use Guava

It's worth pointing out the Guava way, which greatly simplifies these shenanigans:

Usage

For an Immutable List

Use the ImmutableList class and its of() and copyOf() factory methods (elements can't be null):

List<String> il = ImmutableList.of("string", "elements");  // from varargs
List<String> il = ImmutableList.copyOf(aStringArray);      // from array

For A Mutable List

Use the Lists class and its newArrayList() factory methods:

List<String> l1 = Lists.newArrayList(anotherListOrCollection);   // from collection
List<String> l2 = Lists.newArrayList(aStringArray);              // from array
List<String> l3 = Lists.newArrayList("or", "string", elements"); // from varargs

Please also note the similar methods for other data structures in other classes, for instance in Sets.

Why Guava?

The main attraction could be to reduce the clutter due to generics for type-safety, as the use of the Guava factory methods allow the types to be inferred most of the time. However, this argument holds less water since Java 7 arrived with the new diamond operator.

But it's not the only reason (and Java 7 isn't everywhere yet): the shorthand syntax is also very handy, and the methods initializers, as seen above, allow to write more expressive code. You do in one Guava call what takes 2 with the current Java Collections.


If You Can't...

For an Immutable List

Use the JDK's Arrays.asList() and Collections.unmodifiableList() factory methods:

List<String> l1 = Arrays.asList(anArrayOfElements);
List<String> l2 = Arrays.asList("element1", "element2");

Note that:

  • The returned type for asList() says ArrayList, but it's not java.util.ArrayList. It's an inner type, which mimicks ArrayList but actually directly references the past array and forbids some modifications.
  • unmodifiableList() further locks down changes to the returned view of the original list.

See the next step if you need a mutable list.

For a Mutable List

Same as above, but wrapped with an actual java.util.ArrayList:

List<String> l1  = new ArrayList<String>(Arrays.asList(array));    // Java 1.5 to 1.6
List<String> l1b = new ArrayList<>(Arrays.asList(array));          // Java 1.7+
List<String> l2  = new ArrayList<String>(Arrays.asList("a", "b")); // Java 1.5 to 1.6
List<String> l2b = new ArrayList<>(Arrays.asList("a", "b"));       // Java 1.7+

For Educational Purposes: The Good ol' Manual Way

// for Java 1.5+
static <T> List<T> arrayToList(final T[] array) {
  final List<T> l = new ArrayList<T>(array.length);

  for (final T s : array) {
    l.add(s);
  }
  return (l);
}

// for Java < 1.5 (no generics, no compile-time type-safety, boo!)
static List arrayToList(final Object[] array) {
  final List l = new ArrayList(array.length);

  for (int i = 0; i < array.length; i++) {
    l.add(array[i]);
  }
  return (l);
}
查看更多
Luminary・发光体
5楼-- · 2019-01-31 06:01

declaring the list (and initializing it with an empty arraylist)

List<Card> cardList = new ArrayList<Card>();

adding an element:

Card card;
cardList.add(card);

iterating over elements:

for(Card card : cardList){
    System.out.println(card);
}
查看更多
放荡不羁爱自由
6楼-- · 2019-01-31 06:02

As an ArrayList that line would be

import java.util.ArrayList;
...
ArrayList<Card> hand = new ArrayList<Card>();

To use the ArrayList you have do

hand.get(i); //gets the element at position i 
hand.add(obj); //adds the obj to the end of the list
hand.remove(i); //removes the element at position i
hand.add(i, obj); //adds the obj at the specified index
hand.set(i, obj); //overwrites the object at i with the new obj

Also read this http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html

查看更多
登录 后发表回答