获取List中的最大最小值,可以直接使用以下代码
();
()
不过这个方法的参数值却是类似List< Integer > 或者 List< double >
对于实体类的List来说就不太管用了,那么只能自己遍历了
有一个实体类
private class Test{
int id;
double price;
public Wfdd(int id, double price) {
= id;
= price;
}
}
有一个List
List<Test> testList = new ArrayList<>();
for(int i = 0;i < 5 ;i ++ ){
Test test= new Test(i+1, i + 10.24);
testList .add(test);
}
有一个要求:获取最小值及其索引
double minValue = (0).price;
int minIndex = 0;
for (int i = 0; i < testList .size(); i++) {
if (i == testList .size() - 1) {
continue;
}
double next = testList .get(i + 1).price;
if (minValue > next) {
minValue = next;
minIndex = i;
}
}
("checkPrice" + minIndex + "_" + minValue);