I'm using a random number generator to select an item out of an Android string array. Is there a way to set my integer as the length of the array without actually counting the number of items in the array?

Here's an example of the random number code I'm using:

private Random random = new Random();
private int n = random.nextInt(4);

private String randText;

public Object(Context contex)
{
String[] string = context.getResources().getStringArray(R.array.text);

randText = "Stuff to display " + string[n] +".";
}

public String getRandText
{
return randText
}

I would like to define the "4" above as the length of a specific array list. Anyone know how?

share|improve this question
feedback

2 Answers

up vote 2 down vote accepted

I would like to define the "4" above as the length of a specific array list.

Perhaps this is what you're after:

String[] strs = { "str1", "str2", "str3", "str4", "str5" };

// Select a random (valid) index of the array strs
Random rnd = new Random();
int index = rnd.nextInt(strs.length);

// Print the randomly selected string
System.out.println(strs[index]);

To access the actual array, you do the following:

Resources res = getResources();
String[] yourStrings = res.getStringArray(R.array.your_array);

(Then to get the number of elements in the array, you do yourStrings.length.)


Regarding your edit. Try this instead:

private Random random = new Random();
private int n; // Can't decide up here, since array is not declared / initialized

private String randText;

public YourObject(Context contex) {
    String[] string = context.getResources().getStringArray(R.array.text);
    n = random.nextInt(string.length);     // <--- Do it here instead.
    randText = "Stuff to display " + string[n] +".";
}

public String getRandText {
    return randText;
}
share|improve this answer
I'm talking specifically about Android here, so I have a string array in strings.xml that I'm accessing... I'm not declaring an array in my code, just accessing the Android resource-- so I'm looking for syntax to access that resource (a string-array in strings.xml). – thefish7 Feb 13 '11 at 21:26
Then it should be an ordinary Java array, and you should be able to get the number of elements using the arrayName.length as suggested in my answer. – aioobe Feb 13 '11 at 21:28
Updated my answer. – aioobe Feb 13 '11 at 21:31
That was my initial thought too, but when I tried it, Eclipse gave an error of "arrayName cannot be resolved to a variable." – thefish7 Feb 13 '11 at 21:37
How did you declare the variable? (You need to put String[] in front of it!) – aioobe Feb 13 '11 at 21:38
show 7 more comments
feedback
List myList = ...
int n = select.nextInt(myList.size());
share|improve this answer
I'm talking specifically about Android here, so I have a string array in strings.xml that I'm accessing... I'm not declaring an array-list in my code, just accessing the Android resource-- so I'm looking for syntax to access that resource. – thefish7 Feb 13 '11 at 21:24
feedback

Your Answer

 
or
required, but never shown
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.