0
  ArrayList<String> TestList = new ArrayList<String>();
    ArrayList<Integer> indexList = new ArrayList<Integer>();
    indexList.add(1);
    indexList.add(3);
    indexList.add(4);
    TestList .add("String1");
    TestList .add("String2");
    TestList .add("String3");
    TestList .add("String4");
    TestList .add("String5");
    TestList .add("String6");
    for (Integer x : indexList) {
        TestList.remove(x);
    }
    for (String x : TestList ) {
        System.out.println(x);
    }

I want to remove objects at 1,3,4 (in TestList), but result: enter image description here

  • 1
    Um...you removed the contents of `ListTest`, not `TestList`, what did you expect? – Tim Biegeleisen Jul 28 '17 at 08:51
  • `ListTest` is not defined – Lino Jul 28 '17 at 08:51
  • 1
    Please read about java naming conventions. Variable names go **camelCase**. – GhostCat Jul 28 '17 at 08:52
  • Beyond that: you are calling `remove(Integer)` which tries to delete objects that are equal to the parameter. Try x.intValue() instead ;-) – GhostCat Jul 28 '17 at 08:54
  • First problem: you are calling `remove(Object)` (which is by removing equals values) instead of `remove(int)` (which is by index). Fix by `list.remove((int) x);` Second problem: once you removed an element, index of subsequent elements will be shifted down by 1. You need to find a way to tackle it – Adrian Shum Jul 28 '17 at 09:02

0 Answers0