Kasparas
Kasparas

Reputation: 89

IndexOutOfBoundsException in ArrayList Java

Whenever I try to run the code I get IndexOutOfBoundsException. I have tried numerous of ways fixing it but none of them have helped. The method should add a new String element "****" into ArrayList before every String which's length is equal to 4. In this case, it must add "****" before "5555".

Where could be the problem?

import java.util.ArrayList;

public class Main {

    public static ArrayList<String> markLength4(ArrayList<String> list) {

        int sum = 0;
        for ( int i = 0; i < list.size(); i++) {
            if (list.get(i).length() == 4) {
                list.add(list.indexOf(i), "****");
            }
        } 
        return  list;
    }

    public static void main(String[] args) {

        ArrayList<String> list = new ArrayList<>();
        list.add("ddddddddddddd");
        list.add("fffffffffffff");
        list.add("5555fdgdfg");
        list.add("5555");
        list.add("5555");

        System.out.println(markLength4(list));
    }
}

Upvotes: 2

Views: 396

Answers (6)

Hughzi
Hughzi

Reputation: 2580

list.indexOf(i) where i is an int and therefore not in your list will throw your error as stated in comments (index -1).

use either of the following:

list.add("str") to add a String to the end of the list

OR

list.set(i, "****") which will set the value at a given index to this new string.

Upvotes: 0

Razorbolt
Razorbolt

Reputation: 47

import java.util.ArrayList;

public class Test {

    public static ArrayList<String> markLength4(ArrayList<String> list) {
        int sum = 0;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).length() == 4) {
                list.add(i++, "****");
            }
        }
        return list;
    }

    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("ddddddddddddd");
        list.add("fffffffffffff");
        list.add("5555fdgdfg");
        list.add("5555");
        list.add("5555");
        list = markLength4(list);
        for (String x : list) {
            System.out.println(x);
        }
    }
}

You'll loop forever this way, because there's 4-lengthened strings forward and you keep adding...
You can solve this by looping from the end, but you'll have to be careful with your index(you should add and increment the index to avoid that)

After Editing list.add(i++,"****"); the code should work just fine.

Notable
If you want to add before use i++;.
If you want to add after your match use ++i;.

Upvotes: 1

Sathyendran a
Sathyendran a

Reputation: 1819

list.indexOf(i) is not present in the list . It will produce -1

-1 is not available in ArrayList

Replace the Line list.add(list.indexOf(i), "****");

with the following line

list.set(i, "****");

It replace the existing content of the List with new element in the index of i with new element i.e (****)

Upvotes: 0

Chris
Chris

Reputation: 1742

In the markLength4 method, by adding the element in the for loop you keep adding Strings and increasing the list size. You need a flag that tells the index and then ends the loop. You can try something like that

public static ArrayList<String> markLength4(ArrayList<String> list) {

    int i = 0;
    boolean found = false;
    int pos = 0;

    while(i < list.size() && !found){
        if (list.get(i).length() == 4) {
            found = true;
            pos = i;
        }
        i++;
    }
    list.add(pos, "****");
    return list;
}

Upvotes: -1

Andy Turner
Andy Turner

Reputation: 140319

i is not in your arraylist - it is a list of String, not Integer. That means that list.indexOf(i) == -1.

From your description, I think you mean:

list.add(i, "****");

but you will also need to increment i, e.g.

list.add(i++, "****");

to avoid the infinite loop that Eran mentions.

Or, of course, you can iterate the list backwards, and avoid the infinite loop/need to change the loop variable inside the loop body:

for ( int i = list.size() - 1; i >= 0; i--)
{
  if (list.get(i).length() == 4)
  {
    list.add(i, "****");
  }
}

Upvotes: 1

Eran
Eran

Reputation: 393781

list.indexOf(i) will return -1, since i doesn't appear in your list. Therefore adding an element at the -1 position will throw an exception.

If you change list.add(list.indexOf(i), "****") to list.add(i, "****");, you'll get an infinite loop that will end with OutOfMemoryError, since the newly added String also has a length() of 4, so another String will be added on the next iteration, and so on.

Upvotes: 2

Related Questions