简体   繁体   中英

Second highest number ArrayList

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?

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

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;

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.

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ıɥɔɐɯ.

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

        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM