Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have this string array:

String[] directionsList = {"north", "south", "east", "west"};

and I want to find the string at index i (i is a random variable determined in the method). I have this:

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

So say i = 2, I want to return "east". Is there any built-in Java method to do this, or do I have to use a loop? Thanks.

share|improve this question
4  
String myDirection = directionList[i] ? Just make sure that 0 <= i < directionList.length. Also you should read this : docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html –  ZouZou Dec 8 '13 at 9:49

1 Answer 1

up vote 3 down vote accepted
String myDirection = directionsList[i];
share|improve this answer
    
this isn't working for me though –  peppy Dec 8 '13 at 15:57
    
@peppy: What is the value of i you tried? –  Ajai Dec 8 '13 at 16:03
    
nevermind, it's working now. what i have is i = random.nextInt(4) where random is a Random object. thanks for your help! –  peppy Dec 8 '13 at 17:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.