import java.util.*;public class EmployeeSortTest
{
public static void main(String[] args)
{
Employee[] staff = new Employee[3]; staff[0] = new Employee("Harry Hacker", 35000);
staff[1] = new Employee("Carl Cracker", 75000);
staff[2] = new Employee("Tony Tester", 38000); Arrays.sort(staff); // print out information about all Employee objects
for (Employee e : staff)
System.out.println("name=" + e.getName() + ",salary=" + e.getSalary());
}
}class Employee implements Comparable<Employee>
{
public Employee(String n, double s)
{
name = n;
salary = s;
}public String getName()
{
return name;
}public double getSalary()
{
return salary;
}public void raiseSalary(double byPercent)
{
double raise = salary * byPercent / 100;
salary += raise;
}/**
Compares employees by salary
@param other another Employee object
@return a negative value if this employee has a lower
salary than otherObject, 0 if the salaries are the same,
a positive value otherwise
*/
public int compareTo(Employee other)
{
if (salary < other.salary) return -1;
if (salary > other.salary) return 1;
return 0;
}private String name;
private double salary;
}
为什么排序之后的结果是:
name=Harry Hacker,salary=35000.0
name=Tony Tester,salary=38000.0
name=Carl Cracker,salary=75000.0
这样的呢?
不是想象的
name=Harry Hacker,salary=35000.0
name=Carl Cracker,salary=75000.0
name=Tony Tester,salary=38000.0
这样的呢?
{
public static void main(String[] args)
{
Employee[] staff = new Employee[3]; staff[0] = new Employee("Harry Hacker", 35000);
staff[1] = new Employee("Carl Cracker", 75000);
staff[2] = new Employee("Tony Tester", 38000); Arrays.sort(staff); // print out information about all Employee objects
for (Employee e : staff)
System.out.println("name=" + e.getName() + ",salary=" + e.getSalary());
}
}class Employee implements Comparable<Employee>
{
public Employee(String n, double s)
{
name = n;
salary = s;
}public String getName()
{
return name;
}public double getSalary()
{
return salary;
}public void raiseSalary(double byPercent)
{
double raise = salary * byPercent / 100;
salary += raise;
}/**
Compares employees by salary
@param other another Employee object
@return a negative value if this employee has a lower
salary than otherObject, 0 if the salaries are the same,
a positive value otherwise
*/
public int compareTo(Employee other)
{
if (salary < other.salary) return -1;
if (salary > other.salary) return 1;
return 0;
}private String name;
private double salary;
}
为什么排序之后的结果是:
name=Harry Hacker,salary=35000.0
name=Tony Tester,salary=38000.0
name=Carl Cracker,salary=75000.0
这样的呢?
不是想象的
name=Harry Hacker,salary=35000.0
name=Carl Cracker,salary=75000.0
name=Tony Tester,salary=38000.0
这样的呢?
解决方案 »
- 值得各位全心考虑的问题...谢谢关注...
- MyExplorer java编写的资源管理器
- 我是一个初学者,我很想学JAVA,请教:!!!
- 请问JFRAME里面哪个函数可以设置隐藏标题栏?急!!!!
- 请问哪里有实现“自动完成”功能的代码?
- 如何在两个符号之间 获取其内容?
- applet取参数问题?明明有值,为什么还有NullPointerException?请大虾们帮忙看看!
- 怎么从任何地方读取任何形式的文件,再把它放到另外一个地方?
- 又来麻烦老哥老姐们了,搞了一个星期了,请问如何......?(一个十分想学好JAVA的弟弟),谢谢您!
- Arrays.asList()
- java applecation可以播放声音吗
- 我顶!顶顶顶!就是顶这网站!我喜欢!北大青鸟学员
````` 1.5 很好啊!
所以,当你 Arrays.sort(staff); 之后,自然就是按salary从小到大排列了。这是很正常的啊。