【问题标题】:Android Java Array indexoutofbound [closed]Android Java Array indexoutofbound [关闭]
【发布时间】: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


【解决方案1】:

如果得到你想要做的事情,这应该可以工作。 首先计算数组的位置,使其在边界内。然后访问数组位置。

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));

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

【讨论】:

  • 谢谢,这对我来说很完美。
【解决方案2】:

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

{//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)));
}

所以当你得到-1时,position会变成alphabet.length-1 当您获得alphabet.length+1 时,您的位置将更改为0

【讨论】:

    【解决方案3】:

    要停止 IndexOutOfBoundsException 并从数组中的最后一个索引中获取值,您可以使用它

    alphabet[alphabet.length-1]);
    

    使用arrayname.length-1 而不是特定的索引号将始终返回最后一个索引,即使您的数组已更改大小。尽管我认为您的代码存在更多错误,而不仅仅是索引越界。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-06
      • 1970-01-01
      • 1970-01-01
      • 2013-04-29
      • 2021-01-26
      • 2012-03-06
      • 1970-01-01
      • 2021-12-31
      相关资源
      最近更新 更多