迭代器的經典代碼。。。
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+ |