I have an XML resource file for an Android App that contains many string arrays, each named word#, where # is a number (currently from 0-100). i.e. word0, word1, etc.
I am using Random to choose a random number, and then a switch statement to select the correct array based on the number. So, if random.nextInt returns 4, I wand to return R.array.word4
Here is my code:
private String[] getWordArray(int i) { //i is a random number
Resources res = getResources();
String[] x = new String[8];
switch(i){
case 0: x = res.getStringArray(R.array.word0); break;
case 1: x = res.getStringArray(R.array.word1); break;
case 2: x = res.getStringArray(R.array.word2); break;
case 3: x = res.getStringArray(R.array.word3); break;
//and so on
}
I know this is horrendously repetitive, especially because I eventually want to have 1000 arrays. What can I do to make this better?
I must the XML resource storage method.
You can get resource id by its name: