gpt4 book ai didi

java - 使用迭代器时获取特定索引

转载 作者:行者123 更新时间:2023-12-01 11:11:18 26 4
gpt4 key购买 nike

我的代码中有一个列表

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com