0

これが私のコードです:

> 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; 

}
4

3 に答える 3

1

実際に欠けているcompareToのは、オブジェクトに与える機能です。あなたの例では、そのクラスの要素の完全な順序付けを達成するために、Personインスタンスに他のインスタンスと比較する機能を与えます。Person

Lists通常、このメソッドは などのJDK コレクションによって暗黙的に使用されSortedMapsますが、一種のプリミティブ型であるarrayArrays.sort(Object[])があるため、インターフェイスを使用して順序付けを行う方法を確認する必要がありComparableます。

ヒント:compareToメソッドは の で機能するだけなStringのでPerson、同じように動作するかどうかをチェックする代わりに、その値を簡単に返すことができます。

于 2010-04-01T04:19:15.430 に答える
0

まず第一に、最初に more を true に設定するまで、コードはほとんど何もしません。そうしないと、null オブジェクトで tostring を呼び出そうとすると、null ポインター例外が発生します。

また、while ループ内で person 配列を作成しないでください。そうしないと、毎回リセットされ、すべての人を格納する目的と思われるものが無効になります。

また、zoo クラスのオブジェクト foo と bar に与えられた場合、次のことはできません。

foo.compareto(bar);

それは、次のようなコード行を持つようなものです

-4;

Javaはそれが好きではありません。

したがって、compare to メソッドを使用して質問に答えるには、プリフォームの真理値テストのようなものです。例えば

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

ただし、.compareto() を使用して、単に参照するのではなく、実際にオブジェクトを比較することができます。したがって、zoo 型のオブジェクトのうち、foo と bar のどちらが大きいかは、compare to と比較することで判断できます。compareto の API では、0 が返された場合はそれらが等しく、正の数が返された場合は 1 番目の方が大きく、負の数が返された場合は 2 番目の方が大きいと規定されています。そう:

if(foo.compareto(bar) >0)
    /* foo is greater*/;
else if(foo.compareto(bar) = 0)
    /* foo and bar are equal*/;
else
    /* bar is greater*/;
于 2010-04-01T04:23:27.447 に答える
0

まず、ループの外で配列を作成します。第 2 に、宿題で名前を 10 個しか入力しないと言われていたとしても、それ以上だと想定する必要があります。つまり、IndexOutOfBoundsExceptions を要求しているだけです。自動的にソートされるTreeSetのようなコレクションの使用を検討してください。

配列をソートするには:

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

コレクション (セットは重複のないコレクションです) を並べ替えるには、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());
于 2010-04-01T04:21:32.700 に答える