2

I'm trying to remove a value from the list using thread. But the code fails and gives an exception. Plz help I'm a beginner in thread programming.....

This is the content of Test.java

import java.util.*;

public class Test {
    private static final List<Integer> Values = new ArrayList<Integer> ();
    public static void main(String args[]) {
        TestThread t1 = new TestThread(Values);
        t1.start();

        System.out.println(Values.size());
    }
}

This is the content of TestThread.java

import java.util.*;

public class TestThread extends Thread {
    private final List<Integer> Values;

    public TestThread(List<Integer> v) {
        this.Values = v;
        Values.add(5);
    }

    public void run() {
        Values.remove(5);
        System.out.println("5 removed");
    }
}
Jops
  • 22,535
  • 13
  • 46
  • 63
khirod
  • 345
  • 3
  • 6
  • 18
  • What is the exception, and what line is causing it? – drew moore Apr 07 '13 at 06:43
  • 1Exception in thread "Thread-0" java.lang.IndexOutOfBoundsException: Index: 5, Size: 1 at java.util.ArrayList.rangeCheck(ArrayList.java:603) at java.util.ArrayList.remove(ArrayList.java:444) at TestThread.run(TestThread.java:12) – khirod Apr 07 '13 at 06:45

4 Answers4

4

This line means: remove the value at index 5. But there's nothing in index 5.

    Values.remove(5);

There's only 1 value in the array currently because this line means add the value 5 into my list, not add 5 values to my list.

    Values.add(5);

Your error is most likely IndexOutOfBoundsException. You'll see it more clearly if you display the size of your list.

public void run() {
    System.out.println(Values.size()); // should give you 1
    Values.remove(5);
    System.out.println("5 removed");
}

This is how it looks:

enter image description here

When it was inserted, 5 got auto-boxed into an Integer object. Thus, to successfully remove it, you should wrap it into one: new Integer(5). Then issue the remove call. It will then invoke the overloaded version of remove that accepts an Object, instead of the int.

Values.remove(new Integer(5));

means Remove the Integer object named '5' from my list.

Jops
  • 22,535
  • 13
  • 46
  • 63
2

List#remove(int) method removes the element at the specified position from the list, so Values.remove(5) will try to remove at index 5 element which element does exist there. Here int value 5 will not be autoboxed as List#remove(int) is already exist.

you should use List#remove(Object o) which actually Values.remove(new Integer(5)) .

public void run() {
    Values.remove(new integer(5));
    System.out.println("5 removed");
}
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
1

Your call to Values.remove(5); isn't doing what you expect it to do. The int you're passing in the parameter is an index value, so it's trying to delete the item at index 5 in your arraylist, but there is only 1 value in it.

A workaround to enable you to remove an integer of a given value

int given = 5;
for (int curr = 0; curr < Values.size(); curr++){
    if (Values.get(curr) == given) {
         Values.remove(given);
    }
}
drew moore
  • 31,565
  • 17
  • 75
  • 112
1

In List (ArrayList) there are 2 remove methods (overloaded)

  1. remove(int) --> which means remove at index
  2. remove(Object) --> which means remove the particular object from list

When you said Values.remove(5), the compiler took 5 as int and invoked the remove(int) method, which tried to remove the value store at index 5. Since the index 5, dint had any value, IndexOutOfBoundException was thrown.

To tackle it, say remove(new Integer(5)), to make compiler, invoke remove(Object) method. See this SO thread for more clarity.

Community
  • 1
  • 1
sanbhat
  • 17,522
  • 6
  • 48
  • 64