I am trying to write a method that determines the maximum value of a list of arrays using tournament style comparisons. However, I think I donβt understand something about loops because I cannot get the desired result and instead get an IndexOutOfBoundsException.
Here is 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 am trying to get ArrayList to constantly return to the tournament method until the ArrayList contains only one element, which should be the maximum. What else bothers me is that the loop is executed for the first time, and then an exception is thrown. I assume that I am not using recursion correctly or something, but if someone can point me in the right direction, it will be very appreciated!
I use 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); }
source share