JavaEE-实验二 Java集合框架实验
该博客仅专为我的小伙伴提供参考而附加,没空加上代码具体解析,望各位谅解
1、 使用类String类的分割split 将字符串 “Solutions to selected exercises can be found in the electronic document The Thinking in Java Annotated Solution Guide, available for a small fee from BruceEckel” 单词提取输出。单词以空格或,分割。
package experiment; public class Split { public static void main(String[] args) { String str="Solutions to selected exercises can be found in the electronic document The Thinking in Java
Annotated Solution Guide, available for a small fee from BruceEckel"; String[] s = str.split(" "); for(String ss:s) { System.out.print(ss+","); } } }
示例截图
2、 设计一个程序计算2011-05-01日与系统当前日期相差的天数。
package experiment; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class days { public static void main(String[] args) throws ParseException { Date date1=new Date(); SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd"); String s=new String("2011-05-01"); Date date2 = format.parse(s); int days=(int)((date1.getTime()-date2.getTime())/(1000*60*60*24)); System.out.println("今天日期:"+format.format(date1)+"距离"+s+" "+days+"天"); } }
示例截图
3、将本学期开设的课程名称加入到HashSet中,并使用迭代器遍历输出。
package experiment; import java.util.HashSet; import java.util.Iterator; import java.util.Set; public class Courses { public static void main(String[] args) { Set<String> set=new HashSet<String>(); set.add("Python"); set.add("JavaEE"); set.add("软件工程"); set.add("操作系统"); set.add("计算机组成"); Iterator<String> it=set.iterator(); while(it.hasNext()) { System.out.println(it.next()); } } }
示例截图
4 理解元素是如何排序的。完成以下实验:
1)定义一个学生类:属性有学号、姓名、专业、高数成绩、外语成绩、Java课程成绩。
package experiment; public class Student { private String sno; private String sname; private String department; private int maths; private int english; private int java; private int score; public int getScore() { return score; } public Student(String sno, String sname, String department, int maths, int english, int java) { this.sno = sno; this.sname = sname; this.department = department; this.maths = maths; this.english = english; this.java = java; this.score=english+java+maths; } public String getSno() { return sno; } public void setSno(String sno) { this.sno = sno; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } public int getMaths() { return maths; } public void setMaths(int maths) { this.maths = maths; } public int getEnglish() { return english; } public void setEnglish(int english) { this.english = english; } public int getJava() { return java; } public void setJava(int java) { this.java = java; } @Override public String toString() { return "Student [sno=" + sno + ", sname=" + sname + ", department=" + department + ", maths=" + maths + ", english=" + english + ", java=" + java + ", 总分=" + score + "]"; } }
比较类
package experiment; import java.util.Comparator; public class MyCmp implements Comparator<Student>{ @Override public int compare(Student o1, Student o2) { return o2.getScore()-o1.getScore(); } }
2)在测试类中生成多个学生类的对象,放入TreeSet中,要求按照三门课总成绩从高到低排序,总成绩相等按学号排序。输出排序结果。
package experiment; import java.util.Iterator; import java.util.TreeSet; public class test { public static void main(String[] args) { Student s1=new Student("1", "c1", "计算机", 98, 80, 92); Student s2=new Student("2", "c2", "软工", 80, 84, 99); Student s3=new Student("3", "c3", "国交", 88, 79, 82); Student s4=new Student("4", "c4", "计算机", 78, 90, 82); TreeSet<Student> ts=new TreeSet<Student>(new MyCmp()); ts.add(s1); ts.add(s2); ts.add(s3); ts.add(s4); Iterator<Student> it=ts.iterator(); while(it.hasNext()){ Student s=(Student)it.next(); System.out.println(s.toString()); } } }
示例截图