第十周课堂测试补做
Posted cloud795
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第十周课堂测试补做相关的知识,希望对你有一定的参考价值。
相关知识点总结
泛型类声明:
class 名称<泛型列表>
创建链表
LinkedList<String> mylist=new LinkedList<String>();
向链表增加节点
list.add(E obj);
从链表中删除节点
list.remove(index)
- LinkedList
- LinkedList
- 链表对象可以使用iterator()方法获取一个Iterator对象,该对象就是针对当前链表的迭代器
- public static sort(List
int binarySearch(List
题目补做截图
第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 + "个数:\\n"+3+"\\n"+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+"?");
}
}
}
运行截图:
以上是关于第十周课堂测试补做的主要内容,如果未能解决你的问题,请参考以下文章