You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

数组差集实现及遗漏数迭代式Java编程求解

问题1:从list1中筛选出不在list2里的元素得到list3

给定两个数组:

int[] list1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] list2 = {2, 4, 6, 8, 10};

你之前尝试的list3[i] = list1[i] - list2[i]不可行,因为两个数组长度不同,且逻辑上是要筛选list1中不在list2内的元素,不是做数值减法。

解决方法

方法1:利用集合快速筛选(简洁高效)

把list2存入Set实现快速查找,再遍历list1筛选目标元素:

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ArrayFilter {
    public static void main(String[] args) {
        int[] list1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int[] list2 = {2, 4, 6, 8, 10};
        
        Set<Integer> excludeSet = new HashSet<>();
        for (int num : list2) {
            excludeSet.add(num);
        }
        
        List<Integer> list3Temp = new ArrayList<>();
        for (int num : list1) {
            if (!excludeSet.contains(num)) {
                list3Temp.add(num);
            }
        }
        
        // 转成int数组(如果需要)
        int[] list3 = list3Temp.stream().mapToInt(Integer::intValue).toArray();
        System.out.println(java.util.Arrays.toString(list3)); // 输出 [1, 3, 5, 7, 9]
    }
}

方法2:纯数组循环(不依赖集合)

通过双重循环判断元素是否需要保留:

public class ArrayFilter {
    public static void main(String[] args) {
        int[] list1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int[] list2 = {2, 4, 6, 8, 10};
        
        // 先统计目标数组长度
        int count = 0;
        for (int num1 : list1) {
            boolean isExcluded = false;
            for (int num2 : list2) {
                if (num1 == num2) {
                    isExcluded = true;
                    break;
                }
            }
            if (!isExcluded) count++;
        }
        
        // 填充目标数组
        int[] list3 = new int[count];
        int index = 0;
        for (int num1 : list1) {
            boolean isExcluded = false;
            for (int num2 : list2) {
                if (num1 == num2) {
                    isExcluded = true;
                    break;
                }
            }
            if (!isExcluded) list3[index++] = num1;
        }
        
        System.out.println(java.util.Arrays.toString(list3));
    }
}

问题2:Java作业——求未被分配任务的遗漏数

你的现有代码存在几个关键问题:固定了copy数组长度为5导致数据丢失、循环内的break直接终止了整个分配流程、没有跟踪学生的选中状态。以下是符合要求的迭代式实现:

完整代码

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

public class OmittedNumbers {
    public static void main(String[] args) {
        // 处理命令行参数
        if (args.length != 1) {
            System.out.println("请输入一个整数N作为命令行参数");
            return;
        }
        int N;
        try {
            N = Integer.parseInt(args[0]);
        } catch (NumberFormatException e) {
            System.out.println("参数必须是整数");
            return;
        }
        
        // 标记数组:index对应学生编号-1,true表示已被选中
        boolean[] selected = new boolean[N];
        
        // 从t=2开始依次分配任务
        for (int t = 2; t <= N; t++) {
            int count = 0; // 统计当前未被选中的学生位置
            for (int i = 0; i < N; i++) {
                if (!selected[i]) {
                    count++;
                    if (count == t) {
                        selected[i] = true;
                        count = 0; // 重置计数,继续寻找下一个目标
                    }
                }
            }
        }
        
        // 收集未被选中的学生编号
        List<Integer> omitted = new ArrayList<>();
        for (int i = 0; i < N; i++) {
            if (!selected[i]) {
                omitted.add(i + 1);
            }
        }
        
        // 输出结果
        System.out.println("未被分配任务的遗漏数:");
        System.out.println(omitted);
    }
}

代码说明

  1. 命令行参数处理:运行时需传入整数N,例如java OmittedNumbers 10
  2. 标记数组逻辑:用selected数组跟踪学生是否被选中,避免频繁修改原数组
  3. 分配流程:对每个t,遍历所有学生,仅统计未被选中的个体,数到第t个时标记为选中
  4. 结果收集:遍历标记数组,整理出所有未被标记的学生编号

测试示例

当N=10时,程序输出:

未被分配任务的遗漏数:
[1, 7]

内容的提问来源于stack exchange,提问作者soosius

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 11:35:31