Java 返回值数组中两个最大的整数

Java 返回值数组中两个最大的整数,java,arrays,Java,Arrays,我试图从int数组返回两个最大的整数。 我可以返回最大和最小的罚款,但我不能让我的算法返回两个最大的罚款。 非常感谢您的帮助 请原谅我代码中的任何错误。这是一个实践环节,问题取自大学去年的考试材料 这是我的密码: public class TwoLargestIntsArray { public static void main(String [] args){ int [] values = new int[5]; values[0] = 5; values[1]

我试图从int数组返回两个最大的整数。 我可以返回最大和最小的罚款,但我不能让我的算法返回两个最大的罚款。 非常感谢您的帮助

请原谅我代码中的任何错误。这是一个实践环节,问题取自大学去年的考试材料

这是我的密码:

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

}
public类两个最大的intsarray{
公共静态void main(字符串[]args){
int[]值=新的int[5];
数值[0]=5;
数值[1]=10;
数值[2]=15;
数值[3]=20;
数值[4]=25;
System.out.println(两个最大值);
System.out.println();
}
公共静态int two最大(int值[]){
int largestA=值[0];
int largestB=值[0];
对于(int i=0;ilargestA){
大盖塔=值[i];
}
if(值[i]
公共静态void two最大(int值[]){
int largestA=值[0];
int largestB=-1;
对于(int i=0;ilargestA){
最大B=最大;
大盖塔=值[i];
}
else if(值[i]>largestB和值[i]!=largestA){
最大值b=值[i];
}
}
System.out.println(“最大-”+largestA);
系统输出打印项次(“第二大-”+最大B);
}

一个函数不能返回两个值。您必须将它们包装在一个数组中,或者使用引用参数。

传递要用值填充的数组:

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
试试这个:

public static int[] twoLargest(int values[]){

    int[] result = new int[2];
    int largestA = 0;
    int largestB = 0;

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

            if(values[i] > largestA){
            largestB = largestA;
            largestA = values[i];
        }
    }
     result[0] = largestA;
     result[1] = largestB;

    return result; 
}
public static int[]两个最大值(int值[]){
int[]结果=新的int[2];
int-largestA=0;
int-largestB=0;
对于(int i=0;ilargestA){
最大B=最大;
大盖塔=值[i];
}
}
结果[0]=largestA;
结果[1]=最大B;
返回结果;
}

如果您能够从函数中获得最大、第二大、第三大等等,我假设它将对您有所帮助。我创造了这样一个:

public static int xLargest(int values[], int largeIndex)
只需传递数组和大索引,对于最大发送1,对于第二大发送2,依此类推

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);
    }
}
import java.util.ArrayList;
导入java.util.Collections;
导入java.util.List;
公共类X射线{
公共静态void main(字符串[]args){
int[]值=新的int[5];
数值[0]=5;
数值[1]=10;
数值[2]=15;
数值[3]=20;
数值[4]=25;
System.out.println(xLargest(值,2));
System.out.println();
}
公共静态int xLargest(int值[],int大索引){
List intList=new ArrayList();
对于(int index=0;index
您还可以使用嵌套类存储计算结果。例如:

  private static class Result {

    int largestA;
    int largestB;

    Result(int largestA, int largestB) {
        this.largestA = largestA;
        this.largestB = largestB;
    }
}
然后接收数据,例如:

Result result = twoLargest(values);
System.out.println(result.largestA);
System.out.println(result.largestB);
你可以写

public static int[] twoLargest(int values[]){
    int largestA = Integer.MIN_VALUE, largestB = Integer.MIN_VALUE;

    for(int value : values) {
        if(value > largestA) {
            largestB = largestA;
            largestA = value;
        } else if (value > largestB) {
            largestB = value;
        }
    }
    return new int[] { largestA, largestB }; 
}
公共类测试
{
公共静态int[]findtwohighestdistinctValue(int[]数组)
{
int max=整数的最小值;
int secondMax=Integer.MIN\u值;
for(int值:数组)
{
如果(值>最大值)
{
secondMax=max;
最大值=最大值;
}
否则如果(值>第二个最大值&&值<最大值)
{
secondMax=值;
}
}
返回新的int[]{max,secondMax};
}
公共静态void main(字符串[]args)
{
int[]值=新的int[5];
数值[0]=5;
数值[1]=10;
数值[2]=15;
数值[3]=20;
数值[4]=25;
int[]ar=找到两个最高的distinctV值(值);
System.out.println(“1=“+ar[0]);
System.out.println(“2=“+ar[1]);
}  
}
输出:

1=25


2=20

Nilesh Jadav的回答涵盖了所有情况。如果数组的最大值是最后一个元素,则@Peter Lawrey的回答将失败。例如,[10,2,5,1,8,20]使用接受的解决方案返回20和8。

public static int Compute(int\u arrValues[]){
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; 
    }
int _intX=_arrValues[0]; int _intY=_arrValues[1]; 对于(int i=2;i<\u arrValues.length;i++){ 如果(_arrValues[i]>_intX){ _intX=值[i]; } else if(_arrValues[i]>_intY){ _intY=值[i]; } } 返回_intX+_intY; }
公共静态void main(字符串[]args){
int[]数字=新的int[]{1,2,5,4,57,54,656,4};
int temp=数字[0];
int max=温度;
int lastMax=温度;
for(int index=1;index
试试这个

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);
int[]array1={10,2,5,1,8,20};
int最大=阵列1[0];
int secmax=array1[0];
对于(int i=1;i
private static void printTwoMaxNumberWithOutportMethod)(int[]输入){
int max=0,lastMax=0;
lastMax=输入[0];
最大值=输入[0];
对于(int i=1;i
注意:我们可以将此astuce用于实现comparable或使用相关自定义比较器的任何bean对象。

公共类2
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; 
    }}
 public static void main(String[] args) {

    int[] numbers = new int[]{1, 2, 5, 4, 57, 54, 656, 4};
    int temp = numbers[0];
    int max = temp;
    int lastMax = temp;

    for (int index = 1; index < numbers.length; index++){
        int currentIndex = numbers[index];

        // check any time is it bigger than current maximum value
        max = Math.max(max, currentIndex);

        // if is grow up then should be max != temp; set old Max in last or 
        // is not group up but current index is bigger then last index update last max value
        lastMax = max != temp ? temp : Math.max(currentIndex, lastMax);

        temp = max;
    }

    System.out.println("Last max: " + lastMax);
    System.out.println("Max:" + max);
}
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);
private static void printTwoMaxNumberWithoutSortMethod(int[] input) {
    int max=0,lastMax=0;
    lastMax=input[0];
    max=input[0];
    for(int i=1;i<input.length;i++)
    {
        if(lastMax<input[i] & max<input[i])
        {
            lastMax=max;
            max=input[i];

        }
    }
    System.out.println("lastMax="+lastMax+" : max="+max);

}
   //Java8&9 makes this easier with a cleaner code
   int[] numbers = new int[]{1, 2, 5, 4, 57, 54, 656, 4};       
    int maxSize = 2;
    Arrays.stream(numbers) //stream
    .boxed()  //to Integer Object
    .sorted(Comparator.reverseOrder()) //sorted
    .limit(maxSize )  //keep N values
    .forEach(System.out::println); 
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; 
    }}
public static int[] twoLargest(int[] values) {
    Arrays.sort(values);
    return new int[]{values[values.length - 1], values[values.length - 2]};
}