java ArrayList的第二高编号

unguejic  于 2023-06-28  发布在  Java
关注(0)|答案(7)|浏览(116)

到目前为止,我得到了这个代码:

int secondLargest = list.get(0);
int largest = list.get(0);
for (int i = 0; i < list.size(); i++)
{
    if(list.get(i) > largest)
    {
        secondLargest = largest;
        largest = list.get(i);

        if(list.get(i) > secondLargest && list.get(i) != largest)
        {
            secondLargest = list.get(i);
        }
    }
}

System.out.print("Second biggest number ");
return secondLargest;

问题是当我使用这段代码时(列表是:)

list2.add(1);
list2.add(2);
list2.add(10);
list2.add(9);
list2.add(8);
list2.add(7);

因为10是最高的数字,所以对第二高的数字的“搜索”在2处停止。我该怎么解决?

mw3dktmi

mw3dktmi1#

使用Arrays.sort(array);并获取第二个元素。

0wi1tuuw

0wi1tuuw2#

将第二个if condition放在第一个if条件之外。
因为second largest小于largest,所以你永远不会在if块中找到它,它会检查最大值。

int secondLargest = (int) list.get(0);
int largest = list.get(0);
for (int i = 1; i < list.size(); i++) {
  if(list.get(i) > largest) {
    secondLargest = largest;
    largest = list.get(i);
  }
  if(list.get(i) > secondLargest && list.get(i) != largest) {
    secondLargest = list.get(i);
  }
}
System.out.print("Second biggest number ");
return secondLargest;
ebdffaop

ebdffaop3#

你可以首先使用Collections.max()函数在ArrayList中找到最大值,一旦你有了最大值元素,找到该元素的索引并将其从数组中删除。再次使用Collections.max()查找数组中第二大的数字。代码如下

ArrayList<Integer> al = new ArrayList<Integer>();
al.add(1);
al.add(2);
al.add(3);
al.add(4);
System.out.println(al);

int j = Collections.max(al);

System.out.println("Max in the array is " + j);
al.remove(al.indexOf(j));
int max2 = Collections.max(al);
System.out.println(max2);

让我知道如果需要更多的细节。

1yjd4xko

1yjd4xko4#

使用两个for循环。第一个应该找到最大的数字,并存储其索引位置。第二个应该找到与先前找到的数字不在同一索引位置的最大数字。(这将确保您不会错过第二大数字与最大数字相同的情况。)
如果您认为合适,请使用Arrays.sort(array);并按照uı的建议获取第二个元素。

ukqbszuj

ukqbszuj5#

import java.util.Arrays;
import java.util.List;

public class second_highest_array {

    public static void main(String[] args) {
        //List<Integer> numlist = Arrays.asList(9,8,7,6,5,4,3,2,1,0);
        //List<Integer> numlist = Arrays.asList(0,1,2,3,4,5,6,7,8);
        //List<Integer> numlist = Arrays.asList(8,7,4,1,2,3,4,1,9,0,11,9,8);
        List<Integer> numlist = Arrays.asList(9,-8,5,4,1,7,3,2,6);
        int highestnum = numlist.get(0);
        int secondhighest = numlist.get(0);
        for(int numarr : numlist) {
            if(highestnum < numarr ) {
                secondhighest = highestnum ;
                highestnum = numarr;
            }else if(secondhighest < numarr && highestnum != numarr) {
                secondhighest = numarr;
            }else if(secondhighest == highestnum) {
                secondhighest = numarr;
            }
        }
        System.out.println("highest : "+highestnum +" :: Second Highest : "+secondhighest);
    }
}
hmmo2u0o

hmmo2u0o6#

在循环的第一个if statement中,检查second largest而不是largest。下面的内容会让你继续下去

int secondLargest = list.get(0);
    int largest = list.get(0);
    for (int i = 0; i < list.size(); i++)
    {
        if(list.get(i) > secondLargest)
        {
            if(list.get(i) > largest ) {
                secondLargest = largest;
                largest = list.get(i);
            } else {
                secondLargest = list.get(i);
            }

        }
    }
wj8zmpe1

wj8zmpe17#

HashSet integers = new HashSet<>(arrayList);
    ArrayList arrayList1 = new ArrayList(integers);
    return (int) arrayList1.get(arrayList1.size()-2);

相关问题