Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当两个值的最大值绑定时,如何在ArrayList中返回最大值?_Java_Loops_Arraylist_Methods - Fatal编程技术网

Java 当两个值的最大值绑定时,如何在ArrayList中返回最大值?

Java 当两个值的最大值绑定时,如何在ArrayList中返回最大值?,java,loops,arraylist,methods,Java,Loops,Arraylist,Methods,到目前为止,我已经有了这个方法,应该是找到ArrayList的最大年龄。然而,在我的数据中,我有两个值,关节最大值为58。如何从这个循环中获得第二个58?前58的索引是1,但我需要的索引是4。我不会硬编码 public static int maxAge (ArrayList<Integer> ages) { int hold = 0; int max = 0; for (int i = 0; i < ages.size(); i

到目前为止,我已经有了这个方法,应该是找到ArrayList的最大年龄。然而,在我的数据中,我有两个值,关节最大值为58。如何从这个循环中获得第二个58?前58的索引是1,但我需要的索引是4。我不会硬编码

public static int maxAge (ArrayList<Integer> ages) {
        int hold = 0;
        int max = 0;
        for (int i = 0; i < ages.size(); i++) {
            if (max < ages.get(i)) {
                max = ages.get(i);
                hold = i;
            }
            else i++;       
        }
        return hold;
    }
publicstaticintmaxage(arraylistages){
int hold=0;
int max=0;
对于(int i=0;i
您只需将条件更改为:


if(max您只需将您的条件更改为:


if(max这取决于为什么要使用另一个58。如果要返回最新匹配项,可以向后循环。

这取决于为什么要使用另一个58。如果要返回最新匹配项,可以向后循环。

下面的代码将允许您返回与列表最大值相关联的所有索引。但是您必须在Java8上运行它,因为它使用Lambda表达式

public static ArrayList<Integer> maxAge (ArrayList<Integer> ages ) {
        int max = 0;
        ArrayList<Integer> maxIndexes = new ArrayList<Integer>() ;
        for (int i = 0; i < ages.size(); i++) {
            if (max <= ages.get(i)) {
                final int finalMax = max ;
                final int finalIndex = i ;
                maxIndexes.removeIf((elt)-> finalMax < ages.get(finalIndex)) ;
                maxIndexes.add(i) ;
                max = ages.get(i) ;
            }               
        }
        return maxIndexes ;
    }
公共静态ArrayList最大年龄(ArrayList年龄){
int max=0;
ArrayList maxIndexes=新的ArrayList();
对于(int i=0;i
下面的代码将允许您返回与列表最大值相关的所有索引。但您必须在Java 8上运行它,因为它使用Lambda表达式

public static ArrayList<Integer> maxAge (ArrayList<Integer> ages ) {
        int max = 0;
        ArrayList<Integer> maxIndexes = new ArrayList<Integer>() ;
        for (int i = 0; i < ages.size(); i++) {
            if (max <= ages.get(i)) {
                final int finalMax = max ;
                final int finalIndex = i ;
                maxIndexes.removeIf((elt)-> finalMax < ages.get(finalIndex)) ;
                maxIndexes.add(i) ;
                max = ages.get(i) ;
            }               
        }
        return maxIndexes ;
    }
公共静态ArrayList最大年龄(ArrayList年龄){
int max=0;
ArrayList maxIndexes=新的ArrayList();
对于(int i=0;i
我知道,它很难看。但我会尝试编写一个功能版本

Optional<Integer> max = ages.stream()
                            .max(Integer::compare);
if (max.isPresent()) {
  return IntStream.range(0, ages.size())
                  .mapToObj(pos -> {
                    return new int[] {pos, ages.get(pos)};
                  })
                  .filter(pair -> pair[1] == max.get())
                  .collect(Collectors.toCollection(LinkedList::new))
                  .getLast()[0];
} else
  return 0;
Optional max=ages.stream()
.max(整数::比较);
如果(最大isPresent()){
返回IntStream.range(0,ages.size())
.mapToObj(位置->{
返回新的int[]{pos,ages.get(pos)};
})
.filter(对->对[1]==max.get())
.collect(收集器.toCollection(LinkedList::new))
.getLast()[0];
}否则
返回0;

我知道,它很难看。但我会尝试编写一个功能版本

Optional<Integer> max = ages.stream()
                            .max(Integer::compare);
if (max.isPresent()) {
  return IntStream.range(0, ages.size())
                  .mapToObj(pos -> {
                    return new int[] {pos, ages.get(pos)};
                  })
                  .filter(pair -> pair[1] == max.get())
                  .collect(Collectors.toCollection(LinkedList::new))
                  .getLast()[0];
} else
  return 0;
Optional max=ages.stream()
.max(整数::比较);
如果(最大isPresent()){
返回IntStream.range(0,ages.size())
.mapToObj(位置->{
返回新的int[]{pos,ages.get(pos)};
})
.filter(对->对[1]==max.get())
.collect(收集器.toCollection(LinkedList::new))
.getLast()[0];
}否则
返回0;

有多种方法可以更改条件以符合您的描述。请尝试以下操作:

public static int maxAge (ArrayList<Integer> ages) {
    int hold = 0;
    int max = 0;
    for (int i = 0; i < ages.size(); i++) {
        if (max < ages.get(i) || max == ages.get(i) {
            max = ages.get(i);
            hold = i;
        }
        else i++;       
    }
    return hold;
}
publicstaticintmaxage(arraylistages){
int hold=0;
int max=0;
对于(int i=0;i
有多种方法可以更改条件以符合您的描述。请尝试以下操作:

public static int maxAge (ArrayList<Integer> ages) {
    int hold = 0;
    int max = 0;
    for (int i = 0; i < ages.size(); i++) {
        if (max < ages.get(i) || max == ages.get(i) {
            max = ages.get(i);
            hold = i;
        }
        else i++;       
    }
    return hold;
}
publicstaticintmaxage(arraylistages){
int hold=0;
int max=0;
对于(int i=0;i
您想返回两个结果,还是只返回最后一个结果?如果只返回最小的一个,则将if(maxelse i++;
。它会导致不正确的结果,因为它会跳过元素。您想同时返回两个结果,还是只返回最后一个结果?如果只返回一个,则将if(maxelse i++;
。它会导致不正确的结果,因为它会跳过元素。