too long no see
解决方案 »
- 不能编译3
- 阅读《Java核心开发技术从入门到精通》时,遇到的问题:这个语句是什么意思…
- java运行时出现错误
- 小弟最近做到个POI的关于在JAVA中读取EXCEL文件内容是出现个问题望高手指点!!!!!!!!!!!!!
- java能调用winapi吗
- 字符替换
- swt中另一个比较少见的问题,100分,近来就有!
- 请教JTree节点的排序问题
- 向大家请教关于数组的问题
- 调查,有多少公司在开发之前先做Testcase的,另外,对于已有系统结构的重新整理(8 packages and 572 files),大家估计要多少人月。
- 求助各位大神 这里是小白 关于Java检索excel中的数字的
- java调用webservice并解析返回值
或者自己编写一个比较器.同样用Collections.sort(persons,comparator)
汉字排序
case 4:
System.out.println("通讯录 \n 输入功能序号:1:单个查询 2:全体查询");
Scanner scanner2 = new Scanner(System.in);
while(true){
int j = scanner2.nextInt();
switch (j){
case 1: System.out.print("输入要查询的姓名:");
PersonUtils.get(scanner2.next());
break;
case 2:PersonUtils.getAll();
break;
}
}
break;
我想在switch语句里面嵌套switch语句,可是 添加break跳出外层时提示出错啊????
import java.util.Comparator;
import java.util.ArrayList;
import java.util.Collections;public class Person{
private String name;
private String mobile;
private String email;
//仅仅添加做实例.需要其他变量可以自己添加 public Person(){
//default constructor
} public void setName(String name){
this.name = name;
} public void setMobile(String mobile){
this.mobile = mobile;
} public void setEmail(String email){
this.email = email;
} public String getName(){
return name;
} public String getMobile(){
return mobile;
} public String getEmail(){
return email;
} @Override
public String toString(){
return String.format("Person[name:%s,mobile:%s,email:%s]",name,mobile,email);
} public static void main(String[] args){
ArrayList<Person> people = new ArrayList<>();
Person ricky = new Person();
ricky.setName("ricky"); ricky.setMobile("138888888888");ricky.setEmail("[email protected]");
Person jennie = new Person();
jennie.setName("jennie");jennie.setMobile("13999999999");jennie.setEmail("[email protected]");
people.add(ricky);
people.add(jennie); PersonComparator comparator = new PersonComparator();
//默认排序
Collections.sort(people,comparator);
for(Person person : people){
System.out.println(person);
}
System.out.println("---------------------------------------");
//采用MOBILE排序
Collections.sort(people,comparator.setCompareType(PersonComparator.MOBILE));
for(Person person : people){
System.out.println(person);
}
} private static class PersonComparator implements Comparator<Person>{
public int type; static final int NAME = 0;
static final int MOBILE = 1;
static final int EMAIL = 2; @Override
public int compare(Person person1,Person person2){
if(type == NAME){
assert person1.getName() != null;
return person1.getName().compareTo(person2.getName());
}else if(type == MOBILE){
assert person1.getMobile() != null;
return person1.getMobile().compareTo(person2.getMobile());
}else if(type == EMAIL){
assert person1.getEmail() != null;
return person1.getEmail().compareTo(person2.getEmail());
}
//默认比较的是名字
assert person1.getName() != null;
return person1.getName().compareTo(person2.getName());
}
//设置参数可以可以采用不同的排列顺序
public PersonComparator setCompareType(int type){
this.type = type;
return this;
} }
}