Delete ArrayList from index i to i+3

限于喜欢 提交于 2020-05-17 06:25:08

问题


Sorry if this question is too easy or has been asked before, I'm new to programming, I have an ArrayList called ar declared as ArrayList ar = new ArrayList();

And I have the following code:

String delnr = "";
delnr = cin.nextLine();

if(ar.contains(delnr))
{
    for (int i = 0; i < ar.size(); i++)
    {
        if(ar.get(i).equals(delnr))
        {
            ar.remove(i);

            //Here I want to remove i+1 i+2 and i+3 as well.
        }
    }
}

Any logical attempt I try will give me the IndexOutOfBounds error.

My ArrayList is of type [dd1, x, y, z, dd2, x, y, z, dd3, x, y, z, ...]

delnr can only be a dd1 or dd2, etc..

So basically if my delnr is dd2 I want to remove dd2 and the x, y and z that come after, my new list should look like:

[dd1, x, y, z, dd3, x, y, z, ...]

回答1:


You can't use removeRange. Just do it like this. This works because the subList is an immutable view of the original list. So changing the subList changes the original list.

List<Integer> list = new ArrayList<>(List.of(1,2,3,10,20,30,40,4,5,6,7));
System.out.println(list);
list.removeAll(list.subList(3,7));
System.out.println(list);

Prints

[1, 2, 3, 10, 20, 30, 40, 4, 5, 6, 7]
[1, 2, 3, 4, 5, 6, 7]

In your case it would be something like

List<Integer> list = 
    new ArrayList<>(List.of(1, 2, 3, 10, 20, 30, 40, 4, 5, 6, 7));
for (int i = 0; i < list.size(); i++) {
    if (list.get(i) == 10) {
        list.removeAll(list.subList(i, i + 4));
    }
}
System.out.println(list);




回答2:


The most important thing to understand when you remove element using an index is that the List shrinks each time i.e. you just need to call remove again to delete the next element because the next element has taken the place of the last removed element.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>(Arrays.asList("c", "b", "a", "d", "e", "j", "k"));
        System.out.println("Original list: ");
        System.out.println(list);

        // Let's say we are searching `a`
        int index = list.indexOf("a");
        if (index != -1 && ((index + 3) < list.size())) {
            // Remove element at index, index + 1, index + 2 and index +3
            list.remove(index);
            list.remove(index);
            list.remove(index);
            list.remove(index);
        }

        System.out.println("Updated list: ");
        System.out.println(list);
    }
}

Output:

Original list: 
[c, b, a, d, e, j, k]
Updated list: 
[c, b, k]

You can simplify the code given above by putting the repeated code inside a loop as follows:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>(Arrays.asList("c", "b", "a", "d", "e", "j", "k"));
        System.out.println("Original list: ");
        System.out.println(list);

        // Let's say we are searching `a`
        int index = list.indexOf("a");
        if (index != -1 && ((index + 3) < list.size())) {
            // Remove element at index, index + 1, index + 2 and index +3
            for (int i = 1; i <= 4; i++) {
                list.remove(index);
            }
        }

        System.out.println("Updated list: ");
        System.out.println(list);
    }
}

Output:

Original list: 
[c, b, a, d, e, j, k]
Updated list: 
[c, b, k]

Notes:

  1. When the element is not found List::indexOf returns -1.
  2. It's also important to check (index + 3) < list.size() to avoid IndexOutOfBoundsException. For example, let's say a was found at index 2 and there was only one element after that i.e. the list had only 4 elements in all. In that case, trying to access (get or remove) an element from index 4 or 5 would throw IndexOutOfBoundsException.


来源:https://stackoverflow.com/questions/61566609/delete-arraylist-from-index-i-to-i3

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!