java测试

Java测试

开发工具与关键技术:MyEclipse 10、java
作者:梁添荣
撰写时间:2019-4-27

想起第一次java测试时,那条同学排序问题没有做对,看了老师给的答案,他是用list做,然为了锻炼自己,用Treemap做了一遍,加深自己对排序的印象。
package com.gx.test;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.TreeMap;
public class Ss {
public static void main(String[] args) {
Map ss =new TreeMap();
int xuehao1;
String xuehao2;
String xingming2;
Random su =new Random();
for (int i = 1; i < 41; i++) {
xingming2=String.valueOf(i);
if(xingming2.length()<2){
xuehao2=17010+xingming2;
xuehao1=Integer.valueOf(xuehao2);
xingming2=“同学”+“0”+xingming2;
}else{
xuehao2=1701+xingming2;
xuehao1=Integer.valueOf(xuehao2);
xingming2=“同学”+xingming2;
}
int cj = su.nextInt(51)+50;
ss.put(new Ts2(cj,xuehao1), xingming2);
}
Set ke = ss.keySet();
for (Ts2 ts : ke) {
System.out.print(ss.get(ts));
System.out.println(ts.getXuehao()+"\t"+ss.get(ts)+"\t"+ts.getChengji());
}
}
}
class Ts2 implements Comparable{
private int chengji;
private int xuehao;
public Ts2(int chengji, int xuehao) {
this.chengji = chengji;
this.xuehao = xuehao;
}
public int getChengji() {
return chengji;
}
public void setChengji(int chengji) {
this.chengji = chengji;
}
public int getXuehao() {
return xuehao;
}
public void setXuehao(int xuehao) {
this.xuehao = xuehao;
}
@Override
public int compareTo(Ts2 o) {
if(o.chengji!=this.chengji){
return o.chengji-this.chengji;
}else{
return this.xuehao - o.xuehao;
}
}
}效果图以下:
java测试_第1张图片

你可能感兴趣的:(java测试)