标签:题目 asn ber imp 信息 总成绩 int tree 应用
list.add("**");
Collections.sort();
lsit.remove("");
TreeMap<K,V>**适合用于数据的排序**
TreeMap<StudentKey,Student> treemap= new TreeMap<StudentKey,Student>();
创建链表
创建结点
插入自己的学号并排序
删除自己的学号并打印
整体代码
import java.util.*;
public class MyList {
public static void main(String [] args) {
List<String> mylist=new LinkedList<String>();
//选用合适的构造方法,用你学号前后各两名同学的学号创建四个结点
mylist.add("20165224");
mylist.add("20165225");
mylist.add("20165227");
mylist.add("20165228");
//把上面四个节点连成一个没有头结点的单链表
System.out.println("打印初始链表");
//遍历单链表,打印每个结点的
Iterator<String> iter=mylist.iterator();
while (iter.hasNext()){
String num=iter.next();
System.out.println(num);
}
//把你自己插入到合适的位置(学号升序)
mylist.add("20165226");
Collections.sort(mylist);
//遍历单链表,打印每个结点的
System.out.println("插入我的学号在排序之后,链表中的数据:");
iter =mylist.iterator();
while(iter.hasNext()){
String num=iter.next();
System.out.println(num);
}
//从链表中删除自己
mylist.remove("20165226");
System.out.println("删除我的学号之后打印链表:");
//遍历单链表,打印每个结点的
iter=mylist.iterator();
while(iter.hasNext()){
String num=iter.next();
System.out.println(num);
}
}
}
运行结果
创建Student
类的对象
运用TreeMap排序方法
按关键字总成绩
进行排序
按关键字学号
进行排序
运行结果
import java.util.*;
public class E {
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(temp);
stack.push(F2);
k++;
}
}
}
}
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 E {
public static void main(String args[]) {
List<Student> list=new LinkedList<Student>();
int score []={65,76,45,99,77,88,100,79};
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();
while(te.hasNext()) {
Student stu=te.next();
System.out.println(""+stu.name+" "+stu.english);
}
}
}
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 E {
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];
for(int k=0;k<UDisc.length;k++) {
UDisc[k]=new UDisc(amount[k],price[k]);
}
UDiscKey key[]=new UDiscKey[4];
for(int k=0;k<key.length;k++) {
key[k]=new UDiscKey(UDisc[k].amount); }
for(int k=0;k<UDisc.length;k++) {
treemap.put(key[k],UDisc[k]); }
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();
for(int k=0;k<key.length;k++) {
key[k]=new UDiscKey(UDisc[k].price); }
for(int k=0;k<UDisc.length;k++) {
treemap.put(key[k],UDisc[k]); }
number=treemap.size();
collection=treemap.values();
iter=collection.iterator();
while(iter.hasNext()) {
UDisc disc=iter.next();
System.out.println(""+disc.amount+"G "+disc.price+"元");
}
}
}
标签:题目 asn ber imp 信息 总成绩 int tree 应用
原文地址:https://www.cnblogs.com/musea/p/8993973.html