有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在迭代时从列表中删除子列表

我想计算元素的数量,如果它们符合标准,就删除一些元素。使用collect和removeAll删除不起作用,因为它会删除所有相等的元素,我想删除一个范围,而不是全部。 我试着使用子列表。clear()但即使我在使用它,我也会得到ConcurrentModificationException。删除()

public static List<Integer> controlOccurrences(List<Integer> sortedArr, int m) {
    int writelndex = 0, count=1;
    List<List<Integer>> toRemove = new ArrayList<>();

    for (int i = 1; i < sortedArr.size(); ++i) {

        if (sortedArr.get(i-1).equals(sortedArr.get(i))) {
            count++;
        } else {
            if(count == m) {
                int nCopies = Math.min(2,m);
                List<Integer> c = sortedArr.subList(writelndex + nCopies, i);
                toRemove.add(c);
            }
            count = 1;
            writelndex = i;
        }
    }

    Iterator<List<Integer>> iterator = toRemove.iterator();
    while (iterator.hasNext()) {
        List<Integer> integers = iterator.next();
        iterator.remove();
        integers.clear();
    }
    return sortedArr;
}

编辑:添加示例:
假设我们有以下列表:(1,2,2,2,2,3,3,4,4,5,5)和m=3。这意味着所有出现m次的数字都应该出现2次(Math.min(2,3))。所以预期结果是(1,2,2,2,3,3,4,4,5,5)

非常优雅地解决了任务。 然而,我仍然不清楚为什么在使用迭代器时抛出ConcurrentModificationException。remove()以及在遍历列表时如何从列表中删除子列表


共 (2) 个答案

  1. # 1 楼答案

    如果我正确理解了任务,那么算法

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.List;
    
    public class Test {
    
        public static void main(String[] args) throws IOException {
    
            int m = 3;
    
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(System.in));
    
            int numbers;
    
            List<Integer> sortedList = new ArrayList<>();
    
            // Fill in the list with values
            for (int i = 0; i < 13; i++) {
                numbers = Integer.parseInt(reader.readLine());
                sortedList.add(numbers);
            }
    
            System.out.println(controlOccurrences(sortedList, m));
    
        }
    
        public static List<Integer> controlOccurrences(List<Integer> sortedArr, int m) {
    
            int count= 1;
    
    
            for (int i = 0; i < sortedArr.size(); i++) {
    
                for (int j = 0; j < sortedArr.size(); j++) {
    
                    if (sortedArr.get(i).equals(sortedArr.get(j)) && i != j) {
    
                      count += 1;
    
                    }
    
                }
    
                if (count == m) {
                    sortedArr.remove(i);
                    count = 1;
                } else {
                    count = 1;
                }
    
            }
    
            return sortedArr;
        }
    
    }
    
  2. # 2 楼答案

    希望这有助于:

        static List<Integer> controlOccurrences(List<Integer> sortedArr, int m) {
                //make the count of each element
                Map<Integer, Long> result = sortedArr.stream()
                        .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
    
    
                for (Map.Entry<Integer, Long> entry : result.entrySet()) {
                    if (entry.getValue() == m) {
                        // Here 2 is hard coded. You can make a variable and pass it to the method with a parameter
                        for (int i = 0; i < m - 2; i++) 
                        {
                            sortedArr.remove(entry.getKey());
                        }
                    }
                }
    
                return sortedArr;
            }
    

    N.B:这段代码并不完美,因为我假设m>=2