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?
我正在使用随机数生成器从Android字符串数组中选择一个项目。有没有办法将我的整数设置为数组的长度而不实际计算数组中的项目数?
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?
我想将上面的“4”定义为特定数组列表的长度。谁知道怎么样?
2 个解决方案
#1
2
I would like to define the "4" above as the length of a specific array list.
我想将上面的“4”定义为特定数组列表的长度。
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
.)
(然后要获取数组中元素的数量,请执行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;
}
#2
2
List myList = ...
int n = select.nextInt(myList.size());
#1
2
I would like to define the "4" above as the length of a specific array list.
我想将上面的“4”定义为特定数组列表的长度。
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
.)
(然后要获取数组中元素的数量,请执行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;
}
#2
2
List myList = ...
int n = select.nextInt(myList.size());