-1

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
Robert Ryan
  • 294
  • 4
  • 12
  • Well for one, `intArray` is empty. You didn't put anything in it. Two, you never realy made the array in the first place, but it seems `intArray` is already declared as something else. – Rogue Apr 22 '14 at 22:48
  • Where do you create `intArray` (`intArray = new int[10];`)? Why do you have an array and an ArrayList with the name `intArray`? And where do you place the values of the array into the ArrayList? – AntonH Apr 22 '14 at 22:48
  • if you want to use your ArrayList<> intArray, I would suggest to declare it before the for-loop and then use it as the arraylist is supposed to, i.e. adding elements with intArray.add(value) instead of intArray[i] = value – Sara S Apr 22 '14 at 22:50
  • once i place ArrayList intArray = new ArrayList(); or intArray = new int[10];or i get "array required, but java.util. found" – Robert Ryan Apr 22 '14 at 23:01
  • This code makes no sense, and wouldn't compile never mind throw that error. You don't show how you initialize `intArray[]` and then in the code you show you redeclare it as an `ArrayList` which would throw an error upon compilation. Please post an actual example. **Edit:** and the edit is just as bad, and also won't compile. – Brian Roach Apr 22 '14 at 23:04
  • That's because you don't access an ArrayList using the `[]` operator, but instead you use its `add` and `get` methods (or an iterator). – David Conrad Apr 22 '14 at 23:04
  • but doesn't the intArray[index] = 4*index+400; not calulate each element – Robert Ryan Apr 22 '14 at 23:07
  • Each element of the *array*, but aren't you supposed to be using an *ArrayList* instead? – David Conrad Apr 22 '14 at 23:10
  • thank you all for your help. My understanding grew with your help. – Robert Ryan Apr 22 '14 at 23:28

1 Answers1

3

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);
fr1tz
  • 8,018
  • 1
  • 12
  • 8
  • The for loop wouldn't cause the OOB as the increment happens at the bottom of the loop and would cause the exit condition to trigger at the top once it cause `index` to be `>= 10`. I'm guessing the array wasn't initialized properly in the first place. – Brian Roach Apr 22 '14 at 22:58
  • once i place ArrayList intArray = new ArrayList(); i get "array required, but java.util. found" – Robert Ryan Apr 22 '14 at 22:59
  • Thank you fr1tz. And Sara Seppola For the iterator help of get and add methods. I declared the array again and it worked. – Robert Ryan Apr 22 '14 at 23:23