【问题标题】:How to select randomly from a list of string-array如何从字符串数组列表中随机选择
【发布时间】:2015-06-10 12:23:11
【问题描述】:

我有一个字符串数组列表:

<string-array name="chapter_1">
      <item>......</item>
      .           
      .
      .
      .
</string-array>

<string-array name="chapter_2">
      <item>......</item>
      .           
      .
      .
      .
</string-array>

等等。 我有一个随机的 int chapter_no。

我有一个字符串:

String chapter_name = "chapter_" + chapter_no;

现在,我想访问chapter_no对应的字符串数组。

我知道我不能这样做:

String[] 章节 = getResources().getStringArray(R.array.chapter_name);

如何随机访问字符串数组?请帮帮我。

【问题讨论】:

  • 你想要一个随机字符串数组还是来自已知章节的随机字符串?
  • 首先我想获取字符串数组,然后是章节中的字符串。让我说我想要章节中的随机字符串( item )。
  • 你想要任何特定章节或所有章节中的随机字符串(项目)吗?
  • @learner 无法遍历元素并找到您要查找的元素?
  • 如果我对你的理解正确,那么@Moinkhan 的回答应该可以帮助你实现你想要的

标签: java android arrays xml


【解决方案1】:

从 chapter_array 中获取随机字符串 ..

String[] chapter = getResources().getStringArray(R.array.chapter_name);

String randomString = chapter[new Random(chapter.length).nextInt()];

[更新]

        String[] data;

        //first of all give all resources to arrays
        int[] arrays = {R.string.chapter_1, R.string.chapter_2};

        //then we are fetching any one string array resources randomly
        int chapter_no = arrays[new Random(arrays.length).nextInt()];

        //From above we got any one random resource string..So we fetch all string items from that resource into data array
        data = getResources().getStringArray(chapter_no);

        //Now from that data array we fetch any one random string.
        String randomString = data[new Random(data.length).nextInt()];

[更新]

    String[] data;
    int[] arrays = {R.array.chapter_1, R.array.chapter_2};
    int chapter_no = arrays[new Random().nextInt(arrays.length-1)];
    data = getResources().getStringArray(chapter_no);
    String randomString = data[new Random().nextInt(data.length-1)];
    Toast.makeText(this, randomString, Toast.LENGTH_SHORT).show();

【讨论】:

  • 我想确定一件事,... chapter_name 是否对您可用....???
  • chapter_name 是一个变量,而不是一个常量。请通过问题。它根本不起作用,我试过了..
  • 是的,我在这里得到了这样的解决方案:stackoverflow.com/questions/12139015/… 但我不想做一个数组,因为有 100 章。
  • 但是如果你说的是 100 章,那么你应该使用 Sqlite ......这是你的面糊选择..
  • 我知道。但我使用的是字符串数组。并且还使用数组而不是字符串作品..
【解决方案2】:

一次尝试如下

更新

int id = getResources().getIdentifier(chapter_name, "array",this.getPackageName()); 
String[] chapter = getResources().getStringArray(id);

希望对你有帮助。

【讨论】:

【解决方案3】:

如果您只有 2 个字符串并且想要随机获取它们,您可以这样做:

Random random = new Random();

int chapter_no = random.nextInt(2) + 1;
String chapter_name = "chapter_" + chapter_no;

我在上面放置的这个Random 函数将获得直到数字 2 的值。

希望对你有帮助!

【讨论】:

  • 我有两个以上的字符串数组和对应的章节号。
  • @learner 你知道你有多少字符串数组吗?
  • @learner 然后您可以将 nextInt 中的数字更改为您拥有的字符串数组的数量(总数)。 random.nextInt(total) + 1;
【解决方案4】:

创建一个包含所有这些数组 ID 的数组,然后在该数组中选择其中一个。

int[] arrays = {R.array.chapter_1,....};

从这些数组中随机选择一个。

 Random random = new Random();
 int chapter_no = arrays[random.nextInt(arrays.length)];

【讨论】:

【解决方案5】:

你可以这样做:

String[] chapter = getResources().getStringArray(R.array.chapter_1);
String[] chapter2 = getResources().getStringArray(R.array.chapter_2);
Random rand=new Random();
int randomNo=rand.nextInt();


ArrayList<String> temp = new ArrayList<String>();
temp.addAll(Arrays.asList(chapter));
temp.addAll(Arrays.asList(chapter2));
String [] concatedArgs = temp.toArray(new String[chapter.length+chapter2.length]);
String item=concatedArgs[randomNo];

【讨论】:

  • 我不想要这个解决方案的人。这将从第 2 章中选择。我想从任何章节中获得。
  • 请不要删除以前的答案.. 有时其他用户会感到困惑。并在答案顶部写下编辑。我也会尝试你的解决方案。
猜你喜欢
  • 1970-01-01
  • 2020-10-04
  • 1970-01-01
  • 1970-01-01
  • 2016-05-20
  • 1970-01-01
  • 2017-01-12
  • 2014-04-03
  • 2011-07-19
相关资源
最近更新 更多