英文:
removeRange method with ArrayList
问题
以下是您要翻译的内容:
对于课程中的这个方法,我遇到了困难。我必须编写一个方法,该方法接受最大值和最小值的范围,并删除所有包含在这些值之间的元素。例如,如果我在主方法中调用removeRange(list, 5, 7);,它应该将以下值:[7, 9, 4, 2, 7, 7, 5, 3, 5, 1, 7, 8, 6, 7],更改为:[9, 4, 2, 3, 1, 8]。但是,当我编译时,我始终得到[9, 4, 2, 7, 3, 1, 8, 7]。以下是带有参数的removeRange方法。
public static void removeRange(ArrayList<Integer> list, int min, int max) {
for (int i = 0; i < list.size(); i++) {
if ((list.get(i) >= min && list.get(i) <= max)) {
list.remove(i);
}
}
}
英文:
Having trouble figuring this method out for class. I have to write a method that takes the range of a max and min and removes all elements containing those values. FOr example, if I put in the main method removeRange(list, 5, 7); in the main method, it should take the values of [7, 9, 4, 2, 7, 7, 5, 3, 5, 1, 7, 8, 6, 7] and turn it to [9, 4, 2, 3, 1, 8] . But I keep getting [9, 4, 2, 7, 3, 1, 8, 7] when I compile. Here is my removeRange method with the parameters.
public static void removeRange(ArrayList<Integer> list, int min, int max) {
for(int i = 0; i < list.size(); i++) {
if((list.get(i) >= min && list.get(i) <= max)) {
list.remove(i);
}
}
}
答案1
得分: 2
当您从您的 ArrayList
中移除一个元素时,随之移除的元素后面的索引会减少,因此在循环的下一次迭代中,您的循环会跳过下一个元素。
您可以通过在移除元素时将 i
减少来进行调整:
public static void removeRange(ArrayList<Integer> list, int min, int max) {
for(int i = 0; i < list.size(); i++) {
if((list.get(i) >= min && list.get(i) <= max)) {
list.remove(i);
i--;
}
}
}
或者通过反向迭代:
public static void removeRange(ArrayList<Integer> list, int min, int max) {
for(int i = list.size() - 1; i >= 0; i--) {
if((list.get(i) >= min && list.get(i) <= max)) {
list.remove(i);
}
}
}
英文:
When you remove an element from your ArrayList
, the indices of the elements following the removed element are decremented, so your loop would skip the next element in the following iteration.
You can account for it by decrementing i
when you remove an element:
public static void removeRange(ArrayList<Integer> list, int min, int max) {
for(int i = 0; i < list.size(); i++) {
if((list.get(i) >= min && list.get(i) <= max)) {
list.remove(i);
i--;
}
}
}
Or by iterating backwards:
public static void removeRange(ArrayList<Integer> list, int min, int max) {
for(int i = list.size() - 1; i >= 0; i--) {
if((list.get(i) >= min && list.get(i) <= max)) {
list.remove(i);
}
}
}
答案2
得分: 2
在你的数组 7, 9, 4, 2, 7, 7, 5, 3, 5, 1, 7, 8, 6, 7
中,迭代如下 ->
为了方便,让范围为7到7。
最初,i = 0,大小 = 14:移除了7。大小 = 13
新数组 - 9, 4, 2, 7, 7, 5, 3, 5, 1, 7, 8, 6, 7
现在 i = 1,大小 = 13:您现在正在访问a[1],即4,所以跳过了9。
稍后 i = 3,大小 = 13,移除了7。大小 = 12
新数组 - 9, 4, 2, 7, 5, 3, 5, 1, 7, 8, 6, 7
现在 i = 4,大小 = 12:您现在正在访问a[4],即5,所以跳过了7。
希望你现在能看到你的逻辑中的缺陷。
尝试每次移除一个元素时执行i = i-1。
英文:
In your array 7, 9, 4, 2, 7, 7, 5, 3, 5, 1, 7, 8, 6, 7
the iteration goes like this ->
Let range be 7 to 7 for ease.
initially, i = 0, size = 14: 7 is removed. size = 13
new array - 9, 4, 2, 7, 7, 5, 3, 5, 1, 7, 8, 6, 7
now i = 1, size = 13: you are now accessing a[1] ie, 4 so 9 is skipped.
later i = 3, size =13, 7 is removed. size = 12
new array - 9, 4, 2, 7, 5, 3, 5, 1, 7, 8, 6, 7
now i = 4, size = 12: you are now accessing a[4] ie, 5 so 7 is skipped.
hope the flaw in your logic is visible to you now.
Try doing i = i-1 every time you remove an element.
答案3
得分: 0
你可以像这样使用 Iterator
。
public static void removeRange(ArrayList<Integer> list, int min, int max) {
for (Iterator<Integer> i = list.iterator(); i.hasNext();) {
int element = i.next();
if (element >= min && element <= max)
i.remove();
}
}
以及
ArrayList<Integer> list = new ArrayList<>(List.of(7, 9, 4, 2, 7, 7, 5, 3, 5, 1, 7, 8, 6, 7));
removeRange(list, 5, 7);
System.out.println(list);
输出:
[9, 4, 2, 3, 1, 8]
英文:
You can use Iterator
like this.
public static void removeRange(ArrayList<Integer> list, int min, int max) {
for (Iterator<Integer> i = list.iterator(); i.hasNext();) {
int element = i.next();
if (element >= min && element <= max)
i.remove();
}
}
and
ArrayList<Integer> list = new ArrayList<>(List.of(7, 9, 4, 2, 7, 7, 5, 3, 5, 1, 7, 8, 6, 7));
removeRange(list, 5, 7);
System.out.println(list);
output:
[9, 4, 2, 3, 1, 8]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论