201521123075 《Java程序设计》第8周学习总结

1. 本周学习总结

2. 书面作业

本次作业题集集合

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

1.1 实验总结

进行删除操作的时候最好从末尾开始删除。如果从开头开始删除,会使每个元素的对应位置发生改变,代码量大。

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

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

while (sc.hasNext()) {
			String str = sc.next();
			if (str.equals("!!!!!")) break;
			Integer number = map.get(str);
			if (number == null){
				map.put(str, 1);
			}else {
				map.put(str, number + 1);
			}

List<Map.Entry<String, Integer>> entrylist = 
				new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
		Collections.sort(entrylist, new Comparator<Map.Entry<String, Integer>>() 

if (o1.getValue() == o2.getValue())
					return o1.getKey().compareTo(o2.getKey());
				return o2.getValue() - o1.getValue() ;

for (int i = 0; i < 10; i++) {
			System.out.println(entrylist.get(i));
		}
		sc.close();

2.2 实验总结

  • 对map进行排序前要将其转化成ArrayList,才能进行排序;
  • 对于按照字母排序降序,用Collection接口的排序方法会很方便。

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

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];
    }
    change array to list;
    if(!list.isEmpty)
        out(结果);

3.3 实验总结

  • 用TreeMap定义集合,key会直接在内部进行排序,
  • 用ArrayList要考虑是否有重复现象代码量太多。

4.Stream与Lambda

编写一个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中调用,然后输出结果。

输出结果:

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

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

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

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

5.2 GeneralStack接口的代码

interface GeneralStack<T>{
    public T push(T item);//如item为null,则不入栈直接返回null。如栈满,也返回null.
    public T pop();//出栈,如为空,则返回null.
    public T peek();//获得栈顶元素,如为空,则返回null.
    public boolean empty();//如为空返回true
    public int size();//返回栈中元素数量
}

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

答:使用泛型则不需要考虑参数的类型,可以在一个集合里面存放多种类型的数据,可以极大地提高代码复用率。

6.泛型方法

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

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

  • 代码及运行结果为:

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

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

3.1. 码云代码提交记录

posted on 2017-04-15 18:32  是噜噜啊  阅读(166)  评论(0编辑  收藏  举报

导航