Java有关集合的练习

1.某中学有若干学生(学生对象放在一个List中),每个学生有一个姓名属性(String)、班级名称属性(String)和考试成绩属性(double)
某次考试结束后,每个学生都获得了一个考试成绩。遍历list集合,并把学生对象的属性打印出来。

package lianxi01;

public class Student {

private String name;
private String className;
private double score;
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 getScore() {
    return score;
}
public void setScore(double score) {
    this.score = score;
}
public Student(String name, String className, double score) {
    super();
    this.name = name;
    this.className = className;
    this.score = score;
}
public Student() {
    super();
    // TODO Auto-generated constructor stub
}
@Override
public String toString() {
    return "姓名:" + name + ", 班级:" + className + ", 成绩:" + score ;
}

}

package lianxi01;

import java.util.ArrayList;
import java.util.List;

public class Test {

public static void main(String[] args) {

    List list = new ArrayList();
    list.add(new Student("tom","2年级3班",90));
    list.add(new Student("jack","2年级3班",80));
    list.add(new Student("Lily","2年级3班",70));
    list.add(new Student("Lucy","2年级3班",60));

    for (Student student : list) {
        System.out.println(student);
    }

}

}

2.编写程序,获取命令行参数中的字符串列表,输出其中重复的字符、不重复的字符以及消除重复以后的字符列表。

package test;

import java.util.HashSet;
import java.util.Set;

public class Lianxi02 {

public static void main(String[] args) {
    String str = "abcdeafblmbnopawc";
    System.out.println("原字符串:"+str);


    Set set1 = new HashSet();//消除重复后的字符
    Set set2 = new HashSet();//重复的字符
    Set set3 = new HashSet();//不重复的字符

    //把字符串转为字符数组
    char[] cs = str.toCharArray();
    for (char c : cs) {
        boolean b = set1.add(c);
        if(!b){
            set2.add(c);
        }
    }

    //把消除重复后的字符赋给set3
    set3.addAll(set1);
    //把消除重复后的字符 - 重复的字符 = 不重复的字符
    set3.removeAll(set2);

    System.out.println("====消除重复后的字符========");
    for (char c : set1) {
        System.out.print(c+" ");
    }

    System.out.println("\n====重复的字符========");
    for (char c : set2) {
        System.out.print(c+" ");
    }

    System.out.println("\n====不重复的字符========");
    for (char c : set3) {
        System.out.print(c+" ");
    }


}

}

3.使用Scanner从控制台读取一个字符串,统计字符串中每个字符出现的次数,要求使用学习过的知识完成以上要求
实现思路根据Set、List、Map集合的特性完成。

package test;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Lianxi03 {

public static void main(String[] args) {
    String str = "abcdeblmbac";
    System.out.println("原字符串:"+str);

    Map map = new HashMap();
    char[] cs = str.toCharArray();

    for (char c : cs) {

        if(map.containsKey(c)){
            Integer value = map.get(c);
            value++;

            map.put(c, value);

        }else{
            map.put(c, 1);
        }   
    }
    //遍历map
    Set set = map.keySet();
    for (Character c : set) {
        System.out.println(c+"出现了"+map.get(c)+"次");
    }
}

}

4.定义一个Employee类,属性:name:String,age:int,salary:double
把若干Employee对象放在List中,排序并遍历输出,排序规则:salary高的在前面,salary相同时age大的在前面,age也相同时按照name升序排列
把若干Employee对象放在Set中并遍历,要求没有重复元素

package lianxi04;

public class Employee {

private String name;
private int age;
private double salary;
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public int getAge() {
    return age;
}
public void setAge(int age) {
    this.age = age;
}
public double getSalary() {
    return salary;
}
public void setSalary(double salary) {
    this.salary = salary;
}
public Employee() {
    super();
    // TODO Auto-generated constructor stub
}
public Employee(String name, int age, double salary) {
    super();
    this.name = name;
    this.age = age;
    this.salary = salary;
}
@Override
public String toString() {
    return "姓名:" + name + ", 年龄:" + age + ", 工资:" + salary ;
}

}

package lianxi04;

import java.util.ArrayList;
import java.util.List;

public class Test {

public static void main(String[] args) {

    Employee e1 = new Employee("Alis",20,3000);
    Employee e2 = new Employee("Tom",22,3000);
    Employee e3 = new Employee("Jack",22,3000);
    Employee e4 = new Employee("Lily",21,3500);
    Employee e5 = new Employee("Mike",20,2900);
    Employee e6 = new Employee("Bobo",23,4000);

    List list = new ArrayList();
    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(); 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);
    }

}

}

你可能感兴趣的:(Java)