Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在for循环中添加到HashSet时,如何删除重复项,同时用另一个对象替换它们?_Java_Android_Arraylist_Hashset - Fatal编程技术网

Java 在for循环中添加到HashSet时,如何删除重复项,同时用另一个对象替换它们?

Java 在for循环中添加到HashSet时,如何删除重复项,同时用另一个对象替换它们?,java,android,arraylist,hashset,Java,Android,Arraylist,Hashset,问题 我正在建立一个应用程序,可以创建抽奖抽奖从arrayList的条目。开始抽奖时,用户可以输入一个整数,该整数表示抽奖将返回多少名优胜者。应用程序使用randomGenerator从条目arrayList中选择一个随机索引。这个随机生成器位于for循环中,该循环检查它被迭代了多少次,并将其与numberOfWinners整数进行比较 假设用户插入“3”作为numberOfWinners的整数。该应用程序将返回三个随机条目,但有时会重复 所以我很自然地研究了如何避免重复。我遇到了HashSet

问题

我正在建立一个应用程序,可以创建抽奖抽奖从arrayList的条目。开始抽奖时,用户可以输入一个整数,该整数表示抽奖将返回多少名优胜者。应用程序使用randomGenerator从条目arrayList中选择一个随机索引。这个随机生成器位于for循环中,该循环检查它被迭代了多少次,并将其与numberOfWinners整数进行比较

假设用户插入“3”作为numberOfWinners的整数。该应用程序将返回三个随机条目,但有时会重复

所以我很自然地研究了如何避免重复。我遇到了HashSet。如果将两个重复条目添加到哈希集中,则只添加一个唯一条目。然后,我将这个HashSet转换成一个新的ArrayList,并在我的Android视图中以列表视图的形式显示它

然而,HashSet并没有解决这个问题。如果用户输入numberOfWinners的输入“3”,并且存在重复项,则最终结果将只有2个条目

我怎样才能解决这个问题

以下是我的一段代码:

Set drawSet=new HashSet();
//从条目arrayList的大小中选择随机整数
Intent viewDrawIntent=getIntent();
int currentRaffleID=viewDrawIntent.getIntExtra(“raffleIndexInList”,0);
int newNoOfWinners=viewDrawIntent.getIntExtra(“extraNoOfWinners”,0);
抽奖券currentRaffle=Raffle.raffleArrayList.get(currentRaffleID);
对于(int i=1;i<(currentRaffle.getEntryArrayList().size());i++){
如果(i!=newNoOfWinners){
int randomInt=randomGenerator.nextInt(currentRaffle.getEntryArrayList().size());
Entry randomEntry=(currentRaffle.getEntryArrayList()).get(randomInt);
//将新条目添加到entryDraw数组
drawSet.add(随机输入);
}
否则{
}
}

ArrayList entryDraw=新的ArrayList(drawSet)您可以通过循环来修复此问题,直到集合包含所需的赢家数量

工作示例:

List<Integer> tickets = Arrays.asList(1, 2, 3, 4, 5);
Set<Integer> winners = new HashSet<Integer>();
Random random = new Random();
while (winners.size() < 3) {
    winners.add(tickets.get(random.nextInt(tickets.size())));
}
List tickets=Arrays.asList(1,2,3,4,5);
Set winners=新的HashSet();
随机=新随机();
while(winners.size()<3){
winners.add(tickets.get(random.nextInt(tickets.size())));
}

您可以使用HashSet让您知道ArrayList的索引是否已被使用

如果要从列表中选取3个唯一索引,则需要初始化一个哈希集,以包含ArrayList的所有索引。然后,每次绘制随机索引时,都要检查哈希集是否包含该索引

  • 如果是这样,您就知道这个索引还没有被使用过,您可以将它从HashSet中删除
  • 如果没有,则该索引已被使用,因此再次绘制随机索引

重复此过程,直到有3个以前从未使用过的唯一索引。

是否正在检查该列表是否已严格必要?当然,如果你只是向一个集合中添加一个现有值,那么它就没有效果了…@Adam我的建议是从一个完整的集合(包含所有索引)开始,并在使用后删除每个索引,因此,在删除索引之前,您必须知道索引是否存在。该方法的问题是,如果删除索引,则无法再次使用同一arrayList,索引都会被弄乱up@user3105544我没有建议从ArrayList中删除索引。我建议从哈希集中删除它。@user3105544谢谢,这能解决您的问题吗?