Java 对象数组
我的脚本有问题 我有一个方法,一个返回卡片,另一个返回名称,另一个返回值,另一个返回套装Java 对象数组,java,list,iterator,Java,List,Iterator,我的脚本有问题 我有一个方法,一个返回卡片,另一个返回名称,另一个返回值,另一个返回套装 public Card pickRandomCard() { while (rand < 6) { System.out.println(deckOfCards.get(rand)); int cardValuePlayer1 = (deckOfCards.get(rand).toInt()); currentTotal1 = cardV
public Card pickRandomCard() {
while (rand < 6) {
System.out.println(deckOfCards.get(rand));
int cardValuePlayer1 = (deckOfCards.get(rand).toInt());
currentTotal1 = cardValuePlayer1;
String name1 = (deckOfCards.get(rand).toString());
nameF = name1;
String suit_1 = (deckOfCards.get(rand).suit());
suit1 = suit_1;
return deckOfCards.get(rand++);
}
return null;
}
public int totalValuePlayer1() {
return currentTotal1;
}
public String name1() {
return nameF;
}
public String suit_1() {
return suit1;
}
public Card()){
而(兰特<6){
System.out.println(deckOfCards.get(rand));
int cardValuePlayer1=(deckOfCards.get(rand.toInt());
currentTotal1=cardValuePlayer1;
字符串名称1=(deckOfCards.get(rand.toString());
nameF=name1;
字符串suit_1=(deckOfCards.get(rand.suit());
suit1=suit_1;
return deckOfCards.get(rand++);
}
返回null;
}
公共整数totalValuePlayer1(){
返回电流总计1;
}
公共字符串名称1(){
返回名称f;
}
公共串诉讼1(){
返回suit1;
}
但是现在,我想创建一个卡片的数组列表,就像一手卡片,所以我的想法是创建一个包含5张卡片的数组列表,但是如果我做了类似的事情
List<Card> hand = new ArrayList<Card>();
Iterator<Card> iter = hand.iterator();
while (iter.hasNext()) {
hand.add(gr.pickRandomCard());
}
List hand=new ArrayList();
迭代器iter=hand.Iterator();
while(iter.hasNext()){
添加(gr.pickRandomCard());
}
我在线程“main”java.lang.IndexOutOfBoundsException中收到异常:索引:2,大小:0
我在另一个类中有这段代码来调用每个方法、套装、卡片值、图像,但我需要创建一个列表来比较卡片,使用下面的代码,不可能逐个比较卡片
ActionListener one = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (gr1.getCounter1() < 5) {
gr1.setCounter1(gr1.getCounter1() + 1);
test1.setIcon(play1a);
pn1.setText(Integer.toString(play2a));
pn5.setText(play3a);
pn50.setText(play4a);
} else {
pn5.setText("No more cards");
}
}
};
arraybtn[1].addActionListener(one);
arraybtn[1].setPreferredSize(new Dimension(120, 20));
play1a = gr.pickRandomCard().getImage();
play2a = gr.totalValuePlayer1();
play3a = gr.name1();
play4a = gr.suit_1();
arraybtn[1].setText(play3a);//change the name of button
ActionListener one=新建ActionListener(){
已执行的公共无效操作(操作事件e){
if(gr1.getCounter1()<5){
gr1.setCounter1(gr1.getCounter1()+1);
test1.setIcon(play1a);
pn1.setText(Integer.toString(play2a));
pn5.setText(play3a);
pn50.setText(play4a);
}否则{
pn5.setText(“不再有卡”);
}
}
};
arraybtn[1].addActionListener(一个);
arraybtn[1].setPreferredSize(新维度(120,20));
play1a=gr.pickRandomCard().getImage();
play2a=组totalValuePlayer1();
play3a=组名称1();
play4a=组套装1();
arraybtn[1].setText(play3a)//更改按钮的名称
我认为问题是我需要创建4个列表,每个列表对应的方法(套装、图像、价值),但我不知道如何才能做到这一点
谢谢你的代码中有一个缺陷:
// this iterates over existing cards
Iterator<Card> iter = hand.iterator();
// but there are no cards yet
while (iter.hasNext() /* this will never return true */ ) {
// so this is never executed
hand.add(gr.pickRandomCard());
}
//这将迭代现有卡
迭代器iter=hand.Iterator();
//但是还没有牌
while(iter.hasNext()/*这将永远不会返回true*/){
//所以这是永远不会执行的
添加(gr.pickRandomCard());
}
请尝试以下方法:
List<Card> hand = new ArrayList<Card>();
for(int i = 0; i < 5; i++){
hand.add(gr.pickRandomCard());
}
List hand=new ArrayList();
对于(int i=0;i<5;i++){
添加(gr.pickRandomCard());
}
您的代码中存在一个缺陷:
// this iterates over existing cards
Iterator<Card> iter = hand.iterator();
// but there are no cards yet
while (iter.hasNext() /* this will never return true */ ) {
// so this is never executed
hand.add(gr.pickRandomCard());
}
//这将迭代现有卡
迭代器iter=hand.Iterator();
//但是还没有牌
while(iter.hasNext()/*这将永远不会返回true*/){
//所以这是永远不会执行的
添加(gr.pickRandomCard());
}
请尝试以下方法:
List<Card> hand = new ArrayList<Card>();
for(int i = 0; i < 5; i++){
hand.add(gr.pickRandomCard());
}
List hand=new ArrayList();
对于(int i=0;i<5;i++){
添加(gr.pickRandomCard());
}
List hand=new ArrayList();
迭代器iter=hand.Iterator();
while(iter.hasNext()){
添加(gr.pickRandomCard());
}
你的“手”是空的,因此你没有元素。这就是为什么你的尺寸是0
您还可以创建一个新类,比如Playa,它具有名称、套装、图像和值属性;
迭代器iter=hand.Iterator();
while(iter.hasNext()){
添加(gr.pickRandomCard());
}
你的“手”是空的,因此你没有元素。这就是为什么你的尺寸是0
此外,您还可以创建一个新类,例如Playa,它具有名称、套装、图像和值属性。根据Sean的回答,您无法迭代手部
数组列表的原因是因为它没有任何项。因此,您需要使用for循环来添加它们。基于Sean的答案,您无法迭代hand
ArrayList的原因是它没有任何项。因此,您需要使用for循环来添加它们
while (iter.hasNext()) {
hand.add(gr.pickRandomCard());
}
iter来自刚刚创建的空列表,因此hasNext()将始终返回false
你提到了西服和手等概念,但已经决定它们应该是ArrayList。你为什么不开设西服和手工课呢。Hand可能包含一些卡片集合和填充它的构造函数或方法。西装应该是一个枚举式的
enum Suit{
CLUBS,
SPADES,
HEARTS,
DIAMONDS
}
如果您以面向对象的方式正确地建模数据,您会发现代码更容易编写
iter来自刚刚创建的空列表,因此hasNext()将始终返回false
你提到了西服和手等概念,但已经决定它们应该是ArrayList。你为什么不开设西服和手工课呢。Hand可能包含一些卡片集合和填充它的构造函数或方法。西装应该是一个枚举式的
enum Suit{
CLUBS,
SPADES,
HEARTS,
DIAMONDS
}
如果您以面向对象的方式正确地建模数据,您会发现代码更容易编写。您的意思可能是Player
Playa
将具有沙子、海洋、棕榈树等属性:-)@Sean不要成为Playa的仇恨者;)你的意思可能是Player
Playa
将具有沙子、海洋、棕榈树等属性:-)@Sean不要成为Playa的仇恨者;)