ArraySortDemo1
来源:互联网 发布:ubuntu连不上有线网 编辑:程序博客网 时间:2024/04/30 03:28
package ArraySort;
import java.util.Arrays;
public class ArraySortDemo1
{
public static void main(String[] args)
{
Dog o1 = new Dog("dog1", 1);
Dog o2 = new Dog("dog2", 4);
Dog o3 = new Dog("dog3", 5);
Dog o4 = new Dog("dog4", 2);
Dog o5 = new Dog("dog5", 3);
Dog[] dogs = new Dog[] { o1, o2, o3, o4, o5 };
System.out.println("排序前");
for (Dog d:dogs) {
System.out.println(d.getName());
}
System.out.println("排序后");
Arrays.sort(dogs);
for (Dog d:dogs) {
System.out.println(d.getName());
}
}
}
class Dog implements Comparable{
private String name;
private int weight;
public Dog(String name, int weight) {
this.setName(name);
this.weight = weight;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public int compareTo(Object o)
{
Dog d=(Dog)o;
if(this.getWeight()>d.getWeight())
return 1;
else if(this.getWeight()<d.getWeight())
return -1;
else
return 0;
}
}
import java.util.Arrays;
public class ArraySortDemo1
{
public static void main(String[] args)
{
Dog o1 = new Dog("dog1", 1);
Dog o2 = new Dog("dog2", 4);
Dog o3 = new Dog("dog3", 5);
Dog o4 = new Dog("dog4", 2);
Dog o5 = new Dog("dog5", 3);
Dog[] dogs = new Dog[] { o1, o2, o3, o4, o5 };
System.out.println("排序前");
for (Dog d:dogs) {
System.out.println(d.getName());
}
System.out.println("排序后");
Arrays.sort(dogs);
for (Dog d:dogs) {
System.out.println(d.getName());
}
}
}
class Dog implements Comparable{
private String name;
private int weight;
public Dog(String name, int weight) {
this.setName(name);
this.weight = weight;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public int compareTo(Object o)
{
Dog d=(Dog)o;
if(this.getWeight()>d.getWeight())
return 1;
else if(this.getWeight()<d.getWeight())
return -1;
else
return 0;
}
}
- ArraySortDemo1
- Call private Method.
- 给程序员的vim指令速查图表
- 债券研究系列之四——可转换公司债券
- ArraySortDemo
- document.defaultView.getComputedStyle
- ArraySortDemo1
- 光纤交换机如何划分zone
- 给XJTU大一CS系学生的网络资源推荐及一些建议
- jQuery学习之:jqGrid表格插件——参数配置
- 中位数之第K小的线性选择算法
- 水晶报表设置字段自动换行
- 准确获取当前时间
- 黑莓开发环境搭建windows7+eclipse+BlackBerry Java Plug+JDK6
- Sqlite简单介绍与一些常用的例子