【问题标题】:How to shuffle(Arraylist) without displaying a duplicate如何洗牌(Arraylist)而不显示重复
【发布时间】:2014-07-10 03:33:03
【问题描述】:

我想对 Arraylist 进行洗牌,并且再也不会出现相同的结果。

如何使用Collections.shuffle(myList); 做到这一点?

如果这是答案发布在其他地方,请告诉我。

编辑

这是我显示结果的方式 textView.setText(myList.get(0));

【问题讨论】:

  • 使用集合而不是列表。
  • 你能举个例子吗?我以前从未使用过set
  • Collections.shuffle 以什么方式“再次产生相同的结果”?你的意思是洗牌的结果永远不应该重复,即无论你洗一副牌多少次,它们的顺序都不会重复两次? (这显然是不可能的)
  • @a2800276 正确。我的收藏是一个单词列表,同一个单词偶尔会重复。
  • 然后使用一个Set,Set> ss = new HashSet>; ss.addAll(你的列表);这将确保您没有重复

标签: java android


【解决方案1】:

Collections.shuffle 不提供该功能。您需要执行单独的重复数据删除步骤。 Set 将提供该功能。

Set s = new HashSet(myList.size());
s.addAll(myList);
List shuffledList = new ArrayList(s.size());
shuffledList.addAll(s)
// Since items come out of a set in an undefined order, using shuffledList
// in this state may suit your needs. otherwise go ahead and shuffle it too
Collections.shuffle(shuffledList)
return shuffledList;

这里是a more advanced question on deduplication

【讨论】:

    【解决方案2】:

    Collections.shuffle 只会随机播放通过的列表。它不保持任何先前的状态。因此,如果您想避免重复,则必须保留先前生成的列表的状态并进行比较。或者,您可以找到所有可能的组合以及一一列出的访问权限。 Google Guava 库有查找组合的方法。 Collections2.permutations(myList) 例如

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    import com.google.common.collect.Collections2;
    
    public class Shuffle {
    
        public static void main(String args[]) {
            List<String> myList = new ArrayList<String>(Arrays.asList(new String[] {"a", "b", "c"}));
            for(List<String> list : Collections2.permutations(myList)){
                System.out.println(list);
            }
        }
    
    }
    

    这将提供所有非重复的随机播放。

    【讨论】:

      【解决方案3】:

      您可以创建一个额外的列表来存储以前使用的项目。每次你得到一个项目,你可以检查它是否已经被使用过(如果是,跳过它)。这样,您可以保留原始物品被使用的机会。如果您有 10 个 A 和一个 B,并且您将它们洗牌,那么您首先出现 A 的机会就更高......等等。如果您不想要此功能,我建议您使用 set。

      ArrayList<String> used = new ArrayList<String>();
      ArrayList<String> items = getItems();
      Collections.shuffle(items);
      
      public String getItem() {
        if(items.length == 0)
          return null;
        item = items.remove(0);
        if(used.contains(item)) 
          return getItem();
        return used.add(item)
      }
      

      在使用集合的情况下

      HashSet<String> items = new HashSet<String>(getItems());
      Collections.shuffle(items);
      
      public String getItem() {
        return items.remove(0);
      }
      

      【讨论】:

        【解决方案4】:

        如果我理解正确,您只想显示一个列表的随机值,在这种情况下使用它(无需打乱整个列表):

        int rand = (int)(array.size()*Math.random());
        textView.setText(myList.get(rand));
        

        编辑:

        将此函数与全局 tmp 变量一起使用:

        private List<String> tmpList = new ArrayList<String>();
        private String getRandom(List<String> myList){
            if (tmpList.size()<=0) tmpList = new ArrayList<String>(myList);
            int rand = (int)(tmpList.size()*Math.random());
            return tmpList.remove(rand);
        }
        

        并将值设置为:

        textView.setText(getRandom(myList));
        

        【讨论】:

        • 正确。此代码有效,但有时我仍会显示重复的结果。假设我的列表是 1、2、3、4、5,我永远不想生成相同的数字两次。
        • 然后记住上次显示的内容,如果再次出现,请选择下一项。或者,保留整个洗牌列表并一个接一个地显示。除非您在列表中添加了重复项,否则您不会得到重复项。
        【解决方案5】:

        感谢您的所有想法和建议!

        我的问题很愚蠢。

        textView.setText(myList.get(order++)); 已修复!

        【讨论】:

          猜你喜欢
          • 2013-04-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-04-07
          • 2017-01-26
          • 2019-06-03
          • 1970-01-01
          • 2013-02-24
          相关资源
          最近更新 更多