第九次作业
1. 本周学习总结
1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容。
1.2 选做:收集你认为有用的代码片段遍
- 遍历Map的键值对:
Map<String,Integer>map = new HashMap<>();
map.put("a",90);
map.put("b",80);
for(Map.Entry<Stirng,Integer>e:map.entrySet()){
System.out.println(e.getKey()+"="+e.getValue());}
- 泛型的用法
List<String> strList = new ArrayList<>();
strList.add("1");
String str1 = strList.get(0);
2. 书面作业
本次作业题集集合
1. List中指定元素的删除(题集题目)
1.1 实验总结。并回答:列举至少2种在List中删除元素的方法。
- 实验总结:该题可以用很多种方法实现删除,我用了两种,一个是用List的remove方法,一个是用迭代器inerator来实现删除元素,这两种应该是最基本的,而且使用起来比较简单,会了其中一种方法就可以把这道题解决。
//使用List的remove方法删除
for (int m = list.size()-1; m >=0; m--) {
if(list.get(m).equals(word))
list.remove(m);
}
//使用迭代器inerator进行删除
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
String word = iterator.next();
if (word.equals(str)) {
iterator.remove();
}
}
2. 统计文字中的单词数量并按出现次数排序(题集题目)
2.1 伪代码(不得复制代码,否则扣分)
定义一个TreeSet
if(str1.equals("!!!!!"))
break;
System.out.println(str.size());
for(int i=0;i<10;i++)//单词数量{
System.out.println(arr[i]);
}
2.2 实验总结
- 上周已经写过这题的总结,那就再补充一些吧,我这题是用TreeSet来进行统计的,关键代码如下:
TreeSet<String> str=new TreeSet<String>();
while(in.hasNext()){
String str1=in.next();
if(str1.equals("!!!!!"))
break;
str.add(str1);
}
3. 倒排索引(题集题目)
本题较难,做不出来不要紧。但一定要有自己的思考过程,要有提交结果。
3.1 截图你的代码运行结果
3.2 伪代码(不得复制代码,否则扣分)
while(line not equals(!!!!!)){
map.put(line,1);
String [] arr=line.spilt(" ");
for(i<arr.length){
if(map.containsKey(arr[i])){
add line;
else
add line and arr[i];
}
if(list.isEmpty)
System.out.println(found 0 results);
3.3 实验总结
- 该题确实是有难度,不过我的大概思路是:应该要使用到Map,用map装关键字和索引值,然后是根据索引值找到对应每个关键字,如果找到,输出行集与行集内每一行的内容,如果没找到输出
found 0 results
。
4.Stream与Lambda
编写一个Student类,属性为:
private Long id;
private String name;
private int age;
private Gender gender;//枚举类型
private boolean joinsACM; //是否参加过ACM比赛
创建一集合对象,如List
4.1 使用传统方法编写一个搜索方法List<Student> search(List<Student> stuList, Long id, String name, int age, Gender gender, boolean joinsACM)
,然后调用该方法将id>某个值,name为某个值, age>某个值, gender为某个值,参加过ACM比赛的学生筛选出来,放入新的集合。在main中调用,然后输出结果。(截图:出现学号、姓名)
public static void main(String[] args) {
ArrayList<Student> list=new ArrayList<Student>();
List.add(new Student(11L,"zhang",21,Gender.female,true));
List.add(new Student(9L,"li",21,Gender.male,true));
List.add(new Student(22L,"wang",51,Gender.male,true));
List.add(new Student(45L,"zhang",25,Gender.female,true));
ArrayList<Student> list1 = new ArrayList<Student>();
for (Student student : list) {
if (student.getId() <10L && student.getName().equals("zhang") && student.getAge() > 20 && student.getGender().equals(Gender.female) && student.isJoinsACM()) {
List1.add(student);
System.out.println("冯一201621123066");
System.out.println(student);
}
}
}
4.2 使用java8中的stream(), filter(), collect()编写功能同4.1的代码,并测试(要出现测试数据)。构建测试集合的时候,除了正常的Student对象,再往集合中添加一些null,你编写的方法应该能处理这些null而不是抛出异常。(截图:出现学号)
//使用java8中的一些方法编写
ArrayList<Student> List = (ArrayList<Student>) List.Stream().filter(student -> (student.getId() < 10L
&& student.getName().equals("zhang")
&& student.getAge() > 20
&& student.getGender().equals(Gender.female)
&& student.isJoinsACM())).collect(Collectors.toList());
//往集合中添加一些null
ArrayList<Student> arrayList2 = (ArrayList<Student>) arrayList.parallelStream()
.filter(student -> student != null && (student.getId() <10L && student.getName().equals("zhang")
&& student.getAge() > 20 &&
student.getGender().equals(Gender.female)
&& student.isJoinsACM()))
.collect(Collectors.toList())
5. 泛型类:GeneralStack
题集jmu-Java-05-集合
之GeneralStack
5.1 GeneralStack接口的代码
interface GeneralStack
{
public Object push(Object item);
public Object pop();
public Object peek();
public boolean empty();
public int size();
}
5.2 结合本题与以前作业中的ArrayListIntegerStack
相比,说明泛型有什么好处
- 在学泛型之前,我只知道类和方法只能使用具体的类型,学了过后,知道泛型(只是一个编译现象)不需要知道集合内元素的具体类型,而且无需不安全的强制转换,以前IntegerStack接口只能用于存放Integer类型的数据,但运用泛型的知识,写了通用的GeneralStack接口,对任何引用类型的数据都适用,方便了许多。
3.码云及PTA
题目集:jmu-Java-05-集合
3.1. 码云代码提交记录
在码云的项目中,依次选择“统计-Commits历史-设置时间段”, 然后搜索并截图
3.2 截图PTA题集完成情况图
需要有两张图(1. 排名图。2.PTA提交列表图)
3.3 统计本周完成的代码量
需要将每周的代码统计情况融合到一张表中。
自己的目标能实现吗?
周次 | 总代码量 | 新增代码量 | 总文件数 | 新增文件数 |
---|---|---|---|---|
1 | 0 | 0 | 0 | 0 |
2 | 492 | 492 | 10 | 10 |
3 | 435 | -57 | 6 | -4 |
4 | 0 | 0 | 0 | 0 |
5 | 312 | -123 | 8 | 2 |
6 | 525 | 213 | 7 | -1 |
7 | 236 | -289 | 3 | -4 |
8 | 305 | 69 | 3 | 0 |
9 | 183 | -122 | 5 | 2 |
10 | 312 | 129 | 3 | -2 |
- 之前自己的目标只是一周100到200行代码,综上来看,都达到了自己的目标,但还是要继续努力,努力学好Java课程。
4. 评估自己对Java的理解程度
尝试从以下几个维度评估自己对Java的理解程度
维度 | 程度 |
---|---|
语法 | PTA的题目还是要靠同学讲解过后,给予我思路才能自己写出来 |
面向对象设计能力 | 对面对对象的思想还有些生疏,它的思想还是有些抽象 |
应用能力 | 一个人编写小程序还是有难度 |
至今为止代码行数 | 2800 |