简体   繁体   English

ArrayList While Loop给出IndexOutOfBoundsException

[英]ArrayList While Loop Gives IndexOutOfBoundsException

I am trying to write a method that determines the maximum value of an array list using tournament style comparisons. 我正在尝试编写一种方法,使用锦标赛风格比较来确定数组列表的最大值。 However, I guess I don't understand something about while loops because I cannot get the desired output and instead get an IndexOutOfBoundsException. 但是,我想我不了解while循环,因为我无法获得所需的输出,而是得到一个IndexOutOfBoundsException。

Here's my code: 这是我的代码:

import java.util.*;
public class TournamentMax {

public static <T extends Comparable<? super T>> ArrayList<T> tournament(ArrayList<T> tournamentArrayList) {
    ArrayList<T> winners = new ArrayList<T>();
    int n = tournamentArrayList.size();
    int upper;

    if (n % 2 != 0 ){ // if size is odd
        winners.add(tournamentArrayList.get(n));
        upper = n - 2;
    }

    else{  // if size is even
        upper = n - 1;
    }

    for (int index = 0; index < upper; index+=2){


        T winner = max(tournamentArrayList.get(index), tournamentArrayList.get(index + 1));
         System.out.println("Comparison between: " + tournamentArrayList.get(index) + " and " + tournamentArrayList.get(index + 1) );
         System.out.println("Winner was: " + winner);
        winners.add(winner);
    }

    return winners;     
}

public static <T extends Comparable<? super T>> T max (T obj1, T obj2){
    if (obj1.compareTo(obj2) > 0){
    return obj1;    
    }
    else return obj2;
}

public static <T extends Comparable<? super T>> ArrayList<T> maximum(ArrayList<T> tournamentArrayList){
    ArrayList<T> maximum = new ArrayList<T>();
    for (int i = 0; i < tournamentArrayList.size(); i++){
        maximum.add(tournamentArrayList.get(i));
    }
    while (maximum.size() > 1){
    System.out.println("maximum before tournament" + maximum);
    maximum = tournament(maximum);
    System.out.println("maximum after tournament and the one returned" + maximum);
    }   
    return maximum;


}

}

I know the problem is somewhere in this part: 我知道问题出在这个部分的某个地方:

while (maximum.size() > 1){
    System.out.println("maximum before tournament" + maximum);
    maximum = tournament(maximum);
    System.out.println("maximum after tournament and the one returned" + maximum);

In my head I'm trying to make the ArrayList be continually passed back to the tournament method until the ArrayList contains only one item, which should be the maximum. 在我的脑海中,我试图让ArrayList不断传递回锦标赛方法,直到ArrayList只包含一个项目,这应该是最大值。 What confuses me even more is that the loop executes the first time and then the exception is thrown. 令我困惑的是,循环首次执行然后抛出异常。 I'm guessing I'm not using recursion correctly or something but if someone could point me in the right direction it would be much appreciated! 我猜我没有正确使用递归或者某些东西,但如果有人能指出我正确的方向,我将非常感激!

I'm using this as a test client: 我正在使用它作为测试客户端:

public static void main(String... args) {
ArrayList<Integer> test = new ArrayList<Integer>();
test.add(12);
test.add(10);
test.add(65);
test.add(4);
test.add(78);
test.add(89);
test.add(99);
test.add(96);
test.add(24);
test.add(22);
ArrayList<Integer> testWinners = tournament(test);
System.out.println(testWinners);
ArrayList<Integer> testMaximum = maximum(test); 
System.out.println(testMaximum);


}

The following two lines will always throw an IndexOutOfBoundsException - which happens whenever your array size is odd: 以下两行将始终抛出IndexOutOfBoundsException - 只要您的数组大小为奇数,就会发生这种情况:

int n = tournamentArrayList.size();
//...
winners.add(tournamentArrayList.get(n));

Since list indexes start at 0, the last element in a list is at index size() - 1 . 由于列表索引从0开始,因此列表中的最后一个元素是索引size() - 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM