在Java中显示数组中的第二大数字

时间:2012-05-23 08:27:54

标签: java arrays

大家好我想知道数组中的第二大数字是否接受负数。我使用了以下代码,这显示第二大,只有正面没有。所以请建议我如何做到这一点。

class ArrayExample {
    public static void main(String[] args) {
        int secondlargest = 0;
        int largest = 0;
        Scanner input = new Scanner(System.in);
        System.out.println("Enter array values: ");
        int arr[] = new int[5];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = input.nextInt();
            if (largest < arr[i]) {
                secondlargest = largest;
                largest = arr[i];
            }
            if (secondlargest < arr[i] && largest != arr[i])
                secondlargest = arr[i];
        }
        System.out.println("Second Largest number is: " + secondlargest);
    }
}

9 个答案:

答案 0 :(得分:7)

问题来自于您将两个变量初始化为行中的0:

int secondlargest = 0;
int largest = 0;

您应该将它们初始化为Integer.MIN_VALUE,然后它也适用于负值。

答案 1 :(得分:1)

将maximum和secondLargest初始化为Integer.MIN_VALUE而不是零。

答案 2 :(得分:1)

这对我来说既有正面价值,也有负面价值。在循环之前,您需要使用非常小的值初始化largestsecondlargest变量,通过这样做,您可以(几乎)确定数组中的所有其他值都将大于它们:

int largest = Integer.MIN_VALUE;
int secondlargest = Integer.MIN_VALUE;

循环内部:

if (arr[i] > largest) {
    secondlargest = largest;
    largest = arr[i];
}

else if (arr[i] != largest && arr[i] > secondlargest) {
    secondlargest = arr[i];
}

循环之后:

if (secondlargest != Integer.MIN_VALUE)
    System.out.println("Second Largest number is: " + secondlargest);

请注意,对于(非常不太可能)的情况,需要进行最后一次检查,其中数组中的所有元素恰好是Integer.MIN_VALUE

答案 3 :(得分:0)

使用最小的负值初始化变量secondlargestlargest
使用此代码:

class ArrayExample {
        public static void main(String[] args) {
            int secondlargest = Integer.MIN_VALUE;
            int largest = Integer.MIN_VALUE;
            Scanner input = new Scanner(System.in);
            System.out.println("Enter array values: ");
            int arr[] = new int[5];
            for (int i = 0; i < arr.length; i++) {
                arr[i] = input.nextInt();
                if (largest < arr[i]) {
                    secondlargest = largest;
                    largest = arr[i];
                }
                if (secondlargest < arr[i] && largest != arr[i])
                    secondlargest = arr[i];
            }
            System.out.println("Second Largest number is: " + secondlargest);
        }
    }

答案 4 :(得分:0)

我认为这种方法在n + log(n)-2比较中可能很有用

 import java.util.ArrayList;


 public class SecondLargest {
 static ArrayList<ArrayList<Integer>> level = new ArrayList<ArrayList<Integer>>();

public static void main(String[] args) {
    int input[]={9,8,7,4,5,6,1,2,3,1,1,21,33,32,1,2,3,12,3,2,1};
    ArrayList<Integer> arr= new ArrayList<Integer>();

    for(int i=0;i<input.length;i++){
        arr.add(input[i]);
    }
    level.add(arr);
    seconLarger(arr);
    System.out.println(SecondLarge(level));
}

private static ArrayList<Integer> seconLarger(ArrayList<Integer> arr) {
    ArrayList<Integer> tmp= new ArrayList<Integer>();
    if (arr.size()==1)
    {
        return arr;
    }

if(arr.size()%2==0)
    {
        for(int i=0;i<arr.size();i=i+2)
        {
            if(arr.get(i)>arr.get(i+1))
            {
                tmp.add(arr.get(i));
            }
            else
            {
                tmp.add(arr.get(i+1));
            }
        }


    }   
else 
    {
        for(int i=0;i<arr.size()-1;i=i+2)
        {
            if(arr.get(i)>arr.get(i+1))
            {
                tmp.add(arr.get(i));
            }
            else
            {
                tmp.add(arr.get(i+1));
            }
        }
        tmp.add(arr.get(arr.size()-1));
    }
level.add(tmp);
return seconLarger(tmp);
}

private static int SecondLarge(ArrayList<ArrayList<Integer>> li)
{
    int li_size=li.size();
    int large=li.get(li_size-1).get(0);
    int secondlarge=0;
    int tmp=0;
    for(int i=li_size-2;i>=0;i--)
    {
        ArrayList<Integer> arr = li.get(i);
        if(large==arr.get(tmp))
        {
            if(tmp+1<arr.size())
            {
                if(secondlarge<arr.get(tmp+1))
                {
                    secondlarge=arr.get(tmp+1);
                }
            }
        }
        else
        {
            if(secondlarge<arr.get(tmp))
            {
                secondlarge=arr.get(tmp);
            }
            tmp=tmp+1;

        }

        tmp=tmp*2;
    }
    return secondlarge;
}}

答案 5 :(得分:0)

package com.demo.mum;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author cyruses
 * 
 */
public class SecondLargest {
    public static int largest = Integer.MIN_VALUE;
    public static int secondLargest = Integer.MIN_VALUE;

    public static void main(String args[]) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the Size of Array:");
        int n = Integer.parseInt(br.readLine());
        int a[] = new int[n];
        System.out.println("Enter the elements on array:");
        for (int i = 0; i < a.length; i++) {
            a[i] = Integer.parseInt(br.readLine());
        }
        System.out.println("Elements you entered are:");
        for (int i = 0; i < a.length; i++) {
            System.out.println("a[" + i + "]" + "=" + a[i]);
        }
        if (a.length <= 2) {
            if (a[0] == a[1]) {
                System.out.println("Theres no second largest number in your array");
            } else {
                System.out.println("SecondLargest:" + secondLargest(a));
            }
        } else {
            System.out.println("SecondLargest:" + secondLargest(a));
        }
    }

    private static int secondLargest(int[] a) {

        for (int i = 0; i < a.length; i++) {
                if (a[i] > largest) {
                    secondLargest = largest;
                    largest = a[i];
                } else if (a[i] > secondLargest) {
                    secondLargest = a[i];
                }
            }
        return secondLargest;
    }
}

答案 6 :(得分:0)

或者这是一个花哨的Java实现,它不依赖于Integer.MIN_VALUE

int findSecondHighest(int[] arr){
    if(arr == null || arr.length < 2){
        throw new IllegalArgumentException();
    }

    int fh,sh;

    if(arr[0]>=arr[1]){
        fh = arr[0];
        sh = arr[1];
    }else{
        fh = arr[1];
        sh = arr[0];
    }

    for (int i = 2; i < arr.length; i++) {
        if(fh < arr[i]){
            sh = fh;
            fh = arr[i];
        }else if(sh < arr[i]){
            sh = arr[i];
        }
    }

    return sh;
}

答案 7 :(得分:0)

而不是将第二大数字初始化为最小值或零。您可以按照以下代码获得第二大数字。

import java.util.Scanner;

public class SecondLargestNumber {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] a;
        int n;
        System.out.println("Enter number of elements");
        n = scanner.nextInt();
        a = new int[n];
        for(int i=0; i<n; i++) {
            a[i] = scanner.nextInt();
        }
        int secondLargestNumber = a[0];
        int largestNumber = a[0];
        int count = 1;
        for(int i=1; i<a.length; i++) {
            if(a[i] >= largestNumber) {
                if(a[i] == largestNumber) {
                    count++;
                } else {
                    count = 1;
                }
                secondLargestNumber = largestNumber;
                largestNumber = a[i];
            } else {
                if(secondLargestNumber == largestNumber && count == 1) {
                    secondLargestNumber = a[i];
                } else if(a[i] > secondLargestNumber) {
                    secondLargestNumber = a[i];
                }
            }
        }
        System.out.println("Second Largest Number: " + secondLargestNumber);
    }
}

答案 8 :(得分:0)

public static void main(String[] args) {

    Scanner in = new Scanner(System.in);
    System.out.println("Enter array size = ");
    int size=in.nextInt();
    int[] n = new int[size];
    System.out.println("Enter "+ size +" values ");

    for(int i=0;i<n.length;i++)
        n[i] = in.nextInt();
    int big=n[0],sbig=n[0];

    // finding big, second big
    for(int i=0;i<n.length;i++){
        if(big<n[i]){
            sbig=big;
            big=n[i];
            }else if(sbig<n[i])
                sbig=n[i];
    }
    // finding second big if first element itself big
    if(big==n[0]){
        sbig=n[1];
        for(int i=1;i<n.length;i++){
            if(sbig<n[i]){
                sbig=n[i];
                }
        }
    }       

    System.out.println("Big "+ big+" sBig "+ sbig);

    in.close();
}