增强的for循环异常 [英] Enhanced For Loop Exception

查看:154
本文介绍了增强的for循环异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建下面code,同时用循环播放。下面存储code斐波那契值到一个数组,然后使用for循环打印它们。

  INT []号;
    数= INT新[25];    号码[0] = 1;
    号[1] = 1;
    的System.out.println(初始化数组值);    的for(int i = 2; I< numbers.length;我++)
    {
        号码[I] =号码[I-1] +数字〔Ⅰ-2〕;
    }    的System.out.println(打印出来斐波纳契值);    的for(int i = 0; I< numbers.length;我++)
    {
        System.out.print(数字[I] +,);
    }

以上code正常工作。第一次我把它虽然在一起,我用了一个增强的for循环打印出来的值(第二次在code循环)。编译没有问题,但是当我运行它,我得到以下;

 初始化数组值
打印出斐波纳契值
1,1,2,3,8,34,377,17711,异常在线程主java.lang.ArrayIndexOutOfBoundsException:34
在ArrayDemo.main(ArrayDemo.java:21)

我不明白什么地方出了错。改变第二回路不应更改值(你会发现斐波纳契值是错误的(即缺失值))。我不明白为什么增强的for循环简单的将跳过索引。现在,这是不是一个真正的大问题,因为这不是一个项目或任何东西,它只是我的错误,我想不通为什么它这样做。任何线索?

增强的for循环只是看上去像这样;

 为(INT I:数字)
    {
        System.out.print(数字[I] +,);
    }


解决方案

 为(INT I:数字)
{
     System.out.print(数字[I] +,);
}

I 这里是在元素在阵列中,而不是指标。这可能是比 numbers.length 大。

例如,如果数字= {1,2,3,9} 然后 I 1 2 3 9 。但其长度为4,所以当你在它里面的元素循环,你正在试图做的号码[9] 超过它的大小。

您可能希望 System.out.print(I +);

Created the below code whilst playing with loops. The code below stores the Fibonacci values into an array and then prints them using for loops.

    int [] numbers;
    numbers = new int[25];

    numbers[0] = 1;
    numbers[1] = 1;
    System.out.println("Initializing the array values");

    for(int i = 2; i < numbers.length; i++)
    {
        numbers[i] = numbers[i-1] + numbers[i-2];
    }

    System.out.println("Printing out Fibonacci values");

    for(int i = 0; i < numbers.length; i++)
    {
        System.out.print(numbers[i] + ", ");
    }

The above code works fine. The first time I threw it together though, I used an enhanced for loop to print out the values (the second for loop in the code). This compiles fine, but when I run it I get the following;

Initializing the array values
Printing out Fibonacci values
1, 1, 2, 3, 8, 34, 377, 17711, Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 34
at ArrayDemo.main(ArrayDemo.java:21)

I don't get what went wrong. Changing the second loop shouldn't change the values (you'll notice the fibonacci values are wrong (ie missing values)). And I don't get why a simple enhanced for loop would skip indexes. Now, this isn't really a big deal because this isn't for a project or anything, it just bugs me that I can't figure out why it's doing it. Any clues?

The enhanced for loop just looked like this;

for(int i : numbers)
    {
        System.out.print(numbers[i] + ", ");
    }

解决方案

for(int i : numbers)
{
     System.out.print(numbers[i] + ", ");
}

i here is the elements in the array, not the indexes. It could be bigger than numbers.length.

For example, if numbers = {1,2,3,9} then i will be 1, 2, 3, 9. But its length is 4, so when you loop on the elements inside it, you're trying to do numbers[9] which exceeds its size.

You probably want to System.out.print(i + ", ");

这篇关于增强的for循环异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆