我有一个名为 StudentData 的类。 StudentData 类具有 id(唯一)、名称、地址等参数。现在我有两个 StudentData 类的 ArrayList,一个包含 10 个项目的 ArrayList,另一个包含 50 个项目的 ArrayList。 现在我的问题是,有没有更好的方法来找出 50 项列表中 10 项的个别位置?
我是这样写的,如有错误请指正
ArrayList<Integer> allId = new ArrayList<>();
outerLoop:
for (int i = 0; i < totalList.getSmallList().size(); i++) {
for (int position = 0; position < totalList.getBigList().size(); position++) {
if (totalList.getSmallList().get(i).getID() == totalList.getBigList().get(position).getID()) {
allId.add(totalList.getBigList().get(position).getID());
continue outerLoop;
}
}
}
最佳答案
如果我没有误会你的话。您想获取小列表中匹配项的(bigList 的)索引,并将它们添加到单独的 allIdd;
ArrayList<Integer> allId = new ArrayList<>();
outerLoop:
for (int i = 0; i < totalList.getSmallList().size(); i++) {
for (int position = 0; position < totalList.getBigList().size(); position++) {
if (totalList.getSmallList().get(i).getID() == totalList.getBigList().get(position).getID()) {
allId.add(posotion);
continue outerLoop;
}
}
}
编辑:
来自您的评论:
将 bigList
添加到 listAdapter
上面将为您提供要突出显示的内容
关于java - 如何在父自定义对象数组列表中获取自定义对象子列表中的单个项目位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48652161/