package com.Fangfa_Shuzu;
/
练习:
筛选两张Excel表格中不同的内容?
比如说我有两张名单表,一张是报名表,一张是交费表,如何筛选出未交费的名字?
public class Fangfa_lianxi3 {
static String saixuan (String[] nams0,String[] nams1){
String name1 ;
String name2 ;
String name ;
for(int i = 0; i < nams0.length; i++){
name1 = nams0[i];
for(int j = 0; j < nams1.length; j++){
name2 = nams1[j];
if(name1 != name2 ){
String name = name1;
}
}
}
return name;
}
public static void main(String[] args) {
String[] nams0 = {"曹理鹏","李建峰","林亚山","孙卓","郭鹏"};
String[] nams1 = {"赵明杰","张华","林家富","黄进文","林亚山","孙卓","郭鹏","周雄","粟陶陶","曹理鹏","李建峰"};
String name = saixuan(nams0,nams1);
System.out.println(name);
}
}
List<String> already = Arrays.asList(nams0);
List<String> all = Arrays.asList(nams1);
List<String> notPay = new LinkedList<>();
for (String name : all) {
if (!already.contains(name)) {
notPay.add(name);
}
}
这个代码是解决这种业务最耗时的办法, 不过也是最容易理解的, 从你这个业务上看, 问题不大
1、return name; Variable 'name' might not have been initialized,当你不满足if条件的时候,name没有值
2、String name = name1; Variable 'name' is already defined in the scope,同一个作用域变量名重复了
3、字符串比较用equals
4、你的业务逻辑写的有问题,找出未交费的名单,saixuan方法的返回值应该是一个集合;判断是否交费的逻辑也不正确,双层for循环,拿报名表的名单去跟交费表的名单的每一个人都对比一次,比如外层第一次遍历,在报名表里,除了“曹理鹏”,其他人都被列为未交费名单,这样下来,外层循环一次,就会有10个人列入未交费名单,循环结束你会发现,所有人都被列入未交费名单,而且重复出现