简体   繁体   English

Android随机化字符串而无需重复

[英]Android randomize strings without repetition

I'm doing a simple random quiz app in Android. 我正在Android中做一个简单的随机测验应用程序。 So basically I have string array of words. 所以基本上我有单词的字符串数组。 I need to display the string without repetition. 我需要显示字符串而不重复。 This is what I've tried so far: 到目前为止,这是我尝试过的:

String[] words = { "Welcome", "Different", "Teenager", "Transfer", "Italian", 
                   "Timber", "Toxic", "Illiterate", "Irate", "Moderate", "Transportation", "Attention" };


ArrayList<String> wordlist = new ArrayList<String>();
for (String i : words) 
    wordlist.add(i);
Collections.shuffle(wordlist);

randomStr = words[new Random().nextInt(words.length)];

tvWord.setText("");
tvWord.setText(randomStr);

But I still get the random word repeating. 但是我仍然会重复随机单词。 What am I doing wrong in here? 我在这里做错了什么? Any ideas? 有任何想法吗? I would gladly appreciate your help. 非常感谢您的帮助。 Thanks. 谢谢。

Update: 更新:

First on a button click the word should then display. 首先在按钮上单击,然后显示单词。 And many times I click the button I keep getting the same word again. 很多次我单击按钮后,我都会再次得到相同的单词。

 switch(v.getId()){
    case R.id.btPlay:
        randomWordList();
        break;
 }

Where randomWordList(); 其中randomWordList(); is the method I posted above. 是我上面发布的方法。

You don't keep track of what was already used. 您不会跟踪已使用的内容。 I also don't understand why you define wordlist and then don't use it. 我也不明白为什么要定义单词列表然后不使用它。 Just a small modification of your code will do the trick. 只需对您的代码进行少量修改即可解决问题。

Edit : You also need to initialize values in different scope than where you use it. 编辑 :您还需要在与使用它的地方不同的范围内初始化值。 For example like this: 例如这样:

public class MyClass{

LinkedList<String> wordlist;

public MyClass(){
    String[] words = { "Welcome", "Different", "Teenager", "Transfer", "Italian", 
               "Timber", "Toxic", "Illiterate", "Irate", "Moderate", "Transportation", "Attention" };
    wordlist = new LinkedList<String>();
    for (String i : words) 
        wordlist.add(i);
    Collections.shuffle(wordlist);
}

public void useNextWord(){
    tvWord.setText("");
    tvWord.setText(wordlist.pollLast());
}

Create a new list of Strings and shuffle it. 创建一个新的Strings列表并对其进行随机排序。 Then simply iterate over the shuffled list from the beginning. 然后,只需从头开始遍历洗牌列表即可。

ArrayList<String> copyOfWords= new ArrayList<String>(words);
Collections.shuffle(copyOfWords);

This is taken from here . 这是从这里拿来的。 Time complexity is O(n). 时间复杂度为O(n)。

To iterate over the List you can either get the first element and remove it (with the same method) or keep track which was the last used element. 要遍历List您可以获取第一个元素并将其删除(使用相同的方法),或者跟踪最后使用的元素。

First solution: 第一个解决方案:

String randomWord = copyOfWords.remove(0);
tvWord.setText(randomWord);

PS do not use this line in your code randomStr = words[new Random().nextInt(words.length)]; PS请勿在您的代码中使用此行randomStr = words[new Random().nextInt(words.length)];

You will need to either keep track of the Strings you have already used in another array or list and check the value has not already been used or alternatively remove any used Strings from the pool. 您将需要跟踪已在另一个数组或列表中使用过的字符串,并检查该值尚未使用,或者从池中删除所有已使用的字符串。 You can do that with String next = List.remove(index) . 您可以使用String next = List.remove(index)

You first shuffle your list with Collections.shuffle(wordlist); 首先,您可以使用Collections.shuffle(wordlist);随机排列Collections.shuffle(wordlist); and then access the list with a random index. 然后使用随机索引访问列表。 There's no need to use a random index if the list is already shuffled. 如果列表已经被改组,则无需使用随机索引。 Just get one item after another. 只是一个接一个。 You could use wordlist.remove(0) until your list is empty. 您可以使用wordlist.remove(0)直到列表为空。

Display the next string as follows: 显示下一个字符串,如下所示:

if (!wordlist.isEmpty()) {
    randomStr = wordlist.remove(0);
    tvWord.setText(randomStr);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM