Java 将数组转换为ArrayList
在Java中将数组转换为Java 将数组转换为ArrayList,java,arrays,list,arraylist,blackjack,Java,Arrays,List,Arraylist,Blackjack,在Java中将数组转换为ArrayList时,我遇到了很多麻烦。这是我现在的阵列: Card[] hand = new Card[2]; “手”持有一系列“牌”。作为一个ArrayList,它的外观如何?List List=new ArrayList(Arrays.asList(hand)); List<Card> list = new ArrayList<Card>(Arrays.asList(hand)); 这将为您提供一个列表 List<Card>
ArrayList
时,我遇到了很多麻烦。这是我现在的阵列:
Card[] hand = new Card[2];
“手”持有一系列“牌”。作为一个ArrayList
,它的外观如何?List List=new ArrayList(Arrays.asList(hand));
List<Card> list = new ArrayList<Card>(Arrays.asList(hand));
这将为您提供一个列表
List<Card> cardsList = Arrays.asList(hand);
List cardsList=Arrays.asList(手动);
如果你想要一个arraylist,你可以这样做
ArrayList<Card> cardsList = new ArrayList<Card>(Arrays.asList(hand));
arraylistcardslist=newarraylist(Arrays.asList(hand));
声明列表(并使用空arraylist初始化)
迭代元素:
for(Card card : cardList){
System.out.println(card);
}
作为一个
ArrayList
行
import java.util.ArrayList;
...
ArrayList<Card> hand = new ArrayList<Card>();
同时阅读此您是否阅读过任何参考信息,例如?没有!这将提供一个对象,该对象充当底层对象的
列表
包装器。与真实的ArrayList
不同,生成的列表的大小不可调整,并且尝试。将元素添加到列表的末尾将导致不支持操作异常
。谢谢!最后一个简短的问题。。如何将arraylist接受为方法?现在我有:public void getHandValue(ArrayListhand){…}ArrayList在我的主参数中。只有单词“ArrayList”出现错误。您应该能够拥有一个方法签名void getHandValue(ArrayList hand)
调用该方法时,问题更可能出现。
for(Card card : cardList){
System.out.println(card);
}
import java.util.ArrayList;
...
ArrayList<Card> hand = new ArrayList<Card>();
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