Predict_it
Predict_it

Reputation: 233

Arraylist & generics error IndexOutOfBoundsException

I want to separate numbers from bList to list2,list3 and list , which i can divide by 3 ,2 and others at the end i want to print them out) but i get error IndexOutOfBoundsException what does it mean

        Scanner in = new Scanner(System.in);

        ArrayList<Integer> bList = new ArrayList<Integer>();
        ArrayList<Integer> list2 = new ArrayList<Integer>();
        ArrayList<Integer> list3 = new ArrayList<Integer>();
        ArrayList<Integer> list = new ArrayList<Integer>();

        for (int i = 0; i < 20; i++){
            bList.add(in.nextInt());
        }

        for (int y : bList)
        {
            if ((y % 3 == 0) && (y % 2 == 0))
        {
                list3.add(bList.get(y));
                list2.add(bList.get(y));
        }
        if (y % 3 == 0)
        {
            list3.add(bList.get(y));
        }
        if (y % 2 == 0)
        {
                 list2.add(bList.get(y));
        }
        else
        {
                 list.add(bList.get(y));
        }
        }
        printList(list3);
        printList(list2);
        printList(list);
    }

    public static void printList(List<Integer> list) {
        for (Integer x : list){
            System.out.println(list.get(x));
        }

Upvotes: 0

Views: 104

Answers (5)

Jyoti Ranjan Pattnaik
Jyoti Ranjan Pattnaik

Reputation: 713

The problem is in putting the elements in the list. Also , I think, your requirement is to print 3 kinds of elements, one is to print all elements divisible by 3, another is to print all elements divisible by 2 and the last one is to print all elements which are neither divisible by 3 nor 2, if my assumption is right, here is the code...

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class ListTest {

/**
 * @param args
 */
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    ArrayList<Integer> bList = new ArrayList<Integer>();
    ArrayList<Integer> list2 = new ArrayList<Integer>();
    ArrayList<Integer> list3 = new ArrayList<Integer>();
    ArrayList<Integer> list = new ArrayList<Integer>();

    for (int i = 0; i < 20; i++){
        bList.add(in.nextInt());
    }

    in.close();

    for (int y : bList)
    {
        //Not needed
    /*    if ((y % 3 == 0) && (y % 2 == 0))
    {
            list3.add(y);
            list2.add(y);
    }*/
    if (y % 3 == 0)
    {
        list3.add(y);
    }
    if (y % 2 == 0)
    {
             list2.add(y);
    }
    if (y % 3 != 0 && y % 2 != 0)
    {
             list.add(y);
    }
    }

    System.out.println("the numbers divisble by 3 \n");
    printList(list3);
    System.out.println("\n the numbers divisble by 2");
    printList(list2);
    System.out.println("\n the numbers divisible neither by 3 nor 2  \n");
    printList(list);

}

public static void printList(List<Integer> list) {
    for (Integer x : list){
        System.out.print(x+"\t");
    }
}

}

Upvotes: 0

zkk
zkk

Reputation: 207

You are trying to access an index that does not exist. By looking at your program it seems you are doing the following:

  1. You initialize an array by 20 values let say I enter {50,20,30,1,...up to 20}
  2. Then you take a value from this array one by one (that means on the first run of the second for loop in your program you get 50 if we consider the above sample in point 1)
  3. Then inside your if conditions you are basically trying to retrieve an index (bList.get(y) which is 50) that has nothing on it because that array has only 20 values.

Upvotes: 0

Janoz
Janoz

Reputation: 953

Use y instead of bList.get(y). Your using the value as an index instead of the value itself. If the value is >20 you will get an index out of bounds exception.

Upvotes: 0

Alexei Kaigorodov
Alexei Kaigorodov

Reputation: 13525

System.out.println(x);

x is an element already read from the list, not the index.

Upvotes: 0

Konstantin Yovkov
Konstantin Yovkov

Reputation: 62864

The error is in this statement:

for (Integer x : list) {
    System.out.println(list.get(x));
}

You're trying to access the element in the list with index x, which will throw ArrayIndexOutOfBoundsException if x = 1000 and it's the only element in the list (for example).

You probably meant:

for (Integer x : list) {
   System.out.println(x);
}

Upvotes: 2

Related Questions