迭代搜索重复的列表

时间:2015-06-12 10:28:31

标签: java list collections iterator

所以我有这个任务,我必须做一个骰子,然后搜索某个组合,如果它已滚动。我有一个覆盖等于方法,检查组合,它正常工作。来自class Dice的每个对象都有自己的字符串数组,其中包含有关哪个卷是滚动组合的信息。例如,两个掷骰子(2, 4)的组合在第5卷上滚动,因此其数组包含:[.., .., .., .., 5]然后class Dice中的每个对象都存储在List<Dice>中另一方面,它被放入每个骰子的字符串数组旁边的散列图中。 我的挣扎是我无法理解如何迭代骰子列表并检查每个组合是否被多次滚动并将有关哪个卷的信息放入第一个,然后删除副本。

例如,假设组合(4, 1)已在第一个卷上滚动,然后在第4卷上滚动...其字符串数组应如下所示:[1, .., .., 4, ..],而不是打印出的hashmap包含(4, 1)组合的2个元素及其自己的数组:

[1, .., .., .., ..][.., .., .., 4, ..]

我希望你理解我的挣扎。

public class Dice {
  private int firstDice;
  private int secondDice;
  public String[] rollArray;
  public int roll;
  public int duplicate = 1; 

  /**
   * Constructor for the class Dice.
   * @param first first dice
   * @param second second dice
   */
  public Dice(int first, int second) {
    firstDice = first;
    secondDice = second;
  }

  @Override
  public String toString() {
    return "(" + firstDice + ", " + secondDice + ")";
  }

  /**
   * Method equals used for comparing two objects from the class Dice.
   * @param obj object from dice class
   * @return returns true/false if conditions are matched.
   */
  public boolean equals(Dice obj) {
    return (obj.firstDice == firstDice && obj.secondDice == secondDice);
  }
}



import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

/**
 * Created by leo on 6/10/15. Class which contains all methods that realize the rolling of two dices
 * and storing the information about them in a hash map.
 */
public class DiceRoller {

  public List<Dice> diceList = new LinkedList<>();
  public List<String> rollingList = new LinkedList<>();

  /**
   * A method which rolls two dices a number of times with random values.
   *
   * @param numberOfRolls number of rolls
   */
  public void rollDice(int numberOfRolls) {
    Random rand = new Random();
    for (int i = 0; i < numberOfRolls; i++) {
      diceList.add(i, new Dice(rand.nextInt(7 - 1) + 1, rand.nextInt(7 - 1) + 1));
      diceList.get(i).rollArray = new String[numberOfRolls];
      diceList.get(i).roll = i + 1;
      diceList.get(i).rollArray[i] = diceList.get(i).roll + "";
      rollingList.add("" + (i + 1));
      checkDuplicateDice(diceList, diceList.get(i));
    }
  }


  private void checkDuplicateDice(List<Dice> listOfDice, Dice tempDice) {
    /*
     * for (int i = 0; i < listOfDice.size(); i++) { for (int j = i + 1; j < listOfDice.size(); j++)
     * { if (listOfDice.get(i).toString().equals(listOfDice.get(j).toString())) {
     * listOfDice.get(i).duplicate++; } } } for (int i = 0; i < listOfDice.size(); i++) {
     * System.out.println(listOfDice.get(i).toString() + listOfDice.get(i).duplicate); }
     */
    Iterator<Dice> iter = listOfDice.iterator();
    while (iter.hasNext()) {
      Dice elem = iter.next();
      if (elem.toString().equals(tempDice.toString())) {
        elem.duplicate++;
      }
      System.out.println(elem.toString() + elem.duplicate);
    }
  }

  /**
   * A method which checks if the combination entered is rolled.
   *
   * @param first first dice
   * @param second second dice
   */

  public void checkCombination(int first, int second) {
    Dice checker = new Dice(first, second);
    int index = 1;
    boolean flag = false;
    for (Dice diceObject : diceList) {
      diceObject.rollArray = new String[diceList.toArray().length];
      diceObject.rollArray[index - 1] = index + "";
      for (int i = 0; i < diceList.size(); i++) {
        if (diceObject.rollArray[i] == null) {
          diceObject.rollArray[i] = "..";
        }
      }

      if (diceObject.equals(checker)) {
        System.out.println("Combination: (" + first + ", " + second + ") rolled on roll No: "
            + index);
        flag = true;
      }
      index++;
    }
    if (!flag) {
      System.out.println("Combination not rolled.");
    }
  }

  /**
   * A method which stores the data of the dice and each dice'.
   */
  public void hashMapThingy() {
    System.out.print("Roll: ");
    for (int i = 0; i < rollingList.size(); i++) {
      System.out.print((i + 1) + " ");
    }

    System.out.print("\n");
    System.out.println("Comb:");
    HashMap<Dice, String[]> hm = new HashMap<>();
    for (Dice diceObject : diceList) {
      hm.put(diceObject, diceObject.rollArray);
    }

    Set<Map.Entry<Dice, String[]>> set = hm.entrySet();
    for (Map.Entry<Dice, String[]> me : set) {
      System.out.println(me.getKey() + " " + Arrays.toString(printArray(me.getValue())));
    }
  }

  /**
   * Printer method.
   * 
   * @param array array that contains the roll number
   * @return returns the array string
   */
  public String[] printArray(String[] array) {
    return array;
  }
}


public class Test {
  /**
   * Main function.
   * 
   * @param args arguments
   */
  public static void main(String[] args) {
    int number = 5;
    DiceRoller diceRoller = new DiceRoller();
    diceRoller.rollDice(number);
//    Dice.fillDiceList();

//    Dice.printListDices();
    diceRoller.checkCombination(3, 2);
    diceRoller.checkCombination(1, 3);
    diceRoller.checkCombination(6, 3);
    diceRoller.hashMapThingy();
  }
}

当前控制台输出:

(5, 1)2
(5, 1)2
(1, 1)2
(5, 1)3
(1, 1)2
(5, 1)2
(5, 1)3
(1, 1)2
(5, 1)2
(1, 5)2
(5, 1)3
(1, 1)2
(5, 1)2
(1, 5)2
(4, 4)2
Combination not rolled.
Combination not rolled.
Combination not rolled.
Roll: 1 2 3 4 5 
Comb:
(1, 1) [.., 2, .., .., ..]
(1, 5) [.., .., .., 4, ..]
(5, 1) [1, .., .., .., ..]
(5, 1) [.., .., 3, .., ..]
(4, 4) [.., .., .., .., 5]

2 个答案:

答案 0 :(得分:0)

问题在于checkDuplicateDice方法

  private void checkDuplicateDice(List<Dice> listOfDice, Dice tempDice) {
  boolean duplicate = false;
  for (Dice elem : listOfDice) {
      if (elem.roll != tempDice.roll && elem.toString().equals(tempDice.toString())) {
            elem.duplicate++;
            elem.rollArray[tempDice.roll-1] = tempDice.roll + "";
            duplicate = true;
        }
  }
  if(duplicate)
      listOfDice.remove(tempDice.roll -1);

}

如上所述,您需要更新rollArray并将其发回,以便更新rollArray。

这样做并不是最好的方法,但如上所述更改将获得您想要的答案

答案 1 :(得分:0)

我想知道我是否可以遵循Sujit Chaitanya的逻辑,我想为什么方法不会从Dice类返回一个对象,而不是void然后使用该对象安全地删除它而没有例外..我修改了一下它变成了非常好。

private Dice checkDuplicateDice(List<Dice> listOfDice, Dice tempDice) {

boolean duplicate = false;
for (Dice elem : listOfDice) {
  if (elem.roll != tempDice.roll && elem.toString().equals(tempDice.toString())) {
    elem.rollArray[tempDice.roll - 1] = tempDice.roll + "";
    duplicate = true;
  }
}
if (duplicate) {
  return tempDice;
}
return null;

}

rollDice方法中,我在第一个之后插入了一个新的for循环:

for (int j = 0; j < diceList.size(); j++) {
  if (checkDuplicateDice(diceList, diceList.get(j)) != null) {
    diceList.remove(j);
  }
}

我还修改了checkCombination方法以不覆盖数组。我添加了一个新的全局变量listSize,它在所有骰子被滚动后立即取diceList.size(),这样在删除骰子后它不会改变。我在循环中使用listSize以正确标记字符串数组中不包含值的元素(null

public void checkCombination(int first, int second) {
Dice checker = new Dice(first, second);
int index = 1;
boolean flag = false;
for (Dice diceObject : diceList) {

  for (int i = 0; i < listSize; i++) {
    if (diceObject.rollArray[i] == null) {
      diceObject.rollArray[i] = "..";
    }
  }

  if (diceObject.equals(checker)) {
    System.out.println("Combination: (" + first + ", " + second + ") rolled on roll No: "
        + index);
    flag = true;
  }
  index++;
}
if (!flag) {
  System.out.println("Combination not rolled.");
}

}

然后输出如下:

Roll: 1 2 3 4 5 
Comb:
(2, 1) [.., .., 3, .., ..]
(3, 6) [.., 2, .., 4, 5]
(6, 5) [1, .., .., .., ..]
相关问题