问题
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:
- When the element is not found List::indexOf returns
-1
. - It's also important to check
(index + 3) < list.size()
to avoidIndexOutOfBoundsException
. For example, let's saya
was found at index2
and there was only one element after that i.e. the list had only4
elements in all. In that case, trying to access (get or remove) an element from index4
or5
would throwIndexOutOfBoundsException
.
来源:https://stackoverflow.com/questions/61566609/delete-arraylist-from-index-i-to-i3