简体   繁体   English

Java中的迭代器-删除范围内的数字

[英]Iterators in java - remove numbers in a range

I'm trying to remove numbers from a LinkedList using an iterator. 我正在尝试使用迭代器从LinkedList中删除数字。 I can't get it to remove numbers from only between to variables. 我不能删除仅从变量之间的数字。

My list contains these values: 1, 1, 2, 0, 4, 5, 6, 8, 8, 3, 11, 9, 12, 0, 14, 0, 16] 我的列表包含以下值:1、1、2、0、4、5、6、8、8、3、11、9、12、0、14、0、16]

The call of the method removeEvenInRange(list,5,13) should remove the even numbers between index 5 and 13, ending up with a list containing [1,1,2,0,4,5,3,11,9,0,14,0,16 调用方法removeEvenInRange(list,5,13)应该删除索引5和13之间的偶数,最后得到一个包含[1,1,2,0,4,5,3,11,9,0,14,0,16的列表[1,1,2,0,4,5,3,11,9,0,14,0,16

I don't know how to "tell" it to only iterate between the indexes 5 - 13. Any help on how to solve this would be much appreciated. 我不知道如何“告诉”它仅在索引5-13之间进行迭代。对解决此问题的任何帮助将不胜感激。

This is what I have so far: 这是我到目前为止的内容:

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.LinkedList;
import java.util.ListIterator;

public class Ex11_3_RemoveEvenInRange {

public static void main(String[] args) {

    List<Integer> list = new LinkedList<>();
    list.addAll(Arrays.asList(1,1,2,0,4,5,6,8,8,3,11,9,12,0,14,0,16));

    removeEvenInRange(list, 5, 13);
}

private static void removeEvenInRange(List<Integer> list, int i, int j) {


            Iterator<Integer> itr = list.iterator();

            for (int k = i; k < j; k++) {

                int element = itr.next();

                if (element % 2 == 0) {
                    itr.remove();

                }
            }


                System.out.println(list);

    }
}

You can subList between two indexes. 您可以subList两个指标之间。 Try, 尝试,

static void removeEvenInRange(List<Integer> list, int i, int j){

    List<Integer> subList= list.subList(i, j);
    Iterator<Integer> itr = subList.iterator();


    while (itr.hasNext()){
        int element = itr.next();
        if (element % 2 == 0) {
            itr.remove();
        }
    }
    System.out.println(list);
}

You can't, unless you call next() n (5 in this case) times before running your code - which would be insane. 除非您在运行代码之前调用next() n次(本例中为5次next() ,否则您将无法进行操作-这太疯狂了。

Your best option is probably to split your list into three sublists , iterate over the middle one, and merge the lists after you're done. 最好的选择可能是将列表分成三个子列表 ,在中间一个子列表上进行迭代,并在完成后合并列表。

Another approach would be using get(index) and remove(index) on the list directly, which saves the split/merge step, but you'll have to make sure you adjust the index for every remove. 另一种方法是直接在列表上使用get(index)remove(index) ,这样可以节省拆分/合并步骤,但是您必须确保为每次删除都调整索引。 The split/merge approach is less error-prone. 拆分/合并方法不太容易出错。

edit: The Answer by @Masud has example code for this, although it's missing the merge step. 编辑:@Masud的答案具有示例代码,尽管它缺少合并步骤。

What your current implementation is doing is removing all even numbers from the list from index 0 through index j - i - 1 , not i through j - 1 . 您当前的实现方式是从索引0到索引j - i - 1而不是从ij - 1的列表中删除所有偶数。 The range is shifted because you're not iterating until k is in range. 范围已移动,因为您直到k都在范围内才进行迭代。

Start your for loop at index 0 , so your index is consistent with the iterator's index. 从索引0开始for循环,以便您的索引与迭代器的索引一致。 Then add an if statement to determine if your index k is in the proper range between i and j . 然后添加一个if语句,以确定索引k是否在ij之间的适当范围内。

for (int k = 0; k < j; k++) {
    int element = itr.next();

    if (k >= i && k < j)
    {
        if (element % 2 == 0) {
            itr.remove();
        }
    }
}

If you want use an iterator you can write something like: 如果要使用迭代器,则可以编写如下内容:

 for (int k = 0; k < j; k++) {

         int element = itr.next();
             if (k>=i) {
                    if (element % 2 == 0) {
                        itr.remove();
                       }
                   }

          }

but is more efficient if you use get(int index) 但是如果使用get(int index)会更有效

You will need to count and advance the iterator until you reach the start index. 您将需要计数并推进迭代器,直到达到起始索引。 Then you continue counting in parallel of iterating and removing the elements you want to remove through the iterator, until the count reaches the end index. 然后,您继续并行进行计数,以迭代和删除要通过迭代器删除的元素,直到计数达到结束索引。

If you stick to a LinkedList , the above process will be more efficient than removing through the List#remove(index) method (as suggested in some comments), since each call to remove will go through the list to find the index. 如果您坚持使用LinkedList ,则上述过程比通过List#remove(index)方法进行List#remove(index) (如某些注释中所建议的List#remove(index)效率更高,因为每次调用remove都会遍历列表以查找索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM