标签:第四次 汉语 char s 知识点 out 分享 pre 技术分享 位置
class 名称<泛型列表>
声明一个类,为了和普通的类有所区别,这样声明的类称作泛型类,没有指定E是何种类型的数据,它可以是任何对象或接口,但不能是基本类型数据LinkedList<E>
泛型类
iterator()
方法获取一个Iterator对象,该对象就是针对当前链表的迭代器Collections
类提供的用于排序和查找的类方法如下:
public static sort(List<E> list)
: 该方法可以将list中的元素升序排列int binarySearch(List<T> list, T key,CompareTo<T> c)
: 使用折半法查找list是否含有和参数key相等的元素,如果key链表中某个元素相等,方法返回和key相等的元素在链表中的索引位置(链表的索引位置从0考试),否则返回-1Collections
类还提供了将链表中的数据重新随机排列的类方法以及旋转链表中数据的类方法
public static void shuffle(List<E> list)
将list中的数据按洗牌算法重新随机排列static void rotate(List<E> list, int distance)
旋转链表中的数据public static void reverse(List<E> list)
翻转list中的数据TreeSet<E>
泛型类
TreeSet<String> mytree=new TreeSe<String>();
mytree.add("boy");
使用add方法为树集添加节点TreeSet
类的常用方法
public boolean add(E o)
向树集添加加节点public void clear()
删除树集中的所有节点public void contains(Object o)
如果树集中有包含参数指定的对象,该方法返回true,否则返回falsepublic E first()
返回树集中的第一个节点中的数据(最小的节点)public E last()
返回最后一个节点中的数据(最大的节点)public isEmpty()
判断是否是空树集,如果树集不含任何节点,该方法返回truepublic boolean remove(Object o)
删除树集中的存储参数指定的对象的最小节点public int size()
返回树集中节点的数目针对下面的Student类,使用Comparator编程完成以下功能:
- 在测试类StudentTest中新建学生列表,包括自己和学号前后各两名学生,共5名学生,给出运行结果(排序前,排序后)
- 对这5名同学分别用学号和总成绩进行增序排序,提交两个Comparator的代码
- 课下提交代码到码云
class Student {
private String id;//表示学号
private String name;//表示姓名
private int age;//表示年龄
private double computer_score;//表示计算机课程的成绩
private double english_score;//表示英语课的成绩
private double maths_score;//表示数学课的成绩
private double total_score;// 表示总成绩
private double ave_score; //表示平均成绩
public Student(String id, String name){
this.id = id;
this.name = name;
}
public Student(String id, String name, char sex, int age){
this(id, name);
this.sex = sex;
this.age = age;
}
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 computer_score+maths_score+english_score;
}// 计算Computer_score, Maths_score 和English_score 三门课的总成绩。
public double getAveScore(){
return getTotalScore()/3;
}// 计算Computer_score, Maths_score 和English_score 三门课的平均成绩。
}
class Undergraduate extends Student{
private String classID;
public Undergraduate(String id, String name, char sex, int age,String classID){
super(id,name,sex,age);
this.classID=classID;
}
public String getClassID(){
return classID;
}
public void setClassID(String classID){
this.classID=classID;
}
}
最开始使用的是书上例子9中的代码进行修改,得到满足老师运行结果要求的代码:
import java.sql.SQLOutput;
import java.util.*;
class StudentKey implements Comparable {
double d = 0;
String s = "";
StudentKey(double d) {
this.d = d;
}
StudentKey(String s) {
this.s = s;
}
public int compareTo(Object b) {
StudentKey st = (StudentKey) b;
if ((this.d - st.d) == 0)
return -1;
else
return (int) ((this.d - st.d) * 1000);
}
}
class StudentIF {
String name = null;
int ID ;
double math, english, computer, total, aver;
StudentIF(String n, int id,double m, double e, double f, double a, double b) {
name = n;
ID = id;
math = m;
english = e;
computer = f;
total = a;
aver = b;
}
}
public class Student {
public static void main(String args[]) {
TreeMap<StudentKey, StudentIF> treemap = new TreeMap<StudentKey, StudentIF>();
String name[] = {"张晨晖", "陈由章", "阎含", "孙勖哲", "宁心宇"};
int ID[] = {20165313,20165314,20165315,20165316,20165317};
double math[] = {66,77,88,99,100};
double english[] = {100,99,88,77,66};
double computer[] = {60,70,80,90,99};
double total[] = new double[5];
double aver[] = new double[5];
StudentIF student[] = new StudentIF[5];
for (int k = 0; k < student.length; k++) {
total[k] = math[k] + english[k] + computer[k];
aver[k] = total[k] / 3;
}
for (int k = 0; k < student.length; k++) {
student[k] = new StudentIF(name[k], ID[k] , math[k], english[k], computer[k], total[k], aver[k]);
}
StudentKey key[] = new StudentKey[5];
for (int k = 0; k < key.length; k++) {
key[k] = new StudentKey(student[k].total);
}
for (int k = 0; k < student.length; k++) {
treemap.put(key[k], student[k]);
}
int number = treemap.size();
System.out.println("树映射中有" + number + "个对象,按总成绩排序:");
Collection<StudentIF> collection = treemap.values();
Iterator<StudentIF> iter = collection.iterator();
while (iter.hasNext()) {
StudentIF stu = iter.next();
System.out.println("姓名 " + stu.name + " 学号 "+ stu.ID + " 总成绩 " + stu.total);
}
treemap.clear();
StudentKey key2[] = new StudentKey[5];
for (int k = 0; k < key2.length; k++) {
key2[k] = new StudentKey(student[k].ID);
}
for (int k = 0; k < student.length; k++) {
treemap.put(key2[k], student[k]);
}
System.out.println("树映射中有" + number + "个对象,按学号排序:");
Collection<StudentIF> collection2 = treemap.values();
Iterator<StudentIF> iter2 = collection2.iterator();
while (iter2.hasNext()) {
StudentIF stu2 = iter2.next();
System.out.println("姓名 " + stu2.name + " 学号 " + stu2.ID + " 总成绩 " + stu2.total);
}
}
}
运行截图:
虽然此方法可行,但是不满足老师给出的要求,即应该用Comparator
代码,我在课下补写了对应代码:
import java.util.*;
public class MyComparator {
public static void main(String[] args) {
List<StudentIF> list = new LinkedList<>();
StudentIF stu1=new StudentIF(20165313,"张晨晖","male",20,55,77,88);
StudentIF stu2=new StudentIF(20165314,"陈由章","male",20,88,100,66);
StudentIF stu3=new StudentIF(20165315,"阎 含","female",20,77,88,66);
StudentIF stu4=new StudentIF(20165316,"孙勖哲","male",20,99,77,88);
StudentIF stu5=new StudentIF(20165317,"宁心宇","male",20,66,99,77);
stu1.getAveScore();
stu2.getAveScore();
stu3.getAveScore();
stu4.getAveScore();
stu5.getAveScore();
list.add(stu1);
list.add(stu2);
list.add(stu3);
list.add(stu4);
list.add(stu5);
System.out.println("排序前链表中的数据:");
Iterator<StudentIF> iter=list.iterator();
while(iter.hasNext()) {
StudentIF studentIF=iter.next();
System.out.println(studentIF.toString());
}
IDComparator idComparator = new IDComparator();
Collections.sort(list, idComparator);
System.out.println("根据学生学号升序排序后链表中的数据:");
Iterator<StudentIF> iter2=list.iterator();
while(iter2.hasNext()) {
StudentIF studentIF=iter2.next();
System.out.println(studentIF.toString());
}
ScoreComparator scoreComparator = new ScoreComparator();
Collections.sort(list, scoreComparator);
System.out.println("根据学生成绩升序排序后链表中的数据:");
Iterator<StudentIF> iter3=list.iterator();
while(iter3.hasNext()) {
StudentIF studentIF=iter3.next();
System.out.println(studentIF.toString());
}
}
}
class IDComparator implements Comparator {
@Override
public int compare(Object o1, Object o2) {
StudentIF st1 = (StudentIF)o1;
StudentIF st2 = (StudentIF)o2;
return (st1.getId()-st2.getId());
}
}
class ScoreComparator implements Comparator{
@Override
public int compare(Object o1, Object o2){
StudentIF st1 = (StudentIF)o1;
StudentIF st2 = (StudentIF)o2;
return (int)(st1.getTotalScore()-st2.getTotalScore());
}
}
class StudentIF {
private int id;//表示学号
private String name;//表示姓名
private int age;//表示年龄
private String sex;//表示性别
private double computer_score;//表示计算机课程的成绩
private double english_score;//表示英语课的成绩
private double maths_score;//表示数学课的成绩
private double total_score;// 表示总成绩
private double ave_score; //表示平均成绩
@Override
public String toString() {
return "姓名:"+name+",年龄:"+age+",学号:"+id+",总分:"+total_score;
}
public StudentIF(int id, String name, String sex, int age,double computer_score,
double english_score,double maths_score) {
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
this.computer_score = computer_score;
this.english_score = english_score;
this.maths_score = maths_score;
}
public int 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(int 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() {
total_score=computer_score + maths_score + english_score;
return total_score;
}// 计算Computer_score, Maths_score 和English_score 三门课的总成绩。
public double getAveScore() {
return getTotalScore() / 3;
}// 计算Computer_score, Maths_score 和English_score 三门课的平均成绩。
}
运行截图:
补充MyList.java的内容
public class MyList {
public static void main(String [] args) {
//选用合适的构造方法,用你学号前后各两名同学的学号创建四个结点
//把上面四个节点连成一个没有头结点的单链表
//遍历单链表,打印每个结点的
//把你自己插入到合适的位置(学号升序)
//遍历单链表,打印每个结点的
//从链表中删除自己
//遍历单链表,打印每个结点的
}
}
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);
}
public String toString() //返回结点数据域的描述字符串
{
return this.data.toString();
}
}
import java.util.*;
public class MyList {
public static void main(String[] args) {
//选用合适的构造方法,用你学号前后各两名同学的学号创建四个结点
List<String> list = new LinkedList<String>();
list.add(new String("20165313"));
list.add(new String("20165314"));
list.add(new String("20165316"));
list.add(new String("20165317"));
//把上面四个节点连成一个没有头结点的单链表
System.out.println("打印初始链表");
//遍历单链表,打印每个结点
Iterator<String> iter = list.iterator();
while (iter.hasNext()) {
String ID = iter.next();
System.out.println(ID);
}
//把你自己插入到合适的位置(学号升序)
list.add(new String("20165315"));
Collections.sort(list);
//遍历单链表,打印每个结点
System.out.println("打印插入后的链表");
Iterator<String> iter2 = list.iterator();
while (iter2.hasNext()) {
String ID = iter2.next();
System.out.println(ID);
}
//从链表中删除自己
list.remove("20165315");
//遍历单链表,打印每个结点的
System.out.println("打印删除后的链表");
Iterator<String> iter3 = list.iterator();
while (iter3.hasNext()) {
String ID = iter3.next();
System.out.println(ID);
}
}
}
运行截图:
后来仔细阅读题目要求,发现此代码并不符合题意,没有用到Node<T>
,也没有删除头节点,于是我在课下改写了上述代码使之符合题意:
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();
}
}
public class MyList2 {
public static void main(String [] args) {
//选用合适的构造方法,用你学号前后各两名同学的学号创建四个结点
Node<Integer> S1 = new Node<Integer>(20165313, null);
Node<Integer> S2 = new Node<Integer>(20165314, null);
Node<Integer> S3 = new Node<Integer>(20165316, null);
Node<Integer> S4 = new Node<Integer>(20165317, null);
//把上面四个节点连成一个没有头结点的单链表
S1.next=S2;
S2.next=S3;
S3.next=S4;
//遍历单链表,打印每个结点
Node<Integer> oriList=S1;
System.out.println("打印初始链表");
while(oriList!=null){
System.out.println(oriList.data);
oriList=oriList.next;
}
//把你自己插入到合适的位置(学号升序)
Node<Integer> S5=new Node<Integer>(20165315,null);
Node<Integer> oriAddList=S1;
while (oriAddList != null) {
if(oriAddList.data < S5.data && oriAddList.next.data > S5.data){
S5.next=oriAddList.next;
oriAddList.next=S5;
break;
}
else{
oriAddList=oriAddList.next;
}
}
//遍历单链表,打印每个结点的
Node<Integer> addList=S1;
System.out.println("打印插入后的链表");
while(addList!=null){
System.out.println(addList.data);
addList=addList.next;
}
//从链表中删除自己
Node<Integer> oriDeleteList=S1;
while(oriDeleteList!=null){
if(oriDeleteList.next.data.equals(S5.data)){
oriDeleteList.next=S5.next;
break;
}
oriDeleteList=oriDeleteList.next;
}
//遍历单链表,打印每个结点的
Node<Integer> deleteList=S1;
System.out.println("打印删除后的链表");
while(deleteList!=null){
System.out.println(deleteList.data);
deleteList=deleteList.next;
}
}
}
运行截图:
get()
方法遍历链表,通过调用currentTimeMillis()
方法比较二者所用的时间sort
方法(调用compareTo
方法)对链表中的数据按身高排序,如果有身高相同的情况,则输出身高相同的姓名shuffle()
、rotate()
方法分别对链表中的数据进行随机排列、向右旋转一次,最后输出wordPolice
类使用Scanner
解析Word.txt
中的单词,然后将英文单词-汉语翻译作为建/值存储到散列映射中用户查询add(V value)
方法对树集中的数据value按英语成绩进行升序排序,并且compareTo
方法不允许有成绩相同的结点put(K key,V value)
方法对树映射中的数据`value
按关键字key
进行升序排列,并且compareTo
方法允许有成绩相同的结点ArrayList<T>
对链表中数据进行自动装箱(add()
方法)和自动拆箱(get()
方法)Goods
类的对象作为链表的结点,并将链表写入文件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++;
}
}
}
}
运行结果截图:
import java.util.*;
public class E2 {
public static void main(String[] args) {
List<Student> list=new LinkedList<Student>();
int score[]={99,88,77,66,55,44,33,22,11};
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);
}
}
}
class Student implements Comparable{
int english=0;
String name;
Student(int english,String name){
this.name=name;
this.english=english;
}
@Override
public int compareTo(Object b){
Student st=(Student)b;
return (this.english-st.english);
}
}
运行结果截图:
import java.util.*;
class UDiscKey implements Comparable {
double key = 0;
UDiscKey(double d) {
this.key = d;
}
@Override
public int compareTo(Object b) {
UDiscKey discKey = (UDiscKey) b;
if ((this.key - discKey.key) == 0) {
return -1;
}
else {
return (int) ((this.key - discKey.key) * 1000);
}
}
}
class UDisc {
int amount;
double price;
UDisc(int a,double p) {
amount=a;
price=p;
}
}
public class E3 {
public static void main(String args[]) {
TreeMap<UDiscKey,UDisc> treemap = new TreeMap<UDiscKey, UDisc>();
int amount[]={100,200,300,400,500,600,700,800,900,1000};
double price[]={111,222,333,444,555,666,777,888,999,1000};
UDisc uDisc[] = new UDisc[10];
for (int k = 0; k < uDisc.length; k++) {
uDisc[k] = new UDisc(amount[k],price[k]);
}
UDiscKey key[] = new UDiscKey[10];
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();
System.out.println("树映射中有" + number + "个对象,按容量排序:");
Collection<UDisc> collection = treemap.values();
Iterator<UDisc> iter = collection.iterator();
while (iter.hasNext()) {
UDisc disc = iter.next();
System.out.println(" 容量 " + disc.amount);
}
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();
System.out.println("树映射中有" + number + "个对象:按价格排序:");
collection = treemap.values();
iter = collection.iterator();
while (iter.hasNext()) {
UDisc disc = (UDisc) iter.next();
System.out.println(" 价格 " + disc.price);
}
}
}
运行结果截图:
标签:第四次 汉语 char s 知识点 out 分享 pre 技术分享 位置
原文地址:https://www.cnblogs.com/yh666/p/9000227.html