迭代器的经典代码。。。
javascript
本次做业题集集合java
if(list.get(i).equals(str)) list.remove(i);
实如今list中移除掉与str内容相同的元素。
列举:a.利用Iterator遍历删除符合条件的元素。b.利用for循环遍历删除符合条件的元素。
参考网址:http://www.jb51.net/article/98763.htm正则表达式
Map<String,Integer>map = new HashMap<String,Integer>() while(系统有输入){ String str; if(str等于!!!!!) 退出; else map.put(str,1) else map.put(str,map.get(str)+1) } List<Entry<String,Integer>> list =new ArrayList<Entry<String,Integer>>(map.entrySet()); Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() { 比较 getValue if equals 比较 Key }); println(list)
本题较难,作不出来没关系。但必定要有本身的思考过程,要有提交结果。数组
if (行中有关键字) 在关键字集合中加入行号 for (每次搜索) { if (表 == 0) System.out.println("found 0 results"); else { System.out.println(行);
本题使用Map<String, ArrayList
编写一个Student类,属性为:学习
private Long id;
private String name;
private int age;
private Gender gender;//枚举类型
private boolean joinsACM; //是否参加过ACM比赛
建立一集合对象,如List
public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Student[] students = new Student[5]; students[0] = new Student(9l, "zhang", 21, Gender.male, true); students[1] = new Student(15l, "chen", 21, Gender.female, true); students[2] = new Student(67l, "chen", 19, Gender.male, true); students[3] = new Student(78l, "li", 20, Gender.female, true); students[4] = new Student(69l, "zhong", 19, Gender.male, true); List<Student> list2 = new ArrayList<Student>(); list2.add(students[0]); list2.add(students[1]); list2.add(students[2]); list2.add(students[3]); list2.add(students[4]); List<Student> list3 = search(list2,5l,"zhang",20,Gender.male, true); for (int i = 0; i < list3.size(); i++) { System.out.println(list3.get(i).toString()); } System.out.println("陈锦霞201621123061"); } public static List<Student> search(List<Student> stuList, Long id, String name, int age, Gender gender, boolean joinsACM) {// 搜索方法(函数) List<Student> list1 = new ArrayList<Student>(); for (Student student : stuList) { if (student.getId() > id && student.getName().equals( name) && student.getAge() > age && student.getGender().equals(gender) && student.isJoinsACM() == true) { list1.add(student); } } return list1; } }//函数在Main中,而不在main中
ArrayList<Student> list3 =(ArrayList<Student>)list2.parallelStream().filter(student->student!=null&&(student.getId() > 5l && student.getName().equals( "zhang") && student.getAge() > 20 && student.getGender().equals(Gender.male) && student.isJoinsACM() == true)).collect(Collectors.toList()); for (int i = 0; i < list3.size(); i++) { System.out.println(list3.get(i).toString()); } System.out.println("陈锦霞201621123061"); }
题集jmu-Java-05-集合之GeneralStack.net
interface GeneralStack<E> { E push(Object item); E pop(); E peek(); public boolean empty(); public int size(); }
之前做业的ArrayListIntegerStack原本是Integer栈,以后要把栈改为String栈,很麻烦。固然,也能够事先把类型所有定义为Object,以后再进行强制类型转换。可是 强制类型转换容易在运行的时候出现类型所引发的错误,并且不容易找,因此尽可能不用。在本题使用泛型,栈能够用来存储String,Integer,Car,这样能够更好地复用代码,并且在编译阶段就能够发现错误。设计
基础参考文件GenericMain,在此文件上进行修改。3d
public class Test { public static void main(String[] args) { List<String>strList=new ArrayList<String>(); List<Integer>intList=new ArrayList<Integer>(); strList.add("abcd"); strList.add("hello"); strList.add("world"); intList.add(1); intList.add(2); intList.add(3); String maxStr = max(strList); Integer maxInt = max(intList); System.out.println("MaxString= " + maxStr); System.out.println("MaxInteger= " + maxInt); } public static <T extends Comparable<T>> T max(List<T> list) { T max = list.get(0); for (T e : list) { if ( e.compareTo( max ) > 0 ){ max = e; } } return max; } }
public class Main2 { public static void main(String[] args) { List<StuUser>stuList=new ArrayList<StuUser>(); stuList.add(new StuUser(1,"abcd")); stuList.add(new StuUser(2,"hello")); stuList.add(new StuUser(3,"world")); User user = max1(stuList); System.out.println("MaxStuUser= " + user); Object user1 = max1(stuList); System.out.println("MaxStuUser= " + user1); } public static <T extends Comparable<? super T>> T max1(List<? extends T> list) { int i=0; for(;list.get(i)==null;) i++; T max1=list.get(0); for(T e:list){ if(e.compareTo(max1)>0) max1=e; if(e==null) continue; } return max1; } }
public class Main3 { public static void main(String[] args) { List<StuUser>stuList=new ArrayList<StuUser>(); StuUser stuuser1=new StuUser(1,"abcd"); StuUser stuuser2=new StuUser(2,"hello"); StuUser stuuser3=new StuUser(3,"world"); User user1 = new User(4); User user2= new User(5); System.out.println(myCompare(stuuser1,stuuser2,new StuUserComparator())); System.out.println(myCompare(stuuser2,stuuser3,new StuUserComparator())); System.out.println(myCompare(stuuser1,user1,new UserReverseComparator())); System.out.println(myCompare(user2,user1,new UserReverseComparator())); } public static <T> int myCompare (T o1, T o2, Comparator<T> c) { return c.compare(o1, o2); }
题目集:jmu-Java-05-集合
须要有两张图(1. 排名图。2.PTA提交列表图)
周次 | 行数 | 新增行数 | 文件数 | 新增文件数 |
---|---|---|---|---|
1 | 91 | 91 | 5 | 5 |
2 | 504 | 413 | 18 | 13 |
3 | 1092 | 588 | 28 | 10 |
5 | 1158 | 129 | 34 | 6 |
6 | 1539 | 381 | 40 | 6 |
7 | 2023 | 484 | 49 | 9 |
8 | 2477 | 454 | 57 | 8 |
9 | 2709 | 232 | 63 | 6 |
10 | 3156 | 447 | 70 | 7 |
尝试从如下几个维度评估本身对Java的理解程度
维度 | 程度 |
---|---|
语法 | pta上的题目还要问同窗,只有小部分能本身作出来 |
面向对象设计能力 | 不怎么会 |
应用能力 | 不怎么会,只写过一个加法器 |
至今为止代码行数 | 3000+ |