第十周课堂测试补做

相关知识点总结

  • 泛型类声明:
class 名称<泛型列表>
  • 创建链表
LinkedList<String> mylist=new LinkedList<String>();
  • 向链表增加节点
list.add(E obj);
  • 从链表中删除节点
list.remove(index)
  • LinkedList泛型类实现Lis泛型接口中的一些常用方法
    public boolean add(E element) 向链表末尾添加一个新的节点,该节点中的数据是参数elememt指定的数据。
    public void add(int index ,E element) 向链表的指定位置添加一个新的节点,该节点中的数据是参数elememt指定的数据。
    public void clear() 删除链表的所有节点,使当前链表成为空链表。
    public E remove(int index) 删除指定位置上的节点。
    public boolean remove(E element) 删除首次出现含有数据elemen的节点。
    public E get(int index) 得到链表中指定位置处节点中的数据。
  • LinkedList泛型类本身新增加的一些常用方法
    public void addFirst(E element) 向链表的头添加新节点,该节点中的数据是参数elememt指定的数据。
    public void addLast(E element) 向链表的末尾添加新节点,该节点中的数据是参数elememt指定的数据。
    public E getFirst() 得到链表中第一个节点中的数据。
    public E getLast() 得到链表中最后一个节点中的数据。
    public E removeFirst() 删除第一个节点,并返回这个节点中的数据。
  • 链表对象可以使用iterator()方法获取一个Iterator对象,该对象就是针对当前链表的迭代器
  • public static sort(List list) 该方法可以将list中的元素升序排列。
  • int binarySearch(List list, T key,CompareTo c) 使用折半法查找list是否含有和参数key相等的元素

题目补做截图

第15章编程题

  • 1
import java.util.*;
public class StackPrint {
    public static void main(String[] args) {
        Stack<Integer> numberline=new Stack<Integer>();
        int count = Integer.parseInt(args[0]);
        int temp,add1,add2;
        numberline.push(3);
        numberline.push(8);
        System.out.println("输出这个系列的前" + count + "个数:
"+3+"
"+8);
        for (int i = 0; i < count; i++) {
                add2 = numberline.pop();
                add1 = numberline.pop();
                temp = 2*(add1 + add2);
                numberline.push(add1);
                numberline.push(add2);
                numberline.push(temp);
                System.out.println(""+temp);
            }
    }
}

运行截图:

  • 2
import java.util.*;
class Student implements Comparable {
    int english=0;
    String name;
    Student(int english,String name) {
        this.name=name;
        this.english=english;
    }
    public int compareTo(Object b) {
        Student st=(Student)b;
        return (this.english-st.english);
    }
}
public class EnglishGrades {
    public static void main(String args[]) {
        List<Student> list=new LinkedList<Student>();
        int score []={50,94,37,79,84};
        String name[]={"苏祚堃","朱越","张三","李四","王五"};
        for(int i=0;i<score.length;i++){
            list.add(new Student(score[i],name[i]));
        }
        Iterator<Student> iter=list.iterator();
        TreeSet<Student> mytree=new TreeSet<Student>();
        while(iter.hasNext()){
            Student stu=iter.next();
            mytree.add(stu);
        }
        Iterator<Student> te=mytree.iterator();
        System.out.println("按成绩从小往大排:");
        while(te.hasNext()) {
            Student stu=te.next();
            System.out.println(""+stu.name+" "+stu.english);
        }
    }
}

运行截图:

  • 3
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 usbInformation {
    public static void main(String args[ ]) {
        TreeMap<UDiscKey,UDisc>  treemap= new TreeMap<UDiscKey,UDisc>();
        int amount[]={1,2,4,8,16};
        double price[]={867,266,390,556};
        UDisc UDisc[]=new UDisc[4];
        UDiscKey key[]=new UDiscKey[4] ;
        for(int k=0;k<UDisc.length;k++) {
            UDisc[k]=new UDisc(amount[k],price[k]);
            key[k]=new UDiscKey(UDisc[k].amount);
            treemap.put(key[k],UDisc[k]);
        }
        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+"Ԫ");
        }
    }
}

运行截图:

原文地址:https://www.cnblogs.com/cloud795/p/8995007.html