Java 访问数组列表中对象的元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19087144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accessing an element of an object in an arraylist
提问by Casey Conner
I am working with an ArrayList
of objects that contain multiple variables. For example:
我正在处理ArrayList
包含多个变量的对象。例如:
// pseudo code
class Ticket {
int gameID
double price
int seatnumber
}
and I have an ArrayList
of multiple Ticket
objects and need to access them. I've looked at the javadoc and so far I have come up with
我有ArrayList
多个Ticket
对象,需要访问它们。我看过javadoc,到目前为止我已经想出了
list.get(index).attribute
However I get a compile-error saying:
但是我收到一个编译错误说:
cannot find symbol
找不到标志
am I doing something wrong with the syntax? here is my code:
我在语法上做错了吗?这是我的代码:
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;
}
}
采纳答案by Ashok kumar
If attribute is really one of your class member then please use as follow.
如果属性确实是您的班级成员之一,请按如下方式使用。
((Ticket) list.get(index)).attribute;
回答by Piotr Chojnacki
Use this instead:
改用这个:
public static void printGameMoney(ArrayList games, ArrayList<Ticket> tickets)
{
double total = 0;
double tickMoney = 0;
for (int i=0; i<tickets.size(); i++)
{
double tickMoney = tickets.get(i).priceOfTick;
total = total + tickMoney;
}
}
Basically, the change leads to ArrayList<Ticket>
instead of simple ArrayList
. This way you tell the compiler that objects inside your ArrayList
are of Ticket
type, therefore they have attributes you specified (e.g. priceOfTick
).
基本上,更改导致ArrayList<Ticket>
而不是简单的ArrayList
. 通过这种方式,您可以告诉编译器您内部的对象ArrayList
属于Ticket
类型,因此它们具有您指定的属性(例如priceOfTick
)。
Same goes for games
, so if you have Game
class, you should use ArrayList<Game> games
.
也一样games
,所以如果你有Game
课,你应该使用ArrayList<Game> games
.
回答by musical_coder
Start by putting semicolons after each field declaration:
首先在每个字段声明后放置分号:
class Ticket {
int gameID;
double price;
int seatnumber;
}
Also, show the exact code you're using instead of list.get(index).attribute
.
此外,请显示您正在使用的确切代码,而不是list.get(index).attribute
.
回答by Hlib Babii
List<Object> list = new ArrayList<Object>();
((Ticket)list.get(x)).attribute;
回答by Suresh Atta
You have to access like this ,list.get(i).price;
not list.price.get(i);
你必须像这样访问,list.get(i).price;
而不是list.price.get(i);
for (int i=0; i<tickets.size(); i++)
{
double tickMoney = list.get(i).price;
total = total + tickMoney;
}
Here problem is that with your deceleration of ArrayList
这里的问题是你的减速 ArrayList
If you declare like ArrayList<Ticket>
you need not to cast there while getting. Other wise you need a cast there. More over use for each loop.
如果你声明ArrayList<Ticket>
你不需要在获取时投射到那里。否则你需要一个演员。每个循环的过度使用。
回答by Suresh Atta
You code is "old-school", you should use typed-types, interfaces and new style for-loop:
您的代码是“老派”,您应该使用类型化类型、接口和新样式的 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;
}
}
Also note this method is strange as it doesn't return anything.
还要注意这个方法很奇怪,因为它不返回任何东西。