PDA

View Full Version : سوال: اشکال متد pickWord چیه؟



yegane8
شنبه 16 اردیبهشت 1391, 02:50 صبح
سلام
این یک کلاس که از بین کلمات موجود در ارایه newWord یکی رو به صورت random انتخاب می کنه ولی در متد
pickword وقتی دارم return میکنم به قسمت index اشکال میگیره چرا؟؟؟ از متدش مطمئنم قبلا تو یه کلاس دیگه ازش استفاده کردم.
import java.util.ArrayList;
import java.util.Random;
public class RandomWord
{
// instance variables - replace the example below with your own
private Random randomGenerator;
private ArrayList newWord;


/**
* Constructor for objects of class RandomWord
*/
public RandomWord()
{
// initialise instance variables
randomGenerator = new Random();
newWord = new ArrayList<String>();
fillWords();
}

/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public void fillWords()
{
newWord.add("book");
newWord.add("hello");
newWord.add("phone");
newWord.add("smartcar");
newWord.add("laptap");
}

private String pickWord(){
int index = randomGenerator.nextInt(newWord.size());
return newWord.get(index);
}
}