Java集合框架上机练习题

1 编写一个Book类,该类至少有name和price两个属性。该类要实现Comparable接口,在接口的compareTo()方法中规定两个Book类实例的大小关系为二者的price属性的大小关系。在主函数中,选择合适的集合类型存放Book类的若干个对象,然后创建一个新的Book类的对象,并检查该对象与集合中的哪些对象相等。查询结果如下图:


  public Book(String name, double price) {
        this.name = name;
        this.price = price;
    }

    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        List<Book> booklist = new ArrayList<Book>();
        Book b1 = new Book("java基础教程", 30.0);
        Book b2 = new Book("C++基础教程", 28.0);
        Book b3 = new Book("数据库技术1", 29.0);
        Book b4 = new Book("数据库技术2", 29.0);
        Book b5 = new Book("数据库技术3", 29.0);
        booklist.add(b1);
        booklist.add(b2);
        booklist.add(b3);
        booklist.add(b4);
        booklist.add(b5);
        Book b6 = new Book("模式识别", 29.0);
//      Collections.sort(booklist);
//      Iterator<Book> iter=booklist.iterator();
//      while(iter.hasNext()) {
//          Book book=iter.next();
//          System.out.println(book.name+" : "+book.price);
//      }
//      int index=Collections.binarySearch(booklist, b4,null);
//      System.out.println(index);
//      if(index>0) {
//          System.out.println();
//      }
        System.out.println("新书《" + b6.name + "》与下列图书:");
        for (int i = 0; i < booklist.size(); i++) {
            Book book = (Book) booklist.get(i);
            int index = book.compareTo(b4);
            if (index == 0) {
                System.out.print("《"+book.name);
                System.out.println("》");
            }
        }
        System.out.print("相同" + "具体价格为" + b4.price + "元");
    }

    public int compareTo(Object obj) {
        Book p = (Book) obj;
        return (int) (p.price-this.price);
    }

}




2 编写一个应用程序,用户分别从两个文本框输入学术的姓名和分数,程序按成绩排序将这些学生的姓名和分数显示在一个文本区中。


package test;

import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class ScoreOrder extends JFrame implements ActionListener {
    JFrame jframe;
    JTextField jtextfield1;
    JTextField jtextfield2;
    JTextArea jtextarea;
    List<Student> list = new ArrayList<Student>();

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ScoreOrder myJframe = new ScoreOrder();// 显示窗口
    }

    ScoreOrder() {
        jframe = new JFrame("成绩排序");
        jframe.setBounds(200, 100, 700, 400);
        jframe.setLayout(new FlowLayout());
        jtextfield1 = new JTextField(10);
        jtextfield2 = new JTextField(10);
        JButton jbutton = new JButton("插入");
        Box box = Box.createHorizontalBox();
        box.add(new Label("姓名:"));
        box.add(jtextfield1);
        box.add(new Label("分数:"));
        box.add(jtextfield2);
        box.add(Box.createHorizontalStrut(50));
        box.add(jbutton);
        jframe.add(box);
        jtextarea = new JTextArea(40, 80);
        jframe.add(jtextarea);
        jframe.setVisible(true);
        jbutton.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        jtextarea.setText("");
        String name = jtextfield1.getText();
        String grade = jtextfield2.getText();
        if (!"".equals(name) && !"".equals(grade)) {
            list.add(new Student(name, Float.parseFloat(grade)));
            jtextarea.append("当前插入的信息为:姓名:" + name + "    成绩:" + grade + "\n");
        }
        Collections.sort(list);// 对list排序
        Iterator<Student> iterator = list.iterator();
        Student student;
        while (iterator.hasNext()) {
            student = iterator.next();
            jtextarea.append("name:" + student.name + "    grade:" + student.grade + "\n");
        }
    }
}

class Student implements Comparable<Student> {

    String name;
    float grade;

    public Student(String name, float grade) {
        super();
        this.name = name;
        this.grade = grade;
    }

    @Override
    public int compareTo(Student o) {
        // TODO Auto-generated method stub
        return (int) (o.grade - this.grade);
    }

}




已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页