标签:master 查找 名称 index 代码 col new t search git
在数据结构和算法中,排序是很重要的操作,要让一个类可以进行排序,有两种方法:
class 名称<泛型列表>
LinkedList<String> mylist=new LinkedList<String>();
list.add(E obj);
list.remove(index)
public static sort(List<E>list)
int binarySearch(List<T>,Tkey,compareTo<T>c)
TreeMap<StudentKey,Student> treemap= new TreeMap<StudentKey,Student>()
习题2:
成绩排序已提交,学号排序忘记传了:
习题3:
import java.util.*;
class Stu implements Comparable{
int id;
String name;
Stu(String n, int i){
name=n;
id=i;
}
public int compareTo(Object b){
Stu st=(Stu)b;
return (this.id-st.id);
}
}
public class MyList {
public static void main(String [] args) {
//选用合适的构造方法,用你学号前后各两名同学的学号创建四个结点
LinkedList<Stu> list=new LinkedList<>();
list.add(new Stu("其米仁增",5239));
list.add(new Stu("李梓豪",5201));
list.add(new Stu("夏云霄",5203));
list.add(new Stu("贾普涵",5204));
//把上面四个节点连成一个没有头结点的单链表
Iterator<Stu> iter=list.iterator();
//遍历单链表,打印每个结点的
System.out.println("初始单链表为:");
while (iter.hasNext()){
Stu st=iter.next();
System.out.println(st.id+" "+st.name);
}
//把你自己插入到合适的位置(学号升序)
list.add(new Stu("贾海粟",5202));
Collections.sort(list);
//遍历单链表,打印每个结点的
iter=list.iterator();
System.out.println("插入我的学号和姓名后单链表为:");
while (iter.hasNext()){
Stu st=iter.next();
System.out.println(st.id+" "+st.name);
}
//从链表中删除自己
list.remove(1);
iter=list.iterator();
//遍历单链表,打印每个结点的
System.out.println("删除我的学号和姓名后单链表为:");
while (iter.hasNext()){
Stu st=iter.next();
System.out.println(st.id+" "+st.name);
}
}
}
1.使用堆栈结构输出an的若干项,其中an=2an-1+2an-2,a1=3,a2=8
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(temp);
stack.push(F2);
k++;
}
}
}
}
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 E2 {
public static void main(String args[]) {
List<Student> list=new LinkedList<Student>();
int score []={100,76,45,99,77,88,75,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);
}
}
}
3.有10个U盘,有两个重要的属性:价格和容量,编写一个应用程序,使用TreeMap<K,V>
类,分别按照价格和容量排序输出10个U盘的详细信息。
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[]={2,4,8,16,32};
double price[]={32,50,64,128};
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+"元");
}
}
}
4.编程题目码云链接
标签:master 查找 名称 index 代码 col new t search git
原文地址:https://www.cnblogs.com/jhs888/p/8997664.html