我正在使用ArrayList
包含多个变量的对象。例如:
// pseudo code
class Ticket {
int gameID
double price
int seatnumber
}
我有一个ArrayList
多个Ticket
对象并需要访问它们。我查看了 javadoc,到目前为止我已经想出了
list.get(index).attribute
但是我收到一个编译错误:
cannot find symbol
我的语法有问题吗? 这是我的代码:
public static void printGameMoney(ArrayList games, ArrayList tickets)
{
double total = 0;
double tickMoney = 0;
for (int i=0; i<tickets.size(); i++)
{
double tickMoney = tickets.get(i).priceOfTick;
total = total + tickMoney;
}
}
最佳答案
您的代码是“老派”,您应该使用类型、接口(interface)和新样式的 for 循环:
public static void printGameMoney(final List<Game> games, final List<Ticket> tickets)
{
double total = 0;
for (final Ticket ticket : tickets)
{
final double tickMoney = ticket.getPriceOfTick();
total = total + tickMoney;
}
}
另请注意,此方法很奇怪,因为它不返回任何内容。
关于java - 访问数组列表中对象的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19087144/