1.1 以你喜欢的方式(思惟导图或其余)概括总结集合与泛型相关内容。
java
1.1 实验总结
在删除List中的元素中要考虑元素删除后的位置(代码以下:)编程
for (int i = 0; i < list.size(); i++) { if(list.get(i).equals(str)){ list.remove(i); i--; }
2.1 伪代码(简单写出大致步骤)数组
while(has next input){ count=get 键值; if(input is"!!!!!")){ break; } else if(count==null){ 存入map映射中 } else{ 键值+1; } }
2.2 实验总结
这里用到了匿名内部类来进行排序:
函数
3.1 截图你的提交结果(出现学号)
学习
3.2 伪代码(简单写出大致步骤)
创建索引阶段:测试
while(has next input){ lineNumber++;行数+1 if(input is"!!!!!")){ break; } else{ value=键值数组 if(value==null){ add 当前行数 in valuetemp put in map } if(value contains 当前行数){ 跳过余下操做 } else{ add 当前行数 in value put in map } } }
查找阶段网站
while(has next input){ separate the key words; find the all key words 交集 while(has next words) value[i].retainAll((value[i-1])); } output
3.3 实验总结
1,读一整句中的单词用:code
Scanner sc=new Scanner(str); while(sc.hasNext()){ temp=sc.next(); }
2,o1.retainAll(o2);返回o1,o2对象中重合的部分并返回至o1对象
编写一个Student类,属性为:
private Long id;
private String name;
private int age;
private Gender gender;//枚举类型
private boolean joinsACM; //是否参加过ACM比赛
建立一集合对象,如List,内有若干Student对象用于后面的测试。
4.1 使用传统方法编写一个方法,将id>10,name为zhang, age>20, gender为女,参加过ACM比赛的学生筛选出来,放入新的集合。在main中调用,而后输出结果。
该方法为:
放入的测试数据:
运行结果:
blog
4.2 使用java8中的stream(), filter(), collect()编写功能同4.1的函数,并测试。
参考网站(http://ifeve.com/stream/)
static List Selecte(List list){ List<Student> newlist=new ArrayList(); newlist=(ArrayList<Student>) list.parallelStream() .filter(student -> (((Student) student).getId() > 10L && ((Student) student).getName().equals("zhang") && ((Student) student).getAge() > 20 && ((Student) student).getGender().equals(Gender.女) && ((Student) student).isJoinsACM())) .collect(Collectors.toList()); return newlist; }
运行结果:
4.3 构建测试集合的时候,除了正常的Student对象,再往集合中添加一些null,而后从新改写4.2,使其不出现异常。
在数组中加入了null
只是在原先判断基础上,加入了student!=null
运行结果同上
5.1 截图你的提交结果(出现学号)
5.2 GeneralStack接口的代码
5.3 结合本题,说明泛型有什么好处
用泛型编写的代码能够被不一样类型的对象所重用
题中的Integer,Double,Car均可以调用GeneralStack的实现类ArrayListGeneralStack
泛型容许指定集合中元素的类型
如:
这样就能够无需使用有风险的强制类型转换,错误在编译阶段就能发现
基础参考文件GenericMain,在此文件上进行修改。
6.1 编写方法max,该方法能够返回List中全部元素的最大值。List中的元素必须实现Comparable接口。编写的max方法需使得String max = max(strList)能够运行成功,其中strList为List
代码以下:
运行结果:
6.2 选作:现有User类,其子类为StuUser,且均实现了Comparable接口。编写方法max1,基本功能同6.1,并使得max1(stuList);能够运行成功,其中stuList为List
运行结果:
6.3 选作:编写int myCompare(T o1, T o2, Comparator c)方法,该方法能够比较User对象及其子对象,传入的比较器c既能够是Comparator
题目集:jmu-Java-05-集合
函数(4-1),编程(5-3,5-4,5-5) 实验总结已经在做业中体现,不用写。