【发布时间】:2014-09-01 18:39:24
【问题描述】:
我现在正在使用基本集合,我在使用它们 ArrayList 方法时遇到了问题,例如 .get(int),因为这将返回整个对象,在我的例子中是 Name、Age、Phone、Salary...等等,但我只想在所需位置获取对象的名称。
列表已经加载并且方法已经完成,我目前正在使用这个:
System.out.println("Desired Position?");
pos = Integer.parseInt(in.nextLine());
for(Employee i : list)
{
if(list.indexOf(i)==pos) // <- Was out of ideas here
{
System.out.println(i.getName());
}
}
所以...问题是:我输入位置,for-each 循环应该检查列表,如果列表的索引与我想要的匹配,它会打印名称。
谢谢你,对我几乎看不懂的英语感到抱歉
【问题讨论】:
-
试试
list.get(pos).getName()。 ArrayLists 提供快速的按索引查找。 -
我的天...就这么简单?现在我感觉很糟糕......