I am trying to create a fixture list for a group of 6 teams using hashmaps and arrays. I have an array of 6 teams. Each team must play each other twice. I am removing each team at random from the original list of teams and adding them to a new list. This new list is added to a hashmap. In order to avoid the same list of fixtures occuring more than once i am trying to check this new list before adding it to the hashmap, if it already exists then i dont add it. here is my code so far:
List<String> testList = new ArrayList<String>();
List<String> tempList = new ArrayList<String>();
Random myRandomizer = new Random();
String random1,random2;
tempOrder = new ArrayList<ArrayList<String>>();
HashMap<Integer, ArrayList<String>> map = new HashMap<Integer, ArrayList<String>>();
testList.add("team1");
testList.add("team2");
testList.add("team3");
testList.add("team4");
testList.add("team5");
testList.add("team6");
int x = (testList.size()-1) * 2;
int y = 0;
while(map.size() < 10){
System.out.println("Match Day " + (y+1));
//while(tempOrder.size()<10){
// System.out.println("Match Day " + (tempOrder.size()+1));
while(testList.size()>0){
random1 = testList.get(myRandomizer.nextInt(testList.size()));
testList.remove(random1);
tempList.add(random1);
random2 = testList.get(myRandomizer.nextInt(testList.size()));
testList.remove(random2);
tempList.add(random2);
System.out.println( random1 + " V " + random2 + "\n");
}
//tempOrder.add((ArrayList<String>) tempList);
// add to hashmap
// check value exists
// if true add
// if not dont
if(!(map.containsValue(tempList))){
y++;
map.put(y, (ArrayList<String>) tempList);
for(String s: tempList){
testList.add(s);
}
tempList.clear();
//tempOrder.clear();
}
else if((map.containsValue(tempList))){
//System.out.println("issue");
//tempOrder.clear();
for(String s: tempList){
testList.add(s);
}
tempList.clear();
}
I am getting an infinite loop at the moment when i run this code, can someone please help? i think its the right idea but maybe the wrong execution, Is this the correct way to do this?
Thanks in advance