1.某中学有若干学生(学生对象放在一个List中),每个学生有一个姓名属性(String)、班级名称属性(String)和考试成绩属性(double)
某次考试结束后,每个学生都获得了一个考试成绩。遍历list集合,并把学生对象的属性打印出来
方法一:
Student类:
package com.oralce.work001;
public class Student {
public String name;
public String classname;
public double grade;
public Student(String name,String classname,double grade){
this.name = name;
this.classname = classname;
this.grade = grade;
}
}
测试类:
package com.oralce.work001;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class demo {
public static void main(String[] args) {
Student s = new Student("Tom","163计应大数据",79);
List
l.add(new Student("Jack","163计应大数据",88));
l.add(new Student("Mack","163计应大数据",99));
l.add(new Student("Lily","163计应大数据",90));
l.add(new Student("Ray","163计应大数据",78));
l.add(new Student("Killy","163计应大数据",98));
//遍历一
//for (Student student : l) {
// System.out.println("姓名:"+student.name+" 班级:"+student.classname+" 成绩:"+student.grade);
//}
//遍历二:
Iterator
while(it.hasNext()){
Student student = it.next();
System.out.println("姓名:"+student.name+" 班级:"+student.classname+" 成绩:"+student.grade);
}
}
}
方法二:
Student类:
package com.oralce.work011;
public class Student {
private String name;
private String classname;
private double grade;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClassname() {
return classname;
}
public void setClassname(String classname) {
this.classname = classname;
}
public double getGrade() {
return grade;
}
public void setGrade(double grade) {
this.grade = grade;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String name, String classname, double grade) {
super();
this.name = name;
this.classname = classname;
this.grade = grade;
}
@Override
public String toString() {
return "学生姓名:" + name + ", 班级:" + classname + ", 成绩:" + grade;
}
}
测试类:
package com.oralce.work011;
import java.util.ArrayList;
import java.util.List;
public class T {
public static void main(String[] args) {
List
list.add(new Student("tom","163时尚旅游怀",99));
list.add(new Student("jack","163时尚旅游怀",88));
list.add(new Student("killy","163时尚旅游怀",77));
list.add(new Student("roy","163时尚旅游怀",66));
list.add(new Student("lily","163时尚旅游怀",67));
for (Student student : list) {
System.out.println(student);
}
}
}
2.编写程序,获取命令行参数中的字符串列表,输出其中重复的字符、不重复的字符以及消除重复以后的字符列表。
package com.oralce.work002;
3.使用Scanner从控制台读取一个字符串,统计字符串中每个字符出现的次数,要求使用学习过的知识完成以上要求
实现思路根据Set、List、Map集合的特性完成。
package com.oralce.work003;
4.定义一个Employee类,属性:name:String,age:int,salary:double
把若干Employee对象放在List中,排序并遍历输出,排序规则:salary高的在前面,salary相同时age大的在前面,age也相同时按照name升序排列
把若干Employee对象放在Set中并遍历,要求没有重复元素
Employee类:
测试类:
package com.oralce.work004;
import java.util.ArrayList;
import java.util.List;
public class test {
public static void main(String[] args) {
Employee e1 = new Employee("maik",20,3000);
Employee e2 = new Employee("tom",20,3000);
Employee e3 = new Employee("roy",22,3000);
Employee e4 = new Employee("killy",25,3000);
Employee e5 = new Employee("wang",21,3000);
Employee e6 = new Employee("m",23,3000);
List
list.add(e1);
list.add(e2);
list.add(e3);
list.add(e4);
list.add(e5);
list.add(e6);
for (Employee e : list) {
System.out.println(e);
}
System.out.println("=========排序后========");
for (int i = 1; i < list.size()-1; i++) {
for (int j = 0; j < list.size()-i; j++) {
Employee emp1 = list.get(j);
Employee emp2 = list.get(j+1);
//比较收入
if(emp1.getSalary() < emp2.getSalary()){
list.set(j, emp2);
list.set(j+1,emp1);
}else if(emp1.getSalary() == emp2.getSalary()){
if(emp1.getAge() < emp2.getAge()){
list.set(j, emp2);
list.set(j+1,emp1);
}
}else if(emp1.getAge() == emp2.getAge()){
if(emp1.getName().compareTo(emp2.getName()) > 0){
list.set(j, emp2);
list.set(j+1,emp1);
}
}
}
}
for (Employee e : list) {
System.out.println(e);
}
}
}