【问题标题】:How to get strings from array randomly without repeating the string?如何从数组中随机获取字符串而不重复字符串?
【发布时间】:2019-04-01 00:09:38
【问题描述】:

我正在制作一个测验应用程序我有字符串数组,我想在按钮单击时加载它们,字符串应该随机加载到文本视图中。字符串不应重复。

谢谢..

【问题讨论】:

  • 使用Collections.shuffle(),每次获取索引递增的值,直到达到边界。

标签: android arrays string


【解决方案1】:

因为这是你的测验,我会给你提示而不是整个解决方案

Random rnd = new Random;
while(array.size()>0){
    // 1. use rng to get a index between 0 to current array.size()
    // 2. remove the string by array.remove(index) so that it won't duplicated.
    // 3. setText(string)
}

试试这个,如果需要更多详细信息,可以添加 cmets。

【讨论】:

    【解决方案2】:

    一种方法是将数组转换为列表并对其进行洗牌。

    List myList = Arrays.asList(yourArray);
    Collections.shuffle(myList);
    

    之后,您可以遍历列表。

    【讨论】:

      猜你喜欢
      • 2021-03-06
      • 2019-07-16
      • 1970-01-01
      • 2012-06-09
      • 1970-01-01
      • 1970-01-01
      • 2011-02-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多