gpt4 book ai didi

java - 合并索引上的两个列表

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

我有两个包含相同类型对象的列表。每个对象都有一个 int、一个字符串和一些其他内容,其中 int 是它的索引。

这是两个列表所持有的对象:

public class ListElement
{
private int index;
private String data;
private String some;
private String other;
private String stuff;

// gets, sets...

}

我想要的是获取列表 A 的索引和“一些其他内容”,并将其字符串“数据”替换为列表 B 中的字符串。它从列表 B 中获取的字符串应该与列表 A 的索引匹配。我希望这样可以理解。

编辑:需要注意的是,对象内部的索引 int 与对象在列表中的实际位置无关。适用于其他动物

最佳答案

更新:您提到 ListElementindex 字段与其在其包含列表中的实际位置没有关系。

您恰好有一个具有随机名称的字段,您想在其上比较两个列表(加入)。此解决方案使用嵌套循环将一个列表中的每个元素与另一个列表中的每个元素进行比较。

for (int iA = 0; iA < listA.size(); iA++) //iterate over listA
{
ListElement currentElement = listA.get(iA);
for (int iB = 0; iB < listB.size(); iB++) //iterate over listB
{
ListElement other = listB.get(iB);
if (other.index == currentElement.index) //compare the elements
{
//do stuff
currentElement.setData(other.getData());
break; //if only want first match
}
}
}

关于java - 合并索引上的两个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28367903/

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