将数组转换为 ArrayList [英] Convert an array into an ArrayList

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

问题描述

我在 Java 中将数组转换为 ArrayList 时遇到了很多麻烦.这是我现在的数组:

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"持有一系列卡片".这看起来像一个 ArrayList?

"hand" holds an array of "Cards". How this would look like as an 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

另请阅读此http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html

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

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