【问题标题】:Return remaining values of an array返回数组的剩余值
【发布时间】:2017-02-21 17:23:26
【问题描述】:

正如您在此处看到的,我返回数组中最大的两个整数。

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:我既不想对剩余的值求和(我需要每一个),也不想通过冒泡排序或某事对它们进行排序。像这样。

【问题讨论】:

  • 你不能使用myobject.a 来表示“在 if 语句中使用我的数组的每个剩余整数”

标签: java arrays if-statement


【解决方案1】:

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

2) 最简洁的方式:返回一个具有三个字段的类的实例:

  • int 最大值;
  • 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) {
    // ...
}

【讨论】:

  • 好的,但它在我的 GUI 中应该是什么样子? int[] 剩余 = myobject.computeTwoLargestIntAndRemainingValues(myobject.a); if (leftover[0] == 1) { 不起作用,因为 "twolargerstin..." 不能转换为 int[]
  • 您应该在 TwoLargestIntAndRemainingValues() 中添加 getter 以获取 max、beforeMax 和剩余字段。然后用 max 替换 leftover[0],用 max 之前的 le​​ftover[1] 替换。我更新了代码并添加了您的示例。
  • 好吧,但我的问题是关于获取剩余整数的每个值,if 语句应该如何看待它们?在这种情况下,我不知道如何将 int[] 与 int 进行比较
  • 迭代 int[] 并在循环中进行比较。
  • 对不起,我完全不明白,我几个月前才开始编程:(
【解决方案2】:

这是你想要的吗?

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

【讨论】:

    猜你喜欢
    • 2021-05-03
    • 1970-01-01
    • 1970-01-01
    • 2013-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-12
    相关资源
    最近更新 更多