java不能使用ArrayList中的对象的方法
这是我创建卡片的地方
package javaapplication21;
公务舱卡{
private String Name = null;
private String Description = null;
private String Type = null;
public Card(int i, int j) {
if (i == 0) {
Type = "Spades";
} else if (i == 1) {
Type = "Diamonds";
} else if (i == 2) {
Type = "Hearts";
} else if (i == 3) {
Type = "Clubs";
}
int Value = j;
if (j == 11) {
Name = "Jack";
} else if (j == 12) {
Name = "Queen";
} else if (j == 13) {
Name = "King";
} else if (j == 1) {
Name = "Ace";
} else if (j == 14) {
Name = "Joker";
} else {
Name = "" + Value;
}
}
公共字符串getDescription(){
Description="Its a"+Name+"of"+Type;
return this.Description;
}
}
package javaapplication21;
import java.util.ArrayList;
public class JavaApplication21 {
public static void main(String[] args) {
ArrayList Deck=new ArrayList<Card>();
for (int i = 0; i < 4; i++) {
for (int j = 1; j < 15; j++) {
new Card(i, j);
Deck.add(new Card(i, j));
}
}
System.out.println(Deck.get(5).getDescription());
}
}
我在尝试使用组的索引5中的卡对象的getDescription时出错(找不到符号)。我是编程新手,但我真的不知道问题出在哪里
# 1 楼答案
类型为
Object
类和Object
类的ArrayList
返回对象的get()
方法没有getDescription()
您应该将其强制转换为
Card
类-(Card)Deck.get(5)
# 2 楼答案
arraylist不知道存储对象的类型。你必须定义类型