数组a是{1,2,3} 数组b:{1,2,3,4 } 或者{1,4} 比较两个数组 要以a数组为准

如果a数组是{1,2,3}

b数组是{1,2,3,4 } 或者{1,4} 比较两个数组 要以a数组为准

那么删掉b数组中的4 该如何实现;

再解释下:如果b里面的元素比a多 就要把b里面多余的都删除 或者b里面有a里面没有的元素 也要删除b的这个元素,总之就是b中的元素只能<=a中的元素

主要是为了解决 从数据库里取出的字符串转化成数组和 前端页面input标签里的字符串数组就行比较 以前端的为准

public class question {
public static void main(String[] args) {
Integer[] a = {1,2,3};
Integer[] b = {1,2,3,4};
Integer[] c= {1,4};
List list1 = Arrays.asList(a);
List list2 = new ArrayList<>();
List list3 = new ArrayList<>();
for(Integer t : a){
if(list1.contains(t)){
list2.add(t);
}
}
b = (Integer[]) list2.toArray(new Integer[list2.size()]);
for(Integer m : b){
System.out.println(m);
}
System.out.println("----------");
for(Integer t : c){
if(list1.contains(t)){
list3.add(t);
}
}
c = (Integer[]) list3.toArray(new Integer[list3.size()]);
for(Integer m : c){
System.out.println(m);
}
}
}

楼上的解决方法就是可以得,改一下可以用了。


    a = [1, 2, 3, 9];
    var as = ',' + a.join(',') + ',';
    b = [1, 10, 3, 4, 5, 2];
    for (var i = b.length - 1; i >= 0; i--)
        if (as.indexOf(',' + b[i] + ',') == -1) b.splice(i, 1);
    console.log(b)

public static List compare(T[] t1, T[] t2) {
List list1 = Arrays.asList(t1);
List list2 = new ArrayList();
for (T t : t2) {
if (!list1.contains(t)) {
list2.add(t);
}
}
return list2;
}

你自己改下想要的条件,不知道你是不是这个意思

这是控制器部分的代码

if ("16".equals(requestType)) {// 修改和添加世卫组织推荐信息 耳穴治百病和耳穴表
            String disease_id = request.getParameter("id1") == null ? "" : request.getParameter("id1");// 耳穴治百病的id
            String name = request.getParameter("name") == null ? "" : request.getParameter("name");
            String desc = request.getParameter("desc") == null ? "" : request.getParameter("desc");
            String xueweisStr = request.getParameter("xueweis") == null ? "" : request.getParameter("xueweis");// 耳穴的id是一个数组
            String currentPageNum = request.getParameter("currentPageNum2") == null ? ""
                    : request.getParameter("currentPageNum2");
            int currentPageNum1 = Integer.parseInt(currentPageNum);
            int id1 = Integer.parseInt(disease_id);
            String[] xueweisArr = StringUtils.split(xueweisStr, ",");// String
            int[] xueweisArr1 = new int[xueweisArr.length];// String数组转int数组
            for (int i = 0; i < xueweisArr1.length; i++) {
                xueweisArr1[i] = Integer.parseInt(xueweisArr[i]);
                EarAcupointDisease100 earAcupointDisease100 = earStudyService.findEaAndEd100(id1, xueweisArr1[i]);// 根据疾病id和穴位num查询dt_ear_acupoint_disease_100这张表
                System.out.println(xueweisArr1[i]);
                if (earAcupointDisease100 != null) {
                    int num = earStudyService.updateEaAndEd100Swtj(id1, xueweisArr1[i]);
                    if (num != 0) {
                        request.setAttribute("msg", "修改成功");
                    }
                } else {
                    System.out.println("添加 xueweisArr1[i]:" + xueweisArr1[i]);
                    earStudyService.saveEaAndEd100Swtj(id1, xueweisArr1[i]);
                }
            }
            /*从库里取出疾病对应的穴位拼成数组*/
            List<EarAcupointDisease100> earAcupointDisease100=earStudyService.findSwApById(id1);
            String acupoint_numStr="";
            for (int i = 0; i < earAcupointDisease100.size(); i++) {
                int acupoint_num=earAcupointDisease100.get(i).getAcupoint_num();
                System.out.println("数据库里的值:"+acupoint_num);
                acupoint_numStr+=acupoint_num +",";
            }
            acupoint_numStr = acupoint_numStr.substring(0,acupoint_numStr.length()- 1);
            System.out.println(acupoint_numStr);




            List<EarDisease100> listEd100 = earDisease100Service.findEdById(id1);
            if (listEd100.size() != 0) {
                int num2 = earDisease100Service.updateEdSwtj(id1, name, desc);// 修改的仅是ear_disease_100这个表
                if (num2 != 0) {
                    request.setAttribute("ok", "修改成功");
                }
                return "manage/forJsp.do?&requestType=15&num1=" + currentPageNum1;
            } else {
                earDisease100Service.saveEdSwtj(id1, name, desc);
                request.setAttribute("ok", "添加成功");
                return "manage/forJsp.do?&requestType=15&num1=" + currentPageNum1;
            }
        }




List有一个方法retainAll,可以取两个List的交集

    public static void main(String[] args) {
        String[] result = retain(new String[]{"one","two","three"}, new String[]{"two","three","four"});
        System.out.println(Arrays.toString(result));//[two, three]
    }
    public static String[] retain(String[] a, String[] b){
        List<String> lista = Arrays.asList(a);
        List<String> listb = new ArrayList<String>(Arrays.asList(b));
        listb.retainAll(lista);
        return listb.toArray(new String[1]);
    }

数组移除??
提供个方案:
首先定义一个新数组 c对象;
写2个for循环,第一个循环a数组,内嵌b对象循环,每次循环a数组的元素时,循环一轮b数组,如果存在,则保存到c对象中。全部遍历之后,c对象保存的对象就是你想要的数据。

提供思路:

1、若数据量不大,实现方式很多;
2、若数据量很大,可以:
1)将a中元素存入hashset 中;
2)逐个便利b中元素, 若a中含有,保存。