1

I'm running my code in debug mode in eclipse and in the middle of it, I want to change the size of a List,say from 9 to 6, by deleting 3 elements.

But I'm not seeing any option to do that, in fact what I'm seeing is the option to change the values present in the elements.

So how can I delete the elements itself from the List ?

gautam
  • 197
  • 1
  • 4
  • 17

3 Answers3

2

Make sure you have the "Variables" tabs on eclipse "Debug" view focused on your current evaluated code.
In "Debug" view, right-click on "Value" cell inside the "Variables" table, and select "Change value".
You will have an option to write a Java expression so you can add something like:
yourList.add("newItem"); or: yourList.remove(0);
Make sure to reload the variable ("F5") once you are done and you will see the updated state.

Note that not every List implementation supports add() or remove() methods.
See this for more details if you encounter an exception.

See also:
Eclipse docs - Variables view
Eclipse docs - Change variable value

Leet-Falcon
  • 2,107
  • 2
  • 15
  • 23
0

I was looking to do something similar. I had an ArrayList containing 1 element, and I wanted to remove it.

I tried @Leet-Falcon's answer, e.g. yourList.remove(0), and Eclipse replied "Unsupported operation exception".

What ended up working was : return new java.util.ArrayList<>();

bercy46
  • 141
  • 1
  • 1
  • 8
  • As a side-note, the ArrayList used had been created through Arrays.asList(), which returns a fixed-size list, hence you cannot remove items from it. Had the ArrayList been created with new ArrayList<>(), I'm sure Leet-Falcon's answer would have worked just fine. – bercy46 Jan 31 '19 at 17:48
  • So what do you do if you need 1-2 specific elements there, not an empty list? – gene b. Oct 19 '20 at 15:18
0

Looks like the "Debug Shell" view allows this, or additionally if it's a simple enough list, the following simple Change Variable can also work:

new ArrayList<String>(java.util.Arrays.asList("1","2")) // or any other simple list

Modifying Java Collection (List) Variable in Eclipse Debugger

gene b.
  • 10,512
  • 21
  • 115
  • 227