返回数组的剩余值

时间:2017-02-21 17:23:27

标签: java arrays if-statement

正如你在这里看到的,我返回数组的两个最大整数。

int[] a = {a1 = (int) ((Math.random() * 10) + 1), a2 = (int) ((Math.random() * 10) + 1), a3 = (int) ((Math.random() * 10) + 1), a4 = (int) ((Math.random() * 10) + 1), a5 = (int) ((Math.random() * 10) + 1)};

public static int[] showtwolargestints(int a[]) {   // returns two largest integers of my array
int largestA = Integer.MIN_VALUE, largestB = Integer.MIN_VALUE;
for(int value : a) {
 if(value > largestA) {
  largestB = largestA;
  largestA = value;
} else if (value > largestB) {
  largestB = value;
}
}
  return new int[] { largestA, largestB };  
}

我设法在我的GUI类/ JButton方法的if语句中使用它们。

int[] twoLargest = myobject.showtwolargestints(myobject.a);

public void buttonmethod() {   
try {                         
if (twoLargest[0] == 10 && twoLargest[1] == 10) {
// ...
} else if (twoLargest[0] == 10 && twoLargest[1] == 9) {
// ...
}

但是,我现在想通了,对于我的程序,在找到这两个最大值之后,我还必须在if语句中使用我的数组的每个剩余整数。

我是初学者,我的尝试效果不佳:l你能用一种简单的方式帮助我吗?

PS:我既不想总结其余的值(我需要每一个),也不想通过Bubblesort或者某种方式对它们进行排序。那样的。

2 个答案:

答案 0 :(得分:1)

1)快速且不干净/可维护的方式:而不是返回两个int的数组,返回一个数组,首先是两个最大的int,然后是它们(因此从索引2开始,在数组中添加其他int值)

2)最简洁的方法:返回一个包含三个字段的类的实例:

  • int max;
  • int beforeMax;
  • int []剩余;

然后你有两种方法可以继续:

  • 优化方式:在实际迭代过程中添加remainingInts数组中的值。它需要在循环期间进行一些计算。

  • 未经优化的方式:找到两个最大值后,再次迭代数组并添加不同于两个最大值的remainings数组值。

使用没有优化的干净方式,您的方法可能如下所示:

public static TwoLargestIntAndRemainingValues computeTwoLargestIntAndRemainingValues(int a[]) {

    // returns two largest integers of my array
    int largestA = Integer.MIN_VALUE, largestB = Integer.MIN_VALUE;

    for (int value : a) {
        if (value > largestA) {
            largestB = largestA;
            largestA = value;

        } else if (value > largestB) {
            largestB = value;
        } 
    }

    // changes here

    // if minus three values, you have no remaining to handle
    int[] remainings= null;

    if (a.length > 2) {
        remainings= new int[a.length - 2];
        int i = 0;
        for (int value : a) {
            if (value != largestA && value != largestB) {
                remainings[i++] = value;
            }
        }
    }

    return new TwoLargestIntAndRemainingValues(largestA, largestB, remainings);
 }

,数据结构可以是:

public class TwoLargestIntAndRemainingValues {

    private int max;
    private int beforeMax;
    private int[] remainings;

    public TwoLargestIntAndRemainingValues(int max, int beforeMax, int[] remainings) {
        this.max = max;
        this.beforeMax = beforeMax;
        this.remainings = remainings;
    }

    @Override
    public String toString() {
        return "TwoLargestIntAndRemainingValues [max=" + max + ", beforeMax=" + beforeMax + ", remainings="
                + Arrays.toString(remainings) + "]";
    }

    public int getMax() {
        return max;
    }

    public int getBeforeMax() {
        return beforeMax;
    }

    public int[] getRemainings() {
        return remainings;
    }

}

有关如何获取值的编辑:

TwoLargestIntAndRemainingValues result = myobject.computeTwoLargestIntAndRemainingValues(myobject.a);

public void buttonmethod() {   
  try {                         
    if (result.getMax() == 10 && result.getBeforeMax() == 10) {
    // ...
    } else if (result.getMax()  == 10 && result.getBeforeMax() == 9) {
    // ...
}

答案 1 :(得分:0)

这是你想要的吗?

//Assuming a is an array of 5 integers and you already know the 2 largest

int[] remaining = new int[3];
int j = 0;
for(int i : a){
    if(i != twoLargest[0] && i != twoLargest[1]){
        remaining[j] = i;
        j++;
    }
}