0
/**
 * This method should compare two Sets of Integers and return a new 
 * Set of Integers that represent all of the matching numbers.
 * 
 * For example, if the lotteryNumbers are (4, 6, 23, 34, 44, 45) and
 * the userNumbers are (4, 18, 22, 24, 35, 45) then the returned Set
 * of Integers should be (4, 45)
 * 
 * @param lotteryNumbers the lottery numbers that were randomly generated.
 * @param userNumbers the user picked numbers that were picked in the console.
 * @return Set of matched numbers
 */
public Set<Integer> playLottery (Set<Integer> lotteryNumbers, Set<Integer> userNumbers)  {
    Set<Integer> listOfRandom = new HashSet<Integer>(lotteryNumbers);
    listOfRandom.equals(lotteryNumbers);
    listOfRandom.addAll(lotteryNumbers);

    Set<Integer> s = new HashSet<Integer>(userNumbers); 
    s.equals(userNumbers);
    s.addAll(userNumbers);

    Set<Integer> e = new HashSet<Integer>(); 

    for (Integer integer : userNumbers) {
        if (userNumbers.equals(lotteryNumbers));
        userNumbers.remove(lotteryNumbers);
    }
    return userNumbers;
}

現在のところ、すべての userNumbers のみが返されます。remove() メソッドは、返された重複した値を削除すると想定しました。単体テストに合格するにはこれが必要です。

4

3 に答える 3

1

これらと同様の操作にApache Commons - Collectionsを使用することもできます。具体的には、 CollectionUtils.intersection() を使用できます

CollectionUtils.intersection(Arrays.asList(4,6,23,34,44,45),Arrays.asList(4,18,22,24,35,45)) // returns collection with 4,45

ジェネリック バージョンはhttp://sourceforge.net/projects/collections/にあります。

于 2013-09-19T03:43:10.910 に答える