0

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

1
  • 5
    Please format your code - it's much harder to read without indentation. Ideally, you should then convert it to a short but complete program demonstrating the problem. I suspect you can shorten what you've already got, and you can definitely remove the randomness and wrap it up in a complete program Commented Dec 12, 2014 at 14:51

2 Answers 2

2

The problem is that you are creating tempList at the beginning, so tempList will always be the same, and map.containsValue(tempList) will always return true.

Move tempList creation/declaration at the beginning of your while loop:

while (map.size() < 10) {
    List<String> tempList = new ArrayList<String>();
    //...
}
Sign up to request clarification or add additional context in comments.

Comments

0

You add tempList to your map each time it's unique, but then you call tempList.clear() and wipe it out.

Try this:

    String random1,random2;
    HashMap<Integer, ArrayList<String>> map = new HashMap<Integer, ArrayList<String>>();
    Random myRandomizer = new Random();

    List<String> testList = new ArrayList<String>();
    testList.add("team1");
    testList.add("team2");
    testList.add("team3");
    testList.add("team4");
    testList.add("team5");
    testList.add("team6");

    int y = 0;

    while(map.size() < 10) {
        System.out.println("Match Day " + (y + 1));

        List<String> tempList = new ArrayList<String>();

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

        if (!(map.containsValue(tempList))) {

            y++;
            map.put(y, (ArrayList<String>) tempList);
            for (String s : tempList) {
                testList.add(s);
            }

        } else if ((map.containsValue(tempList))) {
            for (String s : tempList) {
                testList.add(s);
            }
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.