Android Java数组索引自动绑定

Android Java数组索引自动绑定,java,android,arrays,arraylist,Java,Android,Arrays,Arraylist,当按下按钮时,如何使按钮转到数组中的最后一个位置,而不会出现indexoutofbound错误 switch (v.getId()) { case R.id.back: mainButton.setText(alphabet[position--]); mainButton.setBackgroundColor(Color.rgb(randomColor, randomColor2, randomColor3)); if (alph

当按下按钮时,如何使按钮转到数组中的最后一个位置,而不会出现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;
    }

如果你得到了你想要做的事情,这应该是可行的。 首先计算数组的位置,使其位于边界内。然后访问阵列位置

switch (v.getId()) {
case R.id.back:
    position--;
    if(position<0) { position=25; }
    // mainButton.setText(alphabet[position]);
    // mainButton.setBackgroundColor(Color.rgb(randomColor, randomColor2, randomColor3));

    break;
case R.id.forward:
    position++;
    if(position>25) { position=0; }
    // mainButton.setText(alphabet[position]);
    // mainButton.setBackgroundColor(Color.rgb(randomColor, randomColor2, randomColor3));

    break;
}
// for better improvement this can be added once for both cases
mainButton.setText(alphabet[position]);
mainButton.setBackgroundColor(Color.rgb(randomColor, randomColor2, randomColor3));
开关(v.getId()){
案例R.id.back:
位置--;
如果(位置25){position=0;}
//main按钮.setText(字母[位置]);
//setBackgroundColor(Color.rgb(randomColor,randomColor2,randomColor3));
打破
}
//为了更好地改进,这两种情况都可以添加一次
main按钮.setText(字母[位置]);
setBackgroundColor(Color.rgb(randomColor,randomColor2,randomColor3));

此检查
alphabet.equals(“A”)
没有实际意义,因为您试图将数组与字符串值进行比较,这将始终返回false。

要停止
索引自动边界异常
并从数组中的最后一个索引中获取值,您可以使用此选项

alphabet[alphabet.length-1]);

使用
arrayname.length-1
而不是特定的索引号,即使数组大小已更改,也会始终返回最后一个索引。虽然我相信你的代码有更多的错误,不仅仅是索引超出了界限。

更短的解决方案,即使有人得到了绿色的勾号:(

因此,当您获得
-1
时,
位置将更改为
字母表。长度-1

当你得到
alphabet.length+1
时,你的位置将变为
0

字母数组的大小??A-Z…它是26@Youngistan25是最后一个位置字母[25]如果你的数组大小是26,并且你正在寻找第27个元素,那么你会得到索引越界,除了(alphabet.equals…)这是一个数组,不是字符串。if(字母表[position].equals…。你有位置问题。谢谢,这对我来说非常有效。
{//your method...
    switch (v.getId()) {
        case R.id.back:
            position--;
            position = modulo(position, alphabet.length);
            mainButton.setText(alphabet[position]);
            mainButton.setBackgroundColor(Color.rgb(randomColor, randomColor2, randomColor3));

            break;
        case R.id.forward:
            position++;
            position = modulo(position, alphabet.length);
            mainButton.setText(alphabet[position]);
            mainButton.setBackgroundColor(Color.rgb(randomColor, randomColor2, randomColor3));

            break;
    }
}

private int modulo(int x, int y) {
    return (int) (x - (y * Math.floor((double) x / (double) y)));
}