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