java 方法查找数组中的第二大数

zhte4eai  于 2023-01-04  发布在  Java
关注(0)|答案(8)|浏览(212)

每次当imment to get 3查看我的代码时都得到0的输出,我不确定我哪里出错了,我可以不使用我知道的方法,而只是尝试练习java

public class App {
    
    public static int second(int a[],int n) {
        int[] arr = new int [n];
        int temp;
        for(int i=0;i<n;i++) {
            for(int j=i+1;j<n;j++) {
                if(arr[i] > arr[j]) {
                    temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
        }
        return arr[n-2];
    }

    public static void main(String[] args) {
        int[] arr = {1,3,2,5,3};
        int n = 5;
        int result = second(arr,n);
        System.out.println(result);
    }
}
llmtgqce

llmtgqce1#

您可以更改数组参数名称arr并移除声明或将值从复制到arr。

public static int second(int arr[],int n) {
    int temp;
    for(int i=0;i<n;i++) {
        for(int j=i+1;j<n;j++) {
            if(arr[i] > arr[j]) {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
    return arr[n-2];
}

你得到零的原因是因为原始的int不能为空,所以,当你创建长度为5的数组时,它一开始就用零填充。

ttcibm8c

ttcibm8c2#

通过使用流来实现:

public static int second(int a[]) {
    return Arrays.stream(a)
            .sorted()
            .skip(a.length - 2)
            .findFirst()
            .getAsInt();
}

我删除了第二个参数,它对数组进行排序,跳过你想要的元素之前的所有元素,然后选择现在的第一个元素。

qoefvg9y

qoefvg9y3#

public static int second(int[] arr) {
    int highest = arr[0];
    int second = 0;
    for (int i = 1; i < arr.length; i++) {
        int j = arr[i];
        if (j >= highest) {
            highest = j;
        } else if (j > second) {
            second = j;
        }
    }
    return second;
}

public static void main(String[] args) {
    int[] arr = {1, 3, 2, 5, 3};
    int result = second(arr);
    System.out.println(result);
}
1dkrff03

1dkrff034#

这里有一种不需要排序的方法。

int[] arr = { 10, 2, 3, 19, 2, 3, 5 };
System.out.println(second(arr));

印刷品

10
  • largest设置为数组中的第一个值
  • secondLargest设置为最小可能值
  • 现在遍历数组。
  • 如果当前值大于最大值:
  • 用最大替换第二大
  • 用当前值替换最大值
  • 否则,检查当前值是否大于secondLargest,如果为真,则赋值。
public static int second(int arr[]) {
    int largest = arr[0];
    int secondLargest = Integer.MIN_VALUE;
    for (int i = 1; i < arr.length; i++) {
        if (arr[i] > largest) {
            secondLargest = largest;
            largest = arr[i];
        } else if (arr[i] > secondLargest) {
            secondLargest = arr[i];
        }
    }
    return secondLargest;
}
eoxn13cs

eoxn13cs5#

public static int second(int arr[],int n) {
    int temp;
    if(arr.length < 2) {
        return -1;
    }
    else {
    for(int i=0;i<n;i++) {
        for(int j=i+1;j<n;j++) {
            if(arr[i] > arr[j]) {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
    return arr[n-2];
}
ws51t4hk

ws51t4hk6#

public static void main(String[] args) {
    // TODO Auto-generated method stub
    int a[] = {23,14,56,77,66,67};
    int high = 0;
    int sec = 0;
    for(int i = 0 ; i <a.length; i++){
        if(high < a[i]){
            sec = high;
            high = a[i];
        }
        else if(sec < a[i]){
            sec = a[i];
        }
        
    }
    System.out.println("the first highest number is " + high);
    System.out.println("the second highest number is " + sec);
}

}

ff29svar

ff29svar7#

public static int sec(){
    int arr[] = {12,3,67,4,5,65};
    int high = 0;
    int low = 0;
    for(int i = 0 ; i < arr.length ; i ++){
        
        if(high < arr[i]){
            low = high;
            high = arr[i];
        }
        else if(low < arr[i]){
            low = arr[i];           
            }
    }
    return low;
}
public static void main(String[] args) {
    // TODO Auto-generated method stub

    sch obj = new sch();
    int a = obj.sec();
    
    System.out.println(a);
}

}

wj8zmpe1

wj8zmpe18#

包com;公共类查找数组中第二个最高数字{

public static void main(String[] args) {
    int []arrayOfIngeger = {-23,-989,-878,-2,-5,-3,-4,-123,-345,-98,-675,-98};
    int highestNumber = arrayOfIngeger[0];
    int secHighestNumber = 0;
    for(int i = 1; i < arrayOfIngeger.length; i++  ) {
        if(highestNumber < arrayOfIngeger[i]) {             
            secHighestNumber = highestNumber; 
            highestNumber = arrayOfIngeger[i];  
        }else if(secHighestNumber < arrayOfIngeger[i] && arrayOfIngeger[i] != highestNumber) {
            secHighestNumber = arrayOfIngeger[i];
        }
    }
    System.out.println("second Highest Number in Array   :  "+secHighestNumber);
}

}

相关问题