gpt4 book ai didi

java - 返回数组的剩余值

转载 作者:行者123 更新时间:2023-11-30 06:53:05 24 4
gpt4 key购买 nike

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

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

最佳答案

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

2)最干净的方法:返回具有三个字段的类的实例:

  • 整数最大值;
  • int beforeMax;
  • int[] 剩余;

那么您有两种方式进行:

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

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

使用未经优化的干净方法,您的方法可能如下所示:

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) {
// ...
}

关于java - 返回数组的剩余值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42373955/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com