首页 技术 正文
技术 2022年11月11日
0 收藏 801 点赞 4,874 浏览 4914 个字

1. 本周学习总结

1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容。


2. 书面作业

本次作业题集集合

1. List中指定元素的删除(题目4-1)

        private static void remove(List<String> list, String word) {//删除List中指定元素的删除
for (Iterator<String> iterator = list.iterator(); iterator.hasNext();) {//使用Iterator迭代器遍历list
String str = (String) iterator.next();
if (str.equals(word))
iterator.remove();
}}private static List<String> convertStringToList(String nextLine) {//把一行字符串转换成一列表list
List<String> list=new ArrayList<String>();
Scanner sc=new Scanner(nextLine);//创建扫描器
while(sc.hasNext()){//遍历
list.add(sc.next());
}
sc.close();
return list;
}

1.1 实验总结

  • Iterator<E> iterator();这个方法必须分配一个新数组,即使这个列表是由一个数组支持的。
  • 返回的数组元素类型是Object,调用者可以自由地修改iterator返回的数组。
  • 扫描器可以用做遍历使用。

2. 统计文字中的单词数量并按出现次数排序(题目5-3)

2.1 伪代码(简单写出大体步骤)

//把不重复单词装入集合strSet
while (in.hasNext()) {
String str = in.next();
if(str.equals("!!!!!"))
break;
strSet.add(str);
}
//控制输出
if (strSet.size() <= 10)
for (String s : strSet) {
System.out.println(s);
}
else{
int m=10;
for (String s : strSet) {
if (m-- <= 0)
break;
System.out.println(s);
}
}

2.2 实验总结

  • TreeSet类实现Set接口,不含重复对象,且TreeSet类的对象是按特定方式排序的,省去多写代码实现排序的麻烦。

3. 倒排索引(题目5-4)

3.1 截图你的提交结果(出现学号)

3.2 伪代码(简单写出大体步骤)

Set<String> strset = new TreeSet<String>();
List<String> strList = new ArrayList<String>();
String str = in.nextLine();
while (!str.equals("!!!!!")) {
//把每个单词放入集合strList
}
Map<String, ArrayList<Integer>> strmap = new TreeMap<String, ArrayList<Integer>>();
for (int i = 0; i < strList.size(); i++) {
String[] str1 = strList.get(i).split(" ");
for (int j = 0; j < str1.length; j++) {
//每个单词对应的行数一起放入集合strmap}
}
List<Map.Entry<String, ArrayList<Integer>>> arrayList = new ArrayList<Map.Entry<String, ArrayList<Integer>>>(
strmap.entrySet());
for (Map.Entry<String, ArrayList<Integer>> entry : arrayList) {
//输出索引列表
}
while (in.hasNextLine()) {
String str2 = in.nextLine();
String[] str2list = str2.split(" ");
ArrayList<Integer> arr = new ArrayList<Integer>();
for (int i = 0; i < strset.size(); i++) {
for (int j = 0; j < str2list.length; j++) {
//找出共有行数
}
}
if (!arr.isEmpty()) {
System.out.println(arr);
//输出所在行的字符串
}
else
System.out.println("found 0 results");
}

3.3 实验总结

  • 很难做,想的比较复杂
  • 需要熟练掌握Set,List,Map

4. Stream与Lambda

编写一个Student类,属性为:

private Long id;
private String name;
private int age;
private Gender gender;//枚举类型
private boolean joinsACM; //是否参加过ACM比赛

创建一集合对象,如List,内有若干Student对象用于后面的测试。

List<Student> stulist = new ArrayList<Student>();
stulist.add(new Student((long) 6, "Li", 20, Gender.女, true));//
stulist.add(new Student((long) 11, "Bai", 21, Gender.女, true));//
stulist.add(new Student((long) 12, "Zhang", 18, Gender.男, false));//
stulist.add(new Student((long) 8, "Xu", 21, Gender.男, true));//
stulist.add(new Student((long) 10, "Wang", 21, Gender.女, true));//
stulist.add(new Student((long) 9, "Lin", 21, Gender.女, false));//
stulist.add(new Student((long) 13, "Chen", 21, Gender.男, true));//
stulist.add(new Student((long) 7, "Li", 19, Gender.男, false));//
stulist.add(new Student((long) 14, "Guo", 22, Gender.女, true));//往集合stulist里添加若干Student对象

4.1 使用传统方法编写一个方法,将id>10,name为zhang, age>20, gender为女,参加过ACM比赛的学生筛选出来,放入新的集合。在main中调用,然后输出结果。

for (Student e : filtrate(stulist)) {//遍历调用`filtrate()`函数返回得到的列表list
System.out.println(e);//输出
}
//filtrate()方法过滤筛选后的对象加入list中返回
public static List<Student> filtrate(List<Student> stulist) {
List<Student> list = new ArrayList<Student>();
for (Student e : stulist)
if (e.getId() > 10 && e.getAge() > 20 && e.getGender().equals(Gender.女) && e.isJoinsACM())
list.add(e);
return list;
}
}

运行结果

4.2 使用java8中的stream(), filter(), collect()编写功能同4.1的函数,并测试。

ArrayList<Student> list = (ArrayList<Student>) stulist.stream().filter(e -> e.getId() > 10)
.filter(e -> e.getAge() > 20).filter(e -> e.getGender().equals(Gender.女)).filter(e -> e.isJoinsACM())
.collect(Collectors.toList());list.forEach(System.out::println);

运行结果同上。

4.3 构建测试集合的时候,除了正常的Student对象,再往集合中添加一些null,然后重新改写4.2,使其不出现异常。

//与4.2相比多加了`.filter(e -> e != null)`,且要放在首位,否则还会出现异常
ArrayList<Student> list = (ArrayList<Student>) stulist.stream().filter(e -> e != null)
.filter(e -> e.getId() > 10).filter(e -> e.getAge() > 20).filter(e -> e.getGender().equals(Gender.女))
.filter(e -> e.isJoinsACM()).collect(Collectors.toList());list.forEach(System.out::println);

修改后结果未出现异常。

5. 泛型类:GeneralStack(题目5-5)

5.1 截图你的提交结果(出现学号)

5.2 GeneralStack接口的代码

interface GeneralStack<T> {
public T push(T item);public T pop();public T peek();public boolean empty();public int size();
}

5.3 结合本题,说明泛型有什么好处

  • 无需使用有风险的强制转换
  • 错误在编译阶段就能发现
  • 泛型可以指定集合中元素的类型,得到强类型
  • GeneralStack<Integer> stack = new ArrayListGeneralStack<Integer>();GeneralStack<Double> stack = new ArrayListGeneralStack<Double>();GeneralStack<Car> stack = new ArrayListGeneralStack<Car>();本题中只编写了一个ArrayListGeneralStack泛型类,就可以创建多个不同类型的对象

6. 泛型方法

基础参考文件GenericMain,在此文件上进行修改。

6.1 编写方法max,该方法可以返回List中所有元素的最大值。List中的元素必须实现Comparable接口。编写的max方法需使得String max = max(strList)可以运行成功,其中strList为List类型。也能使得Integer maxInt = max(intList);运行成功,其中intList为List类型。

public class GenericMain {public static void main(String[] args) {List<String> arrayList = new ArrayList<String>();
arrayList.add("abc");
arrayList.add("DEF");
arrayList.add("ghi");
System.out.println(max(arrayList));
}
public static <T extends Comparable<T>> T max(List<T> list) {
return Collections.max(list);
}
}

运行结果


3. 码云上代码提交记录及PTA实验总结

题目集:jmu-Java-05-集合

3.1. 码云代码提交记录

在码云的项目中,依次选择“统计-Commits历史-设置时间段”, 然后搜索并截图

今天提交的并未显示,不止为何

3.2. PTA实验

函数(4-1),编程(5-3,5-4,5-5)

实验总结已经在作业中体现,不用写。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,086
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,561
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,410
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,183
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,820
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,903