【问题标题】:How to avoid random strings from appearing repeatedly如何避免随机字符串重复出现
【发布时间】:2013-01-22 07:46:02
【问题描述】:

我喜欢将字符串数组中的一些字符串随机显示到 Textview 中。我正在使用以下代码,但问题是大多数时候重复显示某些字符串。我想要的是曾经显示的字符串不应该再次显示。我花了几个小时搜索代码,但没有其中为我工作。请帮忙。提前致谢。

public void GetQuotes(View view) {
     Resources res = getResources();
            myString = res.getStringArray(R.array.Array);
            String q = myString[rgenerator.nextInt(myString.length)];                               
            TextView tv = (TextView)findViewById(R.id.textView1);               
            tv.setText(q);  

【问题讨论】:

  • 啊,不错的伪随机。这个怎么样?创建两个字符串列表。在 list1 上使用随机数来提取字符串。将其从 list1 中删除并放入 list2。你的下一个随机肯定不会与上一个相同。完成从 list1 中提取最后一个字符串后,将 list2 中的所有字符串放回列表 1 并清空 list2 并重新开始?
  • 我不知道该怎么做。你能推荐一个不太复杂的方法吗

标签: android random repeat arrays


【解决方案1】:

Java 内置了数组混洗方法,将所有项目放入一个列表中,随机混洗,并获取第一个元素直到它有元素。如果为空,则再次添加所有元素,然后再次洗牌:

private List<String> myString;

public void GetQuotes(View view) {
    Resources res = getResources();
    if (myString==null || myString.size()==0) {
        myString = new ArrayList<String>();
        Collections.addAll(myString, res.getStringArray(R.array.Array));
        Collections.shuffle(myString); //randomize the list
    }
    String q = myString.remove(0);
    TextView tv = (TextView)findViewById(R.id.textView1);
    tv.setText(q);
}

【讨论】:

    【解决方案2】:

    我建议要么手动检查它之前是否使用过,要么使用一个集合,然后在该集合中写入字符串。

    http://developer.android.com/reference/java/util/Set.html

    【讨论】:

    • 如果您想要代码,请聘请程序员。 Gauravs 的回答向您展示了如何创建一个集合。如果你看的话,肯定会有很多关于如何使用套装的教程。
    • 谢谢。我是 Android 的初学者并且正在学习东西。我会尝试设置。
    【解决方案3】:

    数组和列表通常不是为了避免重复而设计的,它们被设计为一种保持许多元素顺序的集合。如果您想要一个更适合该工作的集合,那么您想要一个集合:

     Set<String> set = new HashSet<String>();
    

    避免重复。

    【讨论】:

    • 谢谢你,我会尽力回复你
    【解决方案4】:

    这是一个非常简单的解决方案。

    现在,当我说这话时,如果您想要一个简单的解决方案,您可以有一个专用的字符串变量来存储最后使用的问题。那么如果你把它初始化为一个空字符串就变得很简单了。假设变量是最后调用的。

    String q = myString[rgenerator.nextInt(myString.length)]; 
    //q has a string which may or may not be the same as the last one
    //the loop will go on until this q is different than the last
    //and it will not execute at all if q and last are already different
    while (last.equals(q))
    {
        //since q and last are the same, find another string
        String q = myString[rgenerator.nextInt(myString.length)]; 
    };
    //this q becomes last for the next time around
    last = q;
    

    现在在其他几个问题中,要记住的关键是,这只是确保 q[1] 不能跟随 q[1],但它并不能完全避免这样的情况,只是为了荒谬,说 q [1]、q[2]、q[1]、q[2] 等等。

    这是一个同样简单的 ArrayList。

    List<String> list1 = new ArrayList<String>();
    List<String> list2 = new ArrayList<String>();
    for (int i = 0; i < myString.length)
    {
        list1.add(myString[i]);
    }
    q = (String)list1.get(rgenerator.nextInt(list1.size()));
    list1.remove(q);
    list2.add(q);
    if (list1.isEmpty())
    {
        list1.addAll(list2);
        list2.clear();
    }
    

    【讨论】:

    • 我的模拟器在 Win7 中经常崩溃。所以我已经为 Ubuntu 下载了它。现在好了。我试过你的溶胶,没有错误,但运行时,应用程序崩溃。但幸运的是 Daniel Fekete 的解决方案对我有用。非常感谢
    • +1 用于 Linux。另外,我很高兴他的解决方案为您工作。那么请选择他的答案。
    猜你喜欢
    • 2014-02-05
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 2019-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多