Second highest number ArrayList

无人久伴 提交于 2020-06-08 12:44:30

问题


So I got this code so far:

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;       

The problem is that when I use this code (the list is:)

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

the "search" for the second highest number stops at 2, because 10 is the highest number. How do I fix this?


回答1:


Put the second if condition outside the first if condition.

Because second largest is smaller than largest so you will never find it in the if block which check for the largest value.

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;



回答2:


Use Arrays.sort(array); and get the second element.




回答3:


You could first find the max number in the ArrayList using the Collections.max() function, once you have the max element, find the index of that element and get this removed from the array. Again use Collections.max() to find the second largest number in the array. Code as below

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);

Let me know if more details are needed around it.




回答4:


Use two for loops. The first should find the largest number, and store its index position. The second should find the largest number that is not at the same index position as the previously found number. (This will ensure that you do not miss cases where the second-largest number is the same as the largest.)

If you think it is appropriate, use Arrays.sort(array); and get the second element as suggested by ɐuıɥɔɐɯ.




回答5:


Within the loop's first if statement, check versus the second largest rather than the largest. The below will get you going

    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);
            }

        }
    }


来源:https://stackoverflow.com/questions/32999365/second-highest-number-arraylist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!