需要方法在Java中只返回匹配的数字 /** *此方法应比较两组整数并返回新的 *表示所有匹配数字的整数集。 * *例如,如果彩票号码是(4、6、23、34、44、45)和 *用户号是(4、18、22、24、35、45),然后是返回的集合 *整数的个数应为(4,45) * *@param lotteryNumber随机生成的彩票号码。 *@param userNumbers用户在控制台中拾取的号码。 *@返回匹配的数字集 */ 公共设置彩票(设置彩票号码、设置用户号码){ Set listOfRandom=新哈希集(乐透号码); 随机数等于(彩票号码)列表; 随机列表addAll(彩票号码); Set s=新的HashSet(userNumbers); s、 等于(用户编号); s、 addAll(用户编号); Set e=新的HashSet(); for(整数:userNumbers){ if(userNumbers.equals(lotteryNumbers)); 删除(乐透号码); } 返回用户号; }
到目前为止,它只返回所有的用户号码。我假设remove()方法将删除返回的任何重复值。我需要这个来通过我的单元测试。需要方法在Java中只返回匹配的数字 /** *此方法应比较两组整数并返回新的 *表示所有匹配数字的整数集。 * *例如,如果彩票号码是(4、6、23、34、44、45)和 *用户号是(4、18、22、24、35、45),然后是返回的集合 *整数的个数应为(4,45) * *@param lotteryNumber随机生成的彩票号码。 *@param userNumbers用户在控制台中拾取的号码。 *@返回匹配的数字集 */ 公共设置彩票(设置彩票号码、设置用户号码){ Set listOfRandom=新哈希集(乐透号码); 随机数等于(彩票号码)列表; 随机列表addAll(彩票号码); Set s=新的HashSet(userNumbers); s、 等于(用户编号); s、 addAll(用户编号); Set e=新的HashSet(); for(整数:userNumbers){ if(userNumbers.equals(lotteryNumbers)); 删除(乐透号码); } 返回用户号; },java,set,duplicates,Java,Set,Duplicates,到目前为止,它只返回所有的用户号码。我假设remove()方法将删除返回的任何重复值。我需要这个来通过我的单元测试。retainal()就是您要找的 /** * 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 lotteryNumbe
retainal()
就是您要找的
/**
* 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;
}
Set lotteryNumbers=new TreeSet();
// ... 用4,6,23,34,44,45填充它
Set userNumbers=new TreeSet();
// ... 用4,18,22,24,35,45填充它
用户号码。保留号码(乐透号码);
//用户编号现在仅为(4,45)
retainAll()
就是您要找的
/**
* 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;
}
Set lotteryNumbers=new TreeSet();
// ... 用4,6,23,34,44,45填充它
Set userNumbers=new TreeSet();
// ... 用4,18,22,24,35,45填充它
用户号码。保留号码(乐透号码);
//用户编号现在仅为(4,45)
谢天谢地,Java为此提供了一种方法,称为retainal()
。要避免破坏任一原始集,请执行以下操作:
Set<Integer> lotteryNumbers = new TreeSet<Integer>();
// ... Populate it with 4, 6, 23, 34, 44, 45
Set<Integer> userNumbers = new TreeSet<Integer>();
// ... Populate it with 4, 18, 22, 24, 35, 45
userNumbers.retainAll(lotteryNumbers);
// userNumbers is now just (4, 45)
Set intersection=新哈希集(lotteryNumber);
交叉口。保留(用户编号);
折返交叉口;
谢天谢地,Java为此提供了一种方法,称为retainal()
。要避免破坏任一原始集,请执行以下操作:
Set<Integer> lotteryNumbers = new TreeSet<Integer>();
// ... Populate it with 4, 6, 23, 34, 44, 45
Set<Integer> userNumbers = new TreeSet<Integer>();
// ... Populate it with 4, 18, 22, 24, 35, 45
userNumbers.retainAll(lotteryNumbers);
// userNumbers is now just (4, 45)
Set intersection=新哈希集(lotteryNumber);
交叉口。保留(用户编号);
折返交叉口;
您也可以用于类似的操作。具体来说,您可以使用CollectionUtils.intersection()
泛型版本位于,您也可以用于类似的操作。具体来说,您可以使用CollectionUtils.intersection()
仿制药的版本在谢谢我在这方面打败了我的大脑几个小时。谢谢我在这方面打败了我的大脑几个小时。谢谢我真的很感激。谢谢我真的很感激。