答:
java
本次做业题集集合编程
答:ide
for (int i =list.size()-1 ; i >-1; i--) { if(list.get(i).equals(str)){ list.remove(i); } }
二、利用迭代器,先越过一个元素,判断该元素是否知足删除条件,是就将其删除,循环判断是否还有下一个元素。应先调用next,再调用remove。函数
for (Iterator<String> iterator = list.iterator(); iterator.hasNext();) { String num = (String) iterator.next(); if list.get(i).equals(str)) iterator.remove(); }
答:学习
创建 wordsMap集合 while(true){ 读取字符串到word count=wordMap.get(word);得到该单词出现的次数 if(word为“!!!!!”) 跳出当前读取单词的循环 else if(count不为null) wordMap.put(word,++count);//不是第一次出现的单词,次数在原来上加一 else wordMap.put(word,1);//第一次出现的单词,次数置为1 } 打印wordMap的长度,得到单词总数。 List<Map.Entry<String,Integer>> list;//取wordsMap中的键值对放入list中。 Collections.sort(list) {//键值对比较排序 compare(o1.o2) if(o1.value==o2.value) 返回o1与o2的key比较结果 else 返回o1和o2的value比较结果 } i=0; 循环输出前10个单词和对应次数,用i>9作标记。 System.out.println(list);
答:
一、使用HashMap集合,Key放单词(String),value放次数(Integer)
二、循环得到单词和出现次数。单词第一次出现value置为1,不然就在原有的value值上加一。测试
Integer count = words.get(word); if(count!=null){ count++;words.put(word, count); } else{ words.put(word, 1); }
三、取wordsMap键值对放入list.使用Map.Entry<K,V>
四、比较键值对的value,相同就比较key.Collections.sort方法使用内部类从新定义compare方法设计
Collections.sort(list,new Comparator<Map.Entry<String, Integer>>() { @Override public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) { if(o1.getValue()==o2.getValue()) return o1.getKey().compareTo(o2.getKey()); return o2.getValue()-o1.getValue(); } });
本题较难,作不出来没关系。但必定要有本身的思考过程,要有提交结果。3d
答:code
答:对象
int count=0;//标记行数 while(true){ 读取一行到strline; count++; if(strline为"!!!!!") 跳出读取单词的循环 esle 新建Scanner,从strline读取 Map linemap.put(Integer,String);//记录每行的内容 while(strScanner.hasnext()){ 读取strSacnner.next()到word中 if 集合中已经存在该单词,则将count直接加到单词对应的行数表中 Map wordMap.put(word,List<Integer>);//记录单词和单词出现的行数集合 else 集合不存在该单词 新建List,将当前行数add进表中,再wordMap.put(word,list); } for(Map.Entry<String,List<Integer>> e:wordMap.entrySet())//循环打印索引 println(e); while get keyString;//得到查询关键字 for(Map.Entry<String,List<Integer>> e:linemap.entrySet())//在行集合的字符串中 判断是否包含该关键字 if (e.getvalue.comtains(keyString)==true) println(e.getkey+e.getvalue); else println("found 0 results");
答:这题作得时间较长,但收获也不少。首先使用两个Map集合,wordMap存放单词和单词出现的行数的集(List),linemap存放行数和每一行的内容。将所需元素循环存入两个Map中。利用Map.Entry遍历输出wordMap,完成索引的打印。读取关键字,在linemap中操做,循环判断每一行的value中是否有包含关键字,是则输出该行的Key和value.循环结束仍没有改关键字,就输出found o result.以上是个人总体思路。
编写一个Student类,属性为:
private Long id; private String name; private int age; private Gender gender;//枚举类型 private boolean joinsACM; //是否参加过ACM比赛
建立一集合对象,如List
答:搜索方法以下:
运行结果以下:
4.2 使用java8中的stream(), filter(), collect()编写功能同4.1的代码,并测试(要出现测试数据)。构建测试集合的时候,除了正常的Student对象,再往集合中添加一些null,你编写的方法应该能处理这些null而不是抛出异常。(截图:出现学号)
答:
题集jmu-Java-05-集合之GeneralStack
答:接口代码:
public interface GeneralStack<E> { E push(E item); //如item为null,则不入栈直接返回null。 E pop(); //出栈,如为栈为空,则返回null。 E peek(); //得到栈顶元素,如为空,则返回null. public boolean empty();//如为空返回true public int size(); //返回栈中元素数量 }
答:在ArrayListIntegerStack中栈存放的元素类型必须明确指明,一旦指明,就没法随意更改。使用泛型,是使用字母E、T、K等做为类型代号来声明对象的类型。当接口使用泛型时,会极大遍历接口的操做,减小不一样类型的相同代码,例如将GeneralStack
public class ArrayListGeneralStack
题目集:jmu-Java-05-集合
在码云的项目中,依次选择“统计-Commits历史-设置时间段”, 而后搜索并截图
须要有两张图(1. 排名图。2.PTA提交列表图)
须要将每周的代码统计状况融合到一张表中。
周次 | 总代码量 | 新增代码量 | 总文件数 | 新增文件数 |
---|---|---|---|---|
1 | 0 | 0 | 0 | 0 |
2 | 0 | 0 | 0 | 0 |
3 | 0 | 0 | 0 | 0 |
4 | 0 | 0 | 0 | 0 |
5 | 739 | 739 | 16 | 16 |
6 | 1084 | 345 | 28 | 12 |
7 | 1180 | 96 | 30 | 2 |
8 | 1627 | 447 | 35 | 5 |
9 | 1986 | 359 | 44 | 9 |
10 | 2350 | 364 | 56 | 12 |
尝试从如下几个维度评估本身对Java的理解程度
维度 | 程度 |
---|---|
语法 | 基本经常使用语法能够熟练运用,PTA的题目部分能够本身解决,部分要多多请教别人 |
面向对象设计能力 | 面向对象编程学习中,使用面向对象思想为所要解决比较简单的问题建模应该是能够的 |
应用能力 | 感受基础仍是没打实,基本没有什么应用能力 |
至今为止代码行数 | 2350 |