2010-04-01 60 views
0

这里是我的代码:如何使用compareto()对Person对象进行排序?

> import java.util.Scanner; 
    import java.util.Arrays; 

    /** 
    This class tests the Person class. 
    */ 
    public class PersonDemo 
    { 
    public static void main(String[] args) 
    { 
    int count = 0; 
    Scanner in = new Scanner(System.in); 

    boolean more = false; 
    Person first = null; 
    Person last = null; 
    while (more) 
    { 
     System.out.println(
      "Please enter the person's name or a blank line to quit"); 
     String name = in.nextLine(); 

     if (name.equals("")) 
     more = false; 
     else 
     { 
     Person p = new Person(name); //new person object created with inputted name 

     Person[] people = new Person[10]; //new array of 10 person objects 
     people[count] = p; //declare person object with index of variable count as the new person object        

     first = people[count]; // I have no idea what to do here. This is where I'm stuck. 
     last = people[count]; // I can't figure out what to do with this either. 

     first.compareTo(p); //call compareTo method on first and new person object 
     last.compareTo(p); //call compareTo method on last and new person object  

     count++; // increase count variable 
     } 
    } 

     System.out.println("First: " + first.toString()); 
     System.out.println("Last: " + last.toString()); 
    } 
    } 

而Person类:

/** 
    A person with a name. 
*/ 
public class Person implements Comparable 

{ 
/** 
    * Constructs a Person with a name. 
    * @param aName the person's name 
    */ 
public Person(String aName) 
{ 
    name = aName; 
} 

public String getName() 
{ 
    return name; 
} 

@Override 
public int compareTo(Object otherObject) 
{ 
    Person other = (Person)otherObject; 
    if (name.compareTo(other.name) < 0) return -1; 
    if (name.compareTo(other.name) > 0) return 1; 
    return 0; 
} 

/** 
     Returns a string representation of the object. 
     @return name of Person 
*/ 
public String toString() 
{ 
    return "[name=" + name + "]"; 
    } 

private String name; 

} 
+0

这功课吗? – 2010-04-01 04:11:07

回答

1

什么你实际上缺少的是compareTo是你给到对象的功能。在你的例子中,你给出了一个Person实例与其他Person进行比较的能力,以实现总订购该类的元素。

通常这个方法implictly使用JDK的集合像ListsSortedMaps等等,但你有一个阵列,这是一种基本类型的,所以你应该看看Arrays.sort(Object[])这会照顾有关使用订购它接口为Comparable

提示:因为您的compareTo方法只适用于PersonString,您可以轻松地返回它的值而不是检查它的行为方式。

0

首先,让您的数组创建在循环之外。其次,即使作业分配表示他们只会输入10个名字,但您应该假设更多 - 您只需要索引IndexOutOfBoundsExceptions。考虑使用自动排序的类似TreeSet的集合。

sort an array

Person[] people = new Person[10]; 
// add elements to the array... 
java.util.Arrays.sort(people); 
first = people[0]; 
// etc... 

要排序集合(Set是没有重复的集合)只使用TreeSet的,他们正在自动排序。

TreeSet<Person> people = new TreeSet<Person>(); // using generics to say "only Person objects are allowed" 
while (more) { 
    System.out.println("Please enter the person's name or a blank line to quit"); 
    String name = in.nextLine(); 

    if (name.equals("")) 
     more = false; 
    else 
     people.add(new Person(name)); 
} 

System.out.println("First: " + people.first()); 
System.out.println("Last: " + people.last()); 
+0

作业可能会说不使用集合/数组列表等等。尽管如果可以的话,我会使用一个类让java为你做。 – 2010-04-01 04:24:29

0

那么,首先,你的代码将不会做任何事情,直到你最初设置更多为真。否则,当你试图在空对象上调用tostring时,你会得到一个空指针异常。

此外,不要在while循环内创建person数组,否则每次都会重置它,并打败似乎是存储所有人的目的。

另外,考虑到对象foo和bar,动物园类的,你不能只是做:

foo.compareto(bar); 

这就好比只是有一样

-4; 

一行代码Java不喜欢它。

所以,回答你的问题,使用比较的方法,就像瓶坯真相测试。例如,

if(foo<bar) 
    /*Do something*/; 

除了,您可以使用.compareto(),以便您实际比较对象,而不是简单的引用。所以,你可以通过比较他们与比较来确定动物园类型的哪个对象更大,foo或bar。 compareto的API规定,如果返回0,则它们相等,如果返回一个正数,则第一个更大,如果返回负数,则第二个更大。所以:

if(foo.compareto(bar) >0) 
    /* foo is greater*/; 
else if(foo.compareto(bar) = 0) 
    /* foo and bar are equal*/; 
else 
    /* bar is greater*/; 
相关问题