这个java代码中的第一大和第二大?

时间:2015-05-01 19:23:22

标签: java

我从字符串中获得了最大数字和最小数字。但是如何从这个问题中找到这个java代码中的第二大数字和第三大数字呢?我应该使用哪些代码?请解释

public class Problem1
{
public static void main(String[] args) {
int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// int b[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
Problem1 app = new Problem1();
app.scrambleArray(a);
app.print(a);
// Usage enable assertions: -ea to VM arguments
int result = app.findInt(a, 10);
assert (result == 10) :
String.format("Expected <10> but was <%d>", result);
result = app.findInt(a, 11);
assert (result == -1) :
String.format("Expected <-1> but was <%d>", result);
System.out.printf("Largest Number is : %d%n", app.getMax(a));
app.print(app.reverseArray(a));
}
public void scrambleArray(int[] a) {
for (int i = 0; i < a.length; i++) {
int pos = new Random().nextInt(a.length);
int tmp = a[i];
a[i] = a[pos];
a[pos] = tmp;
}
}
public void print(int[] a) {
System.out.println(Arrays.toString(a));
}
public int getMax(int[] a) {
int max = a[0];
for (int i = 1; i < a.length; i++) {
max = Math.max(a[i], max);
}
return max;
}
public int findInt(int[] a, int value) {
int result = -1;
for (int i : a) {
if (value == i) {
result = value;
break;
}
}
return result;
}
public int[] reverseArray(int[] a) {
int[] results = new int[a.length];
for (int i = 0, idx = a.length - 1; i < a.length; i++, idx--) {
results[i] = a[idx];
}
return results;
}
}

3 个答案:

答案 0 :(得分:1)

使用Arrays.sort()方法对整数数组进行排序

int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
Arrays.sort(a);
System.out.println("largest value:  " + a[a.length - 1]);
System.out.println("second largest: " + a[a.length - 2]);

答案 1 :(得分:0)

prior answer通常是一个很好的解决方案。例外情况是,数组中的元素数量非常大且性能很重要。在这种情况下,将N个最大元素保留在有序集合中可能会更快,并避免对整个列表进行排序:

  public int[] getNLargest(int[] in, int n){
    TreeSet<Integer> large = new TreeSet<Integer>();
    for(int i : in){
      if(large.size() < n){
        large.add(i);
      } else if(i > large.first().intValue()){
        large.remove(large.first());
        large.add(i);
      }
    }
    int[] result = new int[large.size()];
    int index = 0;
    for(Integer i : large){
      result[index] = i.intValue();
      index++;
    }
    return result;
  }

答案 2 :(得分:0)

import java.util.*;
public class SecondLargestInArray
{
    public static void main(String[] args)
    {
        int arr[] = {14,46,47,86,92,52,48,36,66,85,92};
        int largest = arr[0];
        int secondLargest = arr[0];
        System.out.println("The given array is:" );
        for (int i = 0; i < arr.length; i++)
        {
            System.out.print(arr[i]+"\t");
        }

        for (int i = 0; i < arr.length; i++)
        {
            if (arr[i] > largest)
            {
                secondLargest = largest;
                largest = arr[i];
            }
            else if((arr[i]<largest && arr[i]>secondLargest) || largest==secondLargest)
            {
                secondLargest=arr[i];
            }
        }
        System.out.println("\nLargest number is:" + largest);
        System.out.println("\nSecond largest number is:" + secondLargest);
    }
}