Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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_Arrays_String_Loops_Indexing - Fatal编程技术网

在数组索引处查找字符串?JAVA

在数组索引处查找字符串?JAVA,java,arrays,string,loops,indexing,Java,Arrays,String,Loops,Indexing,我有一个字符串数组: String[] directionsList = {"north", "south", "east", "west"}; 我想找到索引I处的字符串(I是方法中确定的随机变量)。 我有这个: String myDirection = directionsList.valueOf(i); # I have no idea if this is even # close to

我有一个字符串数组:

String[] directionsList = {"north", "south", "east", "west"};
我想找到索引I处的字符串(I是方法中确定的随机变量)。 我有这个:

String myDirection = directionsList.valueOf(i);  # I have no idea if this is even
                                                 # close to correct

假设i=2,我想返回“东”。是否有任何内置的Java方法来实现这一点,或者我必须使用循环?谢谢。

字符串myDirection=directionList[i]
?只需确保
0
String myDirection=directionList[i]
?只要确保
0无需担心,它现在正在工作。我得到的是i=random.nextInt(4),其中random是一个随机对象。谢谢你的帮助!没关系,它现在起作用了。我得到的是i=random.nextInt(4),其中random是一个随机对象。谢谢你的帮助!
String myDirection = directionsList[i];