【问题标题】:How do I ensure unique results when selecting random data?选择随机数据时如何确保唯一的结果?
【发布时间】:2023-03-15 06:34:01
【问题描述】:

我正在使用 XML 来存储短语数据库,其中一次将向用户显示其中的 5 个短语。我需要确保这 5 个短语是唯一的,当然,仅仅获取随机数据并不能确保这一点。如果我可以将我正在使用的字符串数组转换为列表,我想我可以做到这一点,但我找不到关于如何做到这一点的好信息。有人对此有意见吗?

public String getResults(){
    // Get a random string from our results XML and return the string.

    Resources r = getResources();
    String[] resultsList = r.getStringArray(R.array.bossResults); 
    List<String> resultsArrayList = new ArrayList<String>(Arrays.asList(resultsList));      
    //ArrayList resultsArrayList = ;
    String q = resultsList[rgenerator.nextInt(resultsList.length)];
    return q;
    //resultsList.remove(q);

}
    private OnClickListener mAddListener = new OnClickListener() 
{
    public void onClick(View v) 
    {

    //Declare our TextViews for population from the random results from XML
        TextView t1 = new TextView(getApplicationContext());
        t1=(TextView)findViewById(R.id.textView1); 
        TextView t2 = new TextView(getApplicationContext());
        t2=(TextView)findViewById(R.id.textView2);
        TextView t3 = new TextView(getApplicationContext());
        t3=(TextView)findViewById(R.id.textView3);
        TextView t4 = new TextView(getApplicationContext());
        t4=(TextView)findViewById(R.id.textView4);
        TextView t5 = new TextView(getApplicationContext());
        t5=(TextView)findViewById(R.id.textView5);

        // Get a random result for each textview
        String result1 = getResults();
        String result2 = getResults();
        String result3 = getResults();
        String result4 = getResults();
        String result5 = getResults();
}
}

【问题讨论】:

    标签: android xml random


    【解决方案1】:

    最简单的方法可能是重写 getResults() 以返回您需要的所有字符串(另外不要加载“bossResults”数组 5 次)。

    public List<String> getResults(int count){
      // Get a random string from our results XML and return the string.
      List<String> ret = new ArrayList<String>();
      Resources r = getResources();
      List<Integer> picked = new List<Integer>();
      String[] resultsList = r.getStringArray(R.array.bossResults); 
      while (ret.size() < count) {
        int i = rgenerator.nextInt(resultsList.length);
        if (picked.contains(i)) { continue; }
        picked.add(i);
        ret.add(resultsList[i]);
      }
      return ret;
    }
    

    count 的大值可能会很慢。

    编辑:如果count大于数组的大小,就会陷入无限循环!

    【讨论】:

    • 这看起来可能是最好的方法,但我还有另一个问题,因为我是个菜鸟。我如何从方法中调用它?
    • 嗯,getResults(5)?我想我不明白你的问题。
    • getResults(5) 将返回一个列表,对吗?然后我如何获取该列表并从中获取值?搜索android列表是徒劳的,请原谅我的无知:)
    • List&lt;String&gt; results = getResults(5); String result1 = results.get(1); ...
    • 嗯.. 我试过了,但 Eclipse 想将它转换为 String result1 = (String) results.get(1);它不起作用。它可以编译,但是当程序到达这部分代码时它会强制关闭。
    【解决方案2】:

    最简单的方法是记住您已经选择了哪些项目,如果您再次选择其中一项,只需将其丢弃。如果可供选择的项目数量很少,这可能会非常慢。如果是这种情况,您可以shuffle 整个数组并返回前 n 项。

    【讨论】:

      【解决方案3】:

      我将对 getResults() 进行更改,以便一次性为您完成所有工作。这样它就可以记住已经选择了哪些值,并缩小它在未来选择的值......就像这样:

      public List<String> getResults(int count) {
          List<String> results = new ArrayList<String>();
      
          Resources r = getResources();
          String[] resultsList = r.getStringArray(R.array.bossResults); 
          List<String> resultsArrayList = new ArrayList<String>(Arrays.asList(resultsList));      
      
          for(int i = 0; i < count; ++i) {
              int next = rgenerator.nextInt(resultsArrayList.size());
              String nextVal = resultsArrayList.remove(next);
              results.add(nextVal);
          }
      
          return results;
      }
      

      【讨论】:

      • 我开始写这个,但它是 O(n*m) 而不是“简单的替代方案” O(n+m^2)(提供 m
      猜你喜欢
      • 1970-01-01
      • 2016-05-09
      • 2018-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多