java作业。超出界限73

时间:2014-04-22 22:45:38

标签: java for-loop while-loop iterator println

我有一些功课。 我似乎无法找到错误原谅我对所提问题的解释,因为我几周前才开始使用java。但到目前为止,这是问题和我的解决方案。

编写一个方法demoArray4,其中:

声明一个Integer的ArrayList

1.Add size number of int values generated as follows
    --in a for loop
    --each element is 400 + 4*i where i is the loop counter

2. Print out all the elements in the list in rows so that the display matches Figure 4.
    --Use an iterator to traverse the loop as you print each element.

到目前为止我的解决方案。

import java.util.ArrayList;
import java.util.Iterator;

public class DemoArray
{
    private int [] intArray;
    private ArrayList<Integer> nums;

    private int size = 10;

    public DemoArray()
    {
     intArray = new int[10]; 
     ArrayList<Integer> intArray = new ArrayList<>();

public void demoArray()
    {
        int [] intArray = new int[10];
        for (int index=0; index < 10; index++) 
        {
              intArray[index] = index+100;
        }

        for( int index = 0; index < 10; index++)
        {
             System.out.println("Element at index " + index + " is " + intArray[index]);
        }
    }

public void demoArray2() {
    int [] intArray = new int[10];
    int index = 0;
    while (index < 10) 
    {
        intArray[index] = 200 + 2*index;
        index++;
    }
    index = 0;
    while( index < 10) 
    {
         System.out.println("Element at index " + index + " is " + intArray[index]);
         index++;
    }
}


public void demoArray3()
{
    int [] intArray = new int[10];
    int count = 0;
    int index = 0;
    do
    {
        intArray[index] = 300 + 3*index;
        index++;
    }while (index < 10);
    index=0;
    do
    {
        System.out.println("Element at index " + index + " is " + intArray[index]);
        index++;
    }while (index < 10);
}  



        public void demoArray4()

        {

            for (int index=0; index<10; index++) 
            {
                  intArray[index] = 4*index+400;
                  index++;
            }

            ArrayList<Integer> intArray = new ArrayList<Integer>();
            Iterator<Integer> itr = intArray.iterator();
            index=0;
            while(itr.hasNext())
            {
                System.out.println("Element at index " + index + " is " + itr.next());
                index++;
            }
        }

我的问题是它不能从Arrat列表“intArray”打印我需要的数字 什么弹出

java.lang.ArrayIndexOutOfBoundsException: 10
    at DemoArray.demoArray4(DemoArray.java:73)

这就是假设发生的事情 输出这10个元素的列表

1/Element at index 0 is 400
2/Element at index 1 is 404
3/Element at index 2 is 408
4/Element at index 3 is 412
5/Element at index 4 is 416
6/Element at index 5 is 420
7/Element at index 6 is 424
8/Element at index 7 is 428
9/Element at index 8 is 432
10/Element at index 9 is 436

1 个答案:

答案 0 :(得分:3)

编辑:根据Brian的评论,以下问题与程序的逻辑有关:

 for (int index=0; index<10; index++) 
        {
              intArray[index] = 4*index+400;
              index++; <--- this line here
        }

似乎是递增索引变量的不必要的行。在Java中,行for (int index=0; index<10; index++)的第三部分将在每个成功循环结束时将变量索引增加1。你有两次这个,这可能不是你想要的

编辑:此部分:

ArrayList<Integer> intArray = new ArrayList<Integer>();

应该是:

List<Integer> intArray = new ArrayList<Integer>()

并将其放在public void demoArray4()

的开头

要向ArrayList添加元素,请使用.add(item)方法:

intArray[index] = 4*index+400;

应该是:

intArray.add(4*index+400);