Return the two largest integers in an array of val

2020-02-06 03:29发布

I am attempting to return the two largest integers from my int array. I am able to return the largest and the smallest fine, but I cannot get my algorithm to return the two largest. Any help is greatly appreciated here.

Please forgive any errors in my code. This is a practice session and the question has been taken from last years exam material at university.

Here is my code:

public class TwoLargestIntsArray {

public static void main(String [] args){

    int [] values = new int[5];

    values[0] = 5;
    values[1] = 10;
    values[2] = 15;
    values[3] = 20;
    values[4] = 25;

    System.out.println(twoLargest(values));
    System.out.println();

}

public static int twoLargest(int values[]){

    int largestA = values[0];
    int largestB = values[0];

    for(int i = 0; i < values.length; i++){

            if(values[i] > largestA){
                largestA = values[i];
            }
            if(values[i] < largestA){
                largestB = values[i];   
            }

    }
    return largestA + largestB; 
}

}

标签: java arrays
16条回答
我欲成王,谁敢阻挡
2楼-- · 2020-02-06 03:55
public static int Compute(int _arrValues[]){

        int _intX = _arrValues[0];
        int _intY = _arrValues[1];

        for(int i = 2; i < _arrValues.length; i++){

                if(_arrValues[i] > _intX){
                    _intX= values[i];
                }
                else if(_arrValues[i] > _intY){
                    _intY = values[i];   
                }
        }
        return _intX + _intY; 
    }
查看更多
成全新的幸福
3楼-- · 2020-02-06 03:57

Pass array to be filled with values:

public static void twoLargest(int [] values, int [] ret){
    //...
    ret[0] = largestA;
    ret[1] = largestB;
}


int [] ret = new int [2];
twoLargest(values, ret);
// now ret[0] is largestA
// and ret[1] is largestB
查看更多
SAY GOODBYE
4楼-- · 2020-02-06 03:57

I am assuming it will help you in case you are able to get the largest, secondlargest, thirdlargest and so on from a function. I have created such a one :

public static int xLargest(int values[], int largeIndex)

Just pass the array and the largeIndex, for largest send 1 , for second largest send 2 and so on.

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class XLargestIntArray {

    public static void main(String[] args) {

        int[] values = new int[5];

        values[0] = 5;
        values[1] = 10;
        values[2] = 15;
        values[3] = 20;
        values[4] = 25;

        System.out.println(xLargest(values,2));
        System.out.println();

    }

    public static int xLargest(int values[], int largeIndex) {

        List<Integer> intList = new ArrayList<Integer>();
        for (int index = 0; index < values.length; index++) {
            intList.add(values[index]);
        }
        Collections.sort(intList);
        return intList.get(intList.size() - largeIndex);
    }
}
查看更多
▲ chillily
5楼-- · 2020-02-06 04:03
public static void twoLargest(int values[]){

    int largestA = values[0];
    int largestB = -1;

    for(int i = 0; i < values.length; i++){

            if(values[i] > largestA){
                largestB = largestA;
                largestA = values[i];
            }
            else if (values[i] > largestB && values[i] != largestA) {
                largestB = values[i];
            }
    }
    System.out.println("Largest - " + largestA);
    System.out.println("2nd largest Largest - " + largestB);
}
查看更多
我想做一个坏孩纸
6楼-- · 2020-02-06 04:03

Try this out

int [] array1={10,2,5,1,8,20};

int largest= array1[0];

int seclargest=array1[0];

for (int i=1;i<array1.length;i++)
{
    if(largest <= array1[i])
    {
        seclargest=largest;
        largest=array1[i];  
    }   

}

System.out.println("largest and second largest are:" +largest + " " +seclargest);
查看更多
Fickle 薄情
7楼-- · 2020-02-06 04:04
public class TwoLargestIntArray {


    public static void main(String [] args){

        int a[]  = new int[5];

        a[0] = 110;
        a[1] = 50;
        a[2] = 15;
        a[3] = 30;
        a[4] = 60;

        System.out.println(twoLargest(a));
        System.out.println();

    }

    public static int twoLargest(int a[]){

        int firstMax = 0;
        int secondMax = 0;

        for(int i = 0; i < a.length; i++){

                if(a[i]>firstMax) {
                    secondMax=firstMax;
                    firstMax = a[i];}
                    else if (a[i]>secondMax) {
                        secondMax= a[i];
                    }

                    }




        return  firstMax + secondMax; 
    }}
查看更多
登录 后发表回答