转换一个数组转换成一个ArrayList [英] Convert an array into an ArrayList

查看:229
本文介绍了转换一个数组转换成一个ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多的麻烦转向数组到的ArrayList 在Java中。这是我的数组现在:

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];

手持有卡的数组。如何做到这一点的样子为的ArrayList

推荐答案

作为的ArrayList 这条线将是

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

要使用的ArrayList 你必须做的。

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

也阅读了本<一个href=\"http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html\">http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html

这篇关于转换一个数组转换成一个ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆