Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ArrayList While循环提供IndexOutOfBoundsException_Java_Arraylist_While Loop_Indexoutofboundsexception - Fatal编程技术网

Java ArrayList While循环提供IndexOutOfBoundsException

Java ArrayList While循环提供IndexOutOfBoundsException,java,arraylist,while-loop,indexoutofboundsexception,Java,Arraylist,While Loop,Indexoutofboundsexception,我正在尝试编写一个方法,使用锦标赛样式的比较来确定数组列表的最大值。但是,我想我不了解while循环,因为我无法获得所需的输出,而是获得IndexOutOfBoundsException 这是我的密码: import java.util.*; public class TournamentMax { public static <T extends Comparable<? super T>> ArrayList<T> tournament(ArrayLis

我正在尝试编写一个方法,使用锦标赛样式的比较来确定数组列表的最大值。但是,我想我不了解while循环,因为我无法获得所需的输出,而是获得IndexOutOfBoundsException

这是我的密码:

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;


}

}
在我的头脑中,我试图使ArrayList不断地传递回锦标赛方法,直到ArrayList只包含一个项目,这应该是最大值。更让我困惑的是,循环第一次执行,然后抛出异常。我猜我没有正确地使用递归或其他方法,但是如果有人能给我指出正确的方向,我将不胜感激

我将此用作测试客户端:

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);


}
publicstaticvoidmain(字符串…参数){
ArrayList测试=新的ArrayList();
试验。添加(12);
试验。添加(10);
增加(65);
试验。添加(4);
试验。添加(78);
增加(89);
增加(99);
试验。添加(96);
测试。添加(24);
试验。添加(22);
ArrayList testWinners=锦标赛(测试);
System.out.println(testWinners);
ArrayList testMaximum=最大值(测试);
System.out.println(testmax);
}

以下两行将始终引发IndexOutOfBoundsException-每当数组大小为奇数时就会发生这种情况:

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

由于列表索引从0开始,列表中的最后一个元素位于索引
size()-1

,这也解释了为什么循环第一次执行良好。在那一刻,名单仍然有一个均匀的大小,当然!我现在确实觉得有点傻。。。谢谢你抓住了!TL DR用这个代码重的例子;直接转到Sbodd的答案,并查看“由于列表索引从0开始,列表中的最后一个元素的索引大小为()-1”的解释
int n = tournamentArrayList.size();
//...
winners.add(tournamentArrayList.get(n));