2017-02-21 92 views
1

正如你在这里看到的,我返回了我的数组中的两个最大的整数。返回一个数组的剩余值

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

不过,我现在想通了,对于我PROGRAMM我也用我的阵列的每一个剩余整数if语句已经发现这两个最大的值之后。

我是一个初学者,我的努力没有很好地发挥作用:l你能以简单的方式帮助我吗? PS:我既不想总结其余的值(我需要每一个值),也不想通过Bubblesort或某物排序它们。像那样。

+0

你不能用'myobject.a'为“用我的阵列的每一个剩余整数if语句” –

回答

1

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

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

  • INT最大;
  • 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) { 
    // ... 
} 
+0

好吧,但它应该怎么看都像是在我的GUI? int [] leftover = myobject.computeTwoLargestIntAndRemainingValues(myobject.a);因为“twolargerstin ...”不能转换为int [] – user7585069

+0

,所以您应该在TwoLargestIntAndRemainingValues()中添加getters以获取max,beforeMax和其余字段。然后在max之前将剩余[0]替换为max和left [1]。我更新了代码并添加了您的示例。 – davidxxx

+0

好的,但我的问题是要获取剩余整数的每个值,if语句应该如何看待它们?我不知道如何比较int []和int,在这种情况下, – user7585069

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