-1

I currently have an Arraylist as shown below.

T[] v = { v1,v2, v3, v4 }; 

I also have another array list:

removeT[] x = {v2, v4}

From the second, I would like these two values to be removed from the initial Array list. What are the required steps?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
D7170
  • 1
  • 2

2 Answers2

0

The best way to do it is something like this:

for (String each : removeT) {
    if(v.equals(each)){
        v.remove(each)
    }
}

You can find more information on ArrayLists here.

Stephopolis
  • 1,765
  • 9
  • 36
  • 65
0

I don't know of any operation that will do this directly on an array, so the solution I have to convert the arrays to a list:

    String[] v = new String[]{ "v1", "v2", "v3", "v4" };
    String[] x = new String[]{ "v1", "v4" };

    List<String> list1 = new ArrayList<String>();
    list1.addAll(Arrays.asList(v));

    List<String> list2 = Arrays.asList(x);

    list1.removeAll(list2);

and then when you are finished convert the list back to an array.

The problem of doing this directly on an array is that you would end up with null entries, which may create other issues, depending on your usage.

Andre M
  • 6,649
  • 7
  • 52
  • 93