gpt4 book ai didi

java - 根据排序从 Arraylist 中删除对象

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

我有一个包含 4 个对象的列表,每个对象有 4 个属性。我想删除具有最大第四个属性的对象。

这是我所做的。

double temp = 0.0; 
List<type> list1=new ArrayList<>(); //this list has 4 objects

List<Double> attribute1List = new ArrayList<>(); //list of attribute1
for(int i = 0; i < list1.size(); i++) {
attribute1List.add(Double.parseDouble(list1.get(i).getattribute1()));
}

for (int i = 0; i < attribute1List.size(); i++) {
if (attribute1List.get(i) > temp){
temp = attribute1List.get(i); //temp is the greatest
}
}

因为温度给了我最好的。那么,现在如何获取 list1 中最大的对象的索引,以便我可以从 list1 中删除特定对象。

最佳答案

您可以通过执行以下操作来简化此操作

double temp = 0.0; 
List<type>list1=new ArrayList<>();//this list has 4 objects

type biggest = null;
for (type t : list1)
if((Double.parseDouble(t.getattribute1()) > temp){
biggest = t;
temp = t.getattribute1();
}
}

// then remove biggest - should check not null
list1.remove (biggest);

关于java - 根据排序从 Arraylist 中删除对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56681434/

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