[英]Iterating through a list searching for duplicates
因此,我有一個任務,我必須做一個骰子滾子,然后搜索某個組合(如果滾動了)。 我有一個重寫的equals方法,該方法檢查組合是否正常。 class Dice
每個對象都有自己的字符串數組,其中包含有關組合滾動到哪個滾動的信息。 例如,將兩個滾動骰子(2, 4)
組合從5中滾出了第5個,因此其數組具有: [.., .., .., .., 5]
,然后存儲class Dice
每個對象在List<Dice>
,另一方面,它與每個骰子的字符串數組一起放入哈希圖中。 我的掙扎在於,我不知道如何遍歷骰子列表,並檢查每個組合是否滾動了一次以上,然后將有關滾動的信息放入第一個,然后刪除重復項。
例如,假設組合(4, 1)
在第一卷上滾動,然后在第四卷上滾動...其字符串數組應類似於: [1, .., .., 4, ..]
,哈希圖的打印顯示2個元素具有(4, 1)
組合和它們自己的數組:
[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]
問題出在您的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。
這樣做不是最好的方法,但是按上述方法進行更改將得到您想要的答案
我想知道我是否可以遵循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, .., .., .., ..]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.