Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从字符串数组列表中随机选择_Java_Android_Arrays_Xml - Fatal编程技术网

Java 如何从字符串数组列表中随机选择

Java 如何从字符串数组列表中随机选择,java,android,arrays,xml,Java,Android,Arrays,Xml,我有一个字符串数组列表: <string-array name="chapter_1"> <item>......</item> . . . . </string-array> <string-array name="chapter_2"> <item>......</item> .

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

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

<string-array name="chapter_2">
      <item>......</item>
      .           
      .
      .
      .
</string-array>
现在,我想访问与章节号对应的字符串数组

我知道我不能这样做:

Random random = new Random();

int chapter_no = random.nextInt(2) + 1;
String chapter_name = "chapter_" + chapter_no;
字符串[]章= getResources().getStringArray(R.array.chapter_name)


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

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

int[] arrays = {R.array.chapter_1,....};
在这些数组中随机选择一个

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

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

Random random = new Random();

int chapter_no = random.nextInt(2) + 1;
String chapter_name = "chapter_" + chapter_no;
我在上面使用的
Random
函数将获得数字2之前的值


我希望这对你有帮助

从章_数组中获取随机字符串

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();
[更新]

        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();
一次尝试如下

更新

int id = getResources().getIdentifier(chapter_name, "array",this.getPackageName()); 
String[] chapter = getResources().getStringArray(id);
希望这对您有所帮助。

您可以这样做:

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];
String[]chapter=getResources().getStringArray(R.array.chapter_1);
String[]chapter2=getResources().getStringArray(R.array.chapter_2);
Random rand=新的Random();
int randomNo=rand.nextInt();
ArrayList temp=新的ArrayList();
临时addAll(Arrays.asList(第章));
临时addAll(数组asList(第2章));
字符串[]concatedArgs=临时数组(新字符串[章节长度+章节2.长度]);
字符串项=concatedArgs[randomNo];


您想要一个随机字符串数组还是一个已知章节中的随机字符串?首先我想要获取字符串数组,然后是一个章节中的字符串。假设我想要一个章节中的随机字符串(项目)。你想要任何特定章节中的随机字符串(项目)还是所有章节中的随机字符串(项目)?@learner你不能遍历元素并找到你要查找的元素吗?如果我理解正确,那么@Moinkhan的答案应该可以帮助你实现给定问题中描述的内容?我不能有更好的解决办法吗?但是你有一个字符串数组。我有两个以上的字符串数组和相应的章节号@learner,你知道你有多少个字符串数组吗?@learner那么你可以用你拥有的字符串数组数(总数)来更改下一个字符串数组中的数<代码>随机。下一个(总计)+1我想确定一件事,。。。您是否可以使用章节名称….???章节名称是一个变量,而不是常量。。请把问题通读一遍。它根本不起作用,我试过了……是的,我这里有一个解决方案:但我不想做数组,因为有100章。但是如果你说的是100章,那么你应该使用Sqlite。。。这是你最好的选择…我知道。但我使用的是字符串数组。而且用数组代替字符串也很有效..我不想要这个解决方案。这将从第2章中选择。我想从任何一章中得到答案。请不要删除前面的答案。。有时其他用户会感到困惑。并在答案的顶部写上“编辑”。。我也会试试你的解决方案。你的答案很有效。谢谢你的回答。我找不到该主题的任何文档。请给我推荐一个。@我对下面提到的这个也不熟悉