【发布时间】:2014-03-02 17:56:28
【问题描述】:
如何让按钮在按下时转到数组中的最后一个位置而不会出现 indexoutofbound 错误?
switch (v.getId()) {
case R.id.back:
mainButton.setText(alphabet[position--]);
mainButton.setBackgroundColor(Color.rgb(randomColor, randomColor2, randomColor3));
if (alphabet.equals("A")) {
mainButton.setText(alphabet[25]);
}
break;
case R.id.forward:
mainButton.setText(alphabet[position++]);
mainButton.setBackgroundColor(Color.rgb(randomColor, randomColor2, randomColor3));
if (alphabet.equals("Z")) {
mainButton.setText(alphabet[0]);
}
break;
}
【问题讨论】:
-
字母数组的大小是多少??
-
A-Z... 它是 26 @Youngistan
-
25 是字母的最后位置[25]
-
如果您的数组大小为 26 并且您正在寻找第 27 个元素,那么您会得到 index out of bounds 异常
-
如果 (alphabet.equals...)??它是一个数组,而不是一个字符串。 如果(字母[位置].equals...)。而且你有职位问题。
标签: java android arrays arraylist