本次做业题集集合java
实验总结:这题考到了关于List的删除,当从前面日后遍历的时候要考虑到删除了元素时后面的元素的下标都会前移一个位置。
在List中删除元素的方法:
删除大于等于5的数字:学习
//测试数据 List<Integer> list = new ArrayList<Integer>(); for(int i=0;i<10;i++) { list.add(i); }
//方法一 for(int i=0;i<list.size();i++) { if(list.get(i)>=5) { list.remove(list.get(i)); i--; } } System.out.println(list);
运行结果:
测试
//方法二 for(int i=list.size()-1;i>=0;i--) if(list.get(i)>=5) list.remove(list.get(i)); System.out.println(list);
运行结果:
设计
新建Map<String,Integer>对象map来存放单词及其出现次数 WHILE 取得的单词不为"!!!!!" IF 当前单词不存在map中 将单词添加到map中,设置value值为1 ELSE 将map中该单词的value值加1 新建List对象list,其中存放map 实现Comparator接口对其中数据进行排序 输出排序后数据
这题要实现题目规定的排序要使用ArrayList实现Comparator接口来实现排序,还要用到Map.Entry这个内部接口来遍历元素。3d
本题较难,作不出来没关系。但必定要有本身的思考过程,要有提交结果。code
建立Map(String,ArrayList<Integer>)对象map存放单词及其出现行数 建立ArrayList对象line存放每一行句子 WHILE 取得的单词不为"!!!!!" IF 当前单词不存在map中 添加单词到map中,并将当前行数添加进value ELSE 判断这个单词是否已经在该行出现 若没有出现,则添加该行进map的value 使用Iterator迭代器遍历map并输出 以空格为间隔输入若干个单词 判断单词是否都存在map的同一行中 若存在则输出行数及对应句子
实验总结:首先将建立ArrayList对象line存放每一行句子取得的单词不为"!!!!!", 添加单词到map中而后当前行数添加进value。再判断这个单词是否是已经在该行里出现,若是没有出现该行进map的value,使用Iterator迭代器遍历map而后输出 。
这个题挺难的都不怎麽懂都是找同窗交个人,虽然题是作出来的可是还没理解清楚。对象
编写一个Student类,属性为:
private Long id; private String name; private int age; private Gender gender;//枚举类型 private boolean joinsACM; //是否参加过ACM比赛
建立一集合对象,如List
测试数据:
List<Student> student = new ArrayList<Student>(); student.add(new Student(1L,"wang",15,Gender.man,false)); student.add( new Student(8L,"zhao",16,Gender.woman,true)); student.add( new Student(87L,"liao",17,Gender.man,false)); student.add ( new Student(100L,"xu",18,Gender.woman,false)); student.add( new Student(67L,"liao",19,Gender.man,true)); student.add( new Student(90L,"liao",20,Gender.man,true)); student.add( new Student(177L,"liao",21,Gender.man,true));
//search方法定义 static List<Student> search(List<Student> list,Long id, String name, int age, Gender gender, boolean joinsACM){ List<Student> student = new ArrayList<Student>(); for(Student e:list){ if(e.getId()>id&&e.getName().equals(name)&&e.getGender()==gender&&e.isJoinsACM()==joinsACM){ student.add(e); } } return student; }
运行结果:
排序
List<Student> newStudent = student.stream().filter(e -> e!=null&&e.getId()>50&&e.getName().equals("liao")&&e.getAge()>18&&e.getGender()==Gender.man&&e.isJoinsACM()==true).collect(Collectors.toList());
运行结果:
索引
题集jmu-Java-05-集合之GeneralStack
interface GeneralStack<T>{ T push(T item); T pop(); T peek(); public boolean empty(); public int size(); }
使用泛型可使咱们不止实现可以存放Integer类型的栈,还能实现存放Double、String的栈,而不用再去重写写接口。
题目集:jmu-Java-05-集合
在码云的项目中,依次选择“统计-Commits历史-设置时间段”, 而后搜索并截图
须要有两张图(1. 排名图。2.PTA提交列表图)
须要将每周的代码统计状况融合到一张表中。
本身的目标能实现吗?
还需努力
周次 | 总代码量 | 新增代码量 | 总文件数 | 新增文件数 |
---|---|---|---|---|
1 | 0 | 0 | 0 | 0 |
2 | 0 | 0 | 0 | 0 |
3 | 125 | 125 | 2 | 2 |
4 | 141 | 141 | 3 | 3 |
5 | 674 | 647 | 13 | 13 |
6 | 647 | 647 | 13 | 13 |
7 | 695 | 48 | 14 | 1 |
8 | 1867 | 1867 | 25 | 25 |
9 | 1974 | 107 | 29 | 4 |
10 | 2227 | 253 | 34 | 5 |
尝试从如下几个维度评估本身对Java的理解程度
维度 | 程度 |
---|---|
语法 | PTA的题目还得多学习, |
面向对象设计能力 | 都是问同窗百度学习在努力中 |
应用能力 | 可使用Java编写一些实用的小工 |
至今为止代码行数 | 2227 |