简体   繁体   English

为什么我的int []数组循环超出范围?

[英]Why is my int[] array loop out of bounds?

Warning: I am very new to Java and programming in general. 警告: 我一般对Java和编程都不熟悉。 I'll try to be as clear as possible. 我会尽量清楚。

I am attempting to take a simple integer ( inputnumber ), convert it to a string ( temp ), create a new int[] array ( numberarray ), and loop through this int[] array, starting from the last digit, and print out the name of the digit. 我正在尝试获取一个简单的整数( inputnumber ),将其转换为字符串( temp ),创建一个新的int []数组( numberarray ),并从最后一位开始遍历该int []数组,然后打印出来数字的名称。

I am rather sure that the conversion from integer to String to int[] array was functional due to Eclipse debugging, but am stumped as to why I am getting an ArrayOutOfBounds message from Eclipse for such a simple for loop. 我相当确定由于Eclipse调试,从整数到String到int []数组的转换是可行的,但是对于为什么我从Eclipse获得如此简单的for循环的ArrayOutOfBounds消息感到困惑。 Any clues as to what I am doing wrong is appreciated. 任何关于我做错事情的线索都值得赞赏。

    String temp = inputnumber.toString();
    int[] numberarray = new int[temp.length()];

    for (int i=0;i<temp.length();i++) {
        numberarray[i] = temp.charAt(i);
    }


    for (int i=temp.length();i>0;i--) {

        if (numberarray[i]==1) System.out.print("one.");
        if (numberarray[i]==2) System.out.print("two.");
        if (numberarray[i]==3) System.out.print("three.");
        if (numberarray[i]==4) System.out.print("four.");
        if (numberarray[i]==5) System.out.print("five.");
        if (numberarray[i]==6) System.out.print("six.");
        if (numberarray[i]==7) System.out.print("seven.");
        if (numberarray[i]==8) System.out.print("eight.");
        if (numberarray[i]==9) System.out.print("nine.");
        if (numberarray[i]==0) System.out.print("zero");
    }

The Eclipse error message I am getting is: 我收到的Eclipse错误消息是:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at jt.Intermediate8.main(Intermediate8.java:44)

Arrays are 0-indexed in Java. 数组在Java中为0索引。 This means the last value is at index NUMBER_OF_ELEMENTS - 1 这意味着最后一个值位于索引NUMBER_OF_ELEMENTS-1

Therefore, in your for loop, you should change 因此,在您的for循环中,您应该进行更改

int i=temp.length()     // this is last index + 1 (since we are starting from 0)

To: 至:

int i=temp.length() - 1 // this is last index

Also, as @brso05 said, don't forget to change your loop-ending condition to i>=0 since the last value going backwards will be at index 0. 另外,正如@ brso05所说,不要忘记将循环结束条件更改为i>=0因为最后一个倒退的值将位于索引0。

Your for loop: 您的for循环:

for (int i = temp.length(); i >= 0; i--)

You're starting the loop at temp.length(). 您将从temp.length()开始循环。 That's not a valid index. 这不是有效的索引。 Perhaps you want temp.length()-1? 也许您想要temp.length()-1?

You should be doing temp.length() - 1. The reason is that the array starts with index 0 not 1 so the last element in an array is stored at the length - 1. If there are 10 elements then 0-9 are your indexes. 您应该执行temp.length()-1。原因是数组以索引0而不是1开始,因此数组的最后一个元素以长度-1进行存储。如果有10个元素,则0-9是您的索引。 Also change i>0 to i>=0 if you want to hit all elements. 如果要击中所有元素,还可以将i> 0更改为i> = 0。

for (int i=(temp.length() - 1);i>=0;i--) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM