第十周查漏补缺

    • 排序
      • 有类的源代码,针对某一成员变量排序,让类实现Comparable接口,调用Collection.sort(List)
      • 没有类的源代码,或者多种排序,新建一个类,实现Comparator接口 调用Collection.sort(List, Compatator)
    • 泛型类声明:
      class 名称<泛型列表>
    • 创建链表
      LinkedList<String> mylist=new LinkedList<String>();
    • 向链表增加节点
      list.add(E obj);
    • 从链表中删除节点
      list.remove(index)
    • 升序排序
      public static sort(List<E>list)
    • 折半查找list是否含有和参数key一样的元素
      int binarySearch(List<T>,Tkey,compareTo

第二题

import java.util.Comparator;

public class Student {
    private String id;//表示学号
    private String name;//表示姓名
    private int age;//表示年龄
    private char sex;//表示性别
    private double computer_score;//表示计算机课程的成绩
    private double english_score;//表示英语课的成绩
    private double maths_score;//表示数学课的成绩
    private double total_score;// 表示总成绩
    private double ave_score; //表示平均成绩

    public String getName() {
        return name;
    }

    public Student(String id, String name ,double s) {
        this.id = id;
        this.name = name;
        this.total_score = s;
    }


    public String getId() {
        return id;
    }//获得当前对象的学号,

    public double getComputer_score() {
        return computer_score;
    }//获得当前对象的计算机课程成绩,

    public double getMaths_score() {
        return maths_score;
    }//获得当前对象的数学课程成绩,

    public double getEnglish_score() {
        return english_score;
    }//获得当前对象的英语课程成绩,

    public void setId(String id) {
        this.id = id;
    }// 设置当前对象的id值,

    public void setComputer_score(double computer_score) {
        this.computer_score = computer_score;
    }//设置当前对象的Computer_score值,

    public void setEnglish_score(double english_score) {
        this.english_score = english_score;
    }//设置当前对象的English_score值,

    public void setMaths_score(double maths_score) {
        this.maths_score = maths_score;
    }//设置当前对象的Maths_score值,

    public double getTotalScore() {
        return total_score;
    }// 计算Computer_score, Maths_score 和English_score 三门课的总成绩。

    public double getAveScore() {
        return getTotalScore() / 3;
    }// 计算Computer_score, Maths_score 和English_score 三门课的平均成绩。


    public int compareTo(Object o) {
        Student st = (Student)o;
        return (Integer.parseInt(this.id)-Integer.parseInt(st.id));
    }
}
public class Node<T>                             //单链表结点类,T指定结点的元素类型
{
    public T data;                               //数据域,存储数据元素
    public Node<T> next;                         //地址域,引用后继结点

    public Node(T data, Node<T> next)            //构造结点,data指定数据元素,next指定后继结点
    {
        this.data = data;                        //T对象引用赋值
        this.next = next;                        //Node<T>对象引用赋值
    }
    public Node()
    {
        this(null, null);
    }
    @Override
    public String toString()                     //返回结点数据域的描述字符串
    {
        return this.data.toString();
    }
}
import java.util.Comparator;
public class scoreComparator implements Comparator {
    @Override
    public int compare(Object o1, Object o2) {
        Student st1 = (Student)o1;
        Student st2 = (Student)o2;
        return (int) (st1.getTotalScore()-st2.getTotalScore());
    }
}
import junit.framework.TestCase;
import org.junit.Test;

import java.util.LinkedList;
import java.util.List;
import java.util.*;

public class TestStudent extends TestCase {
    public static void main(String[] args) {
        List<Student> list = new LinkedList<Student>();
        list.add(new Student("20165309", "a",580.0));
        list.add(new Student("20165310", "b",640.0));
        list.add(new Student("20165311", "李嘉昕",620.0));
        list.add(new Student("20165312", "c",600.0));
        list.add(new Student("20165313", "d",570.0));
        Iterator<Student> iter = list.iterator();
        System.out.println("排序前,链表中的数据");
        Collections.shuffle(list);
        while (iter.hasNext()) {
            Student stu = iter.next();
            System.out.println(stu.getId() + " " + stu.getName()+ " "+stu.getTotalScore());
        }
        Collections.sort(list,new scoreComparator());
        System.out.println("排序后,链表中的数据");
        iter = list.iterator();
        while (iter.hasNext()) {
            Student stu = iter.next();
            System.out.println(stu.getId() + " " + stu.getName()+ " "+stu.getTotalScore());
        }
    }
    

}

实验结果截图

第三题

import java.util.Iterator;

public class MyList {
    public static void main(String[] args) {
        //选用合适的构造方法,用你学号前后各两名同学的学号创建四个结点
        Node<Integer> S1 = new Node<Integer>(20165309, null);
        Node<Integer> S2 = new Node<Integer>(20165310, null);
        Node<Integer> S3 = new Node<Integer>(20165312, null);
        Node<Integer> S4 = new Node<Integer>(20165313, null);
        //把上面四个节点连成一个没有头结点的单链表
        S1.next = S2;
        S2.next = S3;
        S3.next = S4;
        //遍历单链表,打印每个结点的
        Node<Integer> s = S1;
        while (s != null) {
            System.out.println(s.data);
            s = s.next;
        }
        System.out.println();
        //把你自己插入到合适的位置(学号升序)
        Node<Integer> M = new Node<Integer>(20165311, null);
        s = S1;
        while (s != null) {
            if (s.data < 20165311 && s.next.data > 20165311) {
                M.next = s.next;
                s.next = M;
                break;
            }
            else {
                s = s.next;
            }
        }
        System.out.println();
        //遍历单链表,打印每个结点的
        s = S1;
        while (s != null) {
            System.out.println(s.data);
            s = s.next;
        }
        System.out.println();
        //从链表中删除自己
        s = S1;
        while (s != null) {
            if (s.next.data == 20165311) {
                s.next = s.next.next;
                break;
            }
            else {
                s = s.next;
            }
        }
        System.out.println();
        //遍历单链表,打印每个结点的
        s = S1;
        while (s != null) {
            System.out.println(s.data);
            s = s.next;
        }
    }
}
public class Node<T>                             //单链表结点类,T指定结点的元素类型
{
    public T data;                               //数据域,存储数据元素
    public Node<T> next;                         //地址域,引用后继结点

    public Node(T data, Node<T> next)            //构造结点,data指定数据元素,next指定后继结点
    {
        this.data = data;                        //T对象引用赋值
        this.next = next;                        //Node<T>对象引用赋值
    }
    public Node()
    {
        this(null, null);
    }
    @Override
    public String toString()                     //返回结点数据域的描述字符串
    {
        return this.data.toString();
    }
}

运行结果截图

第十五章课后编程题

(1)使用堆栈结构输出an的若干项

import java.util.*;
public class E1 {
    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<Integer>();
        stack.push(new Integer(3));
        stack.push(new Integer(8));
        int k = 1;
        while(k<=10){
            for(int i=1;i<=2;i++){
                Integer F1 = stack.pop();
                int f1 = F1.intValue();
                Integer F2 = stack.pop();
                int f2 = F2.intValue();
                Integer temp = new Integer(2*f1+2*f2);
                System.out.println(""+temp.toString());
                stack.push(F2);
                k++;
            }
        }
    }
}

截图

(2)编写一个程序,将链表中的学生英语成绩单存放到一个树集中,使得按成绩自动排序,并输出排序结果.

import java.util.*;
class CollegeStu implements Comparable {
    int english=0;
    String name;
    CollegeStu(int english,String name) {
        this.name=name;
        this.english=english;
    }
    @Override
    public int compareTo(Object b) {
        CollegeStu stu=(CollegeStu)b;
        return (this.english-stu.english);
    }
}
public class T2 {
    public static void main(String[] args) {
        List<CollegeStu> list=new LinkedList<CollegeStu>();
        int score []={ 90, 88, 80};
        String name []={"张三","李四","王五"};
        for (int i=0;i<score.length;i++) {
            list.add(new CollegeStu(score[i],name[i]));
        }
        Iterator<CollegeStu> iter=list.iterator();
        TreeSet<CollegeStu> mytree=new TreeSet<CollegeStu>();
        while (iter.hasNext()) {
            CollegeStu stu=iter.next();
            mytree.add(stu);
        }
        Iterator<CollegeStu> te=mytree.iterator();
        while (te.hasNext()) {
            CollegeStu stu=te.next();
            System.out.println(""+stu.name+" "+stu.english);
        }
    }
}

截图

(3)有10个U盘,有两个重要属性:价格和容量。编写一个应用程序,使用TreeMap

import java.util.*;
class UDiscKey implements Comparable{
    double key = 0;
    UDiscKey(double d){
        key = d;
    }
    public int compareTo(Object b){
        UDiscKey disc = (UDiscKey)b;
        if((this.key-disc.key)==0)
            return -1;
        else
            return (int)((this.key-disc.key)*1000);
    }
}
class UDisc{
    int amount;
    double price;
    UDisc(int m,double e){
        amount = m;
        price = e;
    }
}
public class E3 {
    public static void main(String[] args) {
        TreeMap<UDiscKey,UDisc> treeMap = new TreeMap<UDiscKey,UDisc>();
        int amount[] = {1,2,3,4,8,16,32,64,128,10};
        double price[] = {10,20,30,40,70,60,70,100,90,80};
        UDisc UDisc[] = new UDisc[10];
        for(int i=0;i<UDisc.length;i++){
            UDisc[i]= new UDisc(amount[i],price[i]);
        }
        System.out.println("按容量排序如下:");
        UDiscKey key[]= new UDiscKey[10];
        for(int i=0;i<key.length;i++){
            key[i] = new UDiscKey(UDisc[i].amount);
        }
        for(int i=0;i<UDisc.length;i++){
            treeMap.put(key[i],UDisc[i]);
        }
        int number = treeMap.size();
        Collection<UDisc> collection = treeMap.values();
        Iterator<UDisc> iter = collection.iterator();
        while(iter.hasNext()){
            UDisc disc = iter.next();
            System.out.println(""+disc.amount+"G "+disc.price+"元");
        }
        treeMap.clear();
        System.out.println("按价格排序如下:");
        for(int i=0;i<key.length;i++){
            key[i] = new UDiscKey(UDisc[i].price);
        }
        for(int i=0;i<UDisc.length;i++){
            treeMap.put(key[i],UDisc[i]);
        }
        number = treeMap.size();
        collection = treeMap.values();
        iter = collection.iterator();
        while (iter.hasNext()){
            UDisc disc = iter.next();
            System.out.println(""+disc.amount+"G "+disc.price+"元");
        }
    }
}

截图

 

猜你喜欢

转载自www.cnblogs.com/fakerli/p/9000258.html