Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将数组转换为ArrayList_Java_Arrays_List_Arraylist_Blackjack - Fatal编程技术网

Java 将数组转换为ArrayList

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>

在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> 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