我的代码中有一个列表
List result = dm.findByCustomQuery(uc,query);
我想从结果中获取某个值,特别是当索引为 0 时。
有没有一种方法可以使用迭代器并获取索引为 0 的列表的值。
最佳答案
我不知道你为什么要使用迭代器来获取第一个元素,无论如何,这就是你可以这样做的:
Iterator<String> it = result.iterator();
boolean first = true;
while(it.hasNext()) {
if (first) {
System.out.println("The first element is: "+it.next() );
first=false;
} else {
System.out.println("The next element is: "+it.next() );
}
}
关于java - 使用迭代器时获取特定索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32333705/