Pregunta

I have a bit of homework. I can't seem to find the fault forgive my interpretation of the question asked, as I only started a few weeks ago with java. But here is the question and my solution so far.

Write a method demoArray4 in which you:

Declare an ArrayList of Integer

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.

My solution so far.

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++;
            }
        }

My problem it won't print the numbers I require from Arrat list "intArray" what pops up

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

this is what is suppose to happen outputs this list of 10 elements

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
¿Fue útil?

Solución

EDIT: As per Brian's comment, the following problem is with the logic of the program:

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

Seems to be an unnecessary line incrementing the index variable. In Java the third part of the line for (int index=0; index<10; index++) will increase the variable index by 1 at the end of each successful loop. You've got this in twice, which is probably not what you want

EDIT: This part:

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

Should be:

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

And place this at the start of public void demoArray4()

To add elements to the ArrayList use the .add(item) method:

intArray[index] = 4*index+400;

Should be :

intArray.add(4*index+400);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top