数组问题 java.lang.ArrayIndexOutOfBoundsException: 9

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16867875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:12:42  来源:igfitidea点击:

Array Problems java.lang.ArrayIndexOutOfBoundsException: 9

javaarraysindexoutofboundsexception

提问by Max

I am trying to write a class that takes a phone number that the user inputs as a string, then stores each digit in an array. I converted the string to a long using Long.parseLong. Here is the function that is supposed to store each individual digit in an array.

我正在尝试编写一个类,该类将用户输入的电话号码作为字符串,然后将每个数字存储在一个数组中。我使用 Long.parseLong 将字符串转换为 long。这是应该将每个单独的数字存储在数组中的函数。

public void storetoarray()
        //Turn the strings to longs
        //A loop chops of the last digit and stores in an array
{
    phonelong = Long.parseLong(initialphone);
    phonetemp1 = phonelong;
    for (int i = phonelength; i>=0; i--)
    {
        phonearray[i-1] = phonetemp1%10;
        phonetemp2 = phonetemp1 - phonetemp1%10;
        phonetemp1 = phonetemp2/10;
        System.out.print("Phone temp 2" + phonetemp2 + phonetemp1);
    }
}

The logic of that loop is to find the last digit of the phone number using a %10, then subtract that number from the original phone number. That new difference is then set to be phonetemp1/10.

该循环的逻辑是使用 %10 找到电话号码的最后一位数字,然后从原始电话号码中减去该数字。然后将该新差异设置为 phonetemp1/10。

Example 4158884532%10 = 2. That is stored in phonearray[9]. Then phonetemp2 is subtracted giving us 4158884530. Divide that by 10 and you get 415888453. Should be ready to store the next digit.

示例 4158884532%10 = 2。它存储在 phonearray[9] 中。然后将 phonetemp2 减去,得到 4158884530。将其除以 10,得到 415888453。应该准备好存储下一个数字。

What is going wrong? Thanks for the help!

出了什么问题?谢谢您的帮助!

回答by dasblinkenlight

Your phonearraydoes not have enough space: you are accessing index 9, but phonearrayhas fewer than ten elements.

phonearray没有足够的空间:您正在访问 index 9,但phonearray元素少于十个。

You need to allocate the array like this:

您需要像这样分配数组:

phonearray = new int[10];

Once you fix this problem, change the loop to avoid accessing index -1(that's what is going to happen when ireaches zero).

一旦你解决了这个问题,改变循环以避免访问索引-1(当i达到零时会发生这种情况)。

Finally, there is no need to subtract phonetemp1%10from phonetemp1: integer division drops the fraction, so you could do this:

最后,不需要phonetemp1%10phonetemp1: 整数除法中减去分数,所以你可以这样做:

phonearray[i-1] = phonetemp1%10;
phonetemp1 /= 10;

回答by moonlighter

If you have initialized the array correctly with the size same as the length of the initialPhone, you should not see this exception. Your code would rather produce ArrayIndexOutOfBoundException: -1 Rewrote your code a bit, and works as expected:

如果您已正确初始化数组,其大小与 initialPhone 的长度相同,则不应看到此异常。您的代码宁愿产生 ArrayIndexOutOfBoundException: -1 稍微重写您的代码,并按预期工作:

import java.util.Arrays;

public class TestMain {

  public static void main(String[] args) {

    String initialPhone = "4158884532";
    int phoneLength = initialPhone.length();
    long[] phoneArray = new long[phoneLength];

    Long phoneNum = Long.parseLong(initialPhone);
    Long phonetemp1 = phoneNum;
    for (int i = phoneLength; i > 0; i--) {
      phoneArray[i - 1] = phonetemp1 % 10;
      Long phonetemp2 = phonetemp1 - phonetemp1 % 10;
      phonetemp1 = phonetemp2 / 10;
      System.out.println("Phone temp 2 --> " + phonetemp2 + phonetemp1);
    }
    System.out.println(Arrays.toString(phoneArray));
  }
}

Enjoy!

享受!