Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用compareto()对Person对象数组进行排序?_Java_Sorting_Compareto - Fatal编程技术网

Java 如何使用compareto()对Person对象数组进行排序?

Java 如何使用compareto()对Person对象数组进行排序?,java,sorting,compareto,Java,Sorting,Compareto,这是我的密码: > 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.

这是我的密码:

> 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());
     }
   }
以及个人类别:

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

}
/**
有名字的人。
*/
公共类人员实现可比性
{
/**
*用一个名字构造一个人。
*@param aName这个人的名字
*/
公众人物(字符串名称)
{
name=aName;
}
公共字符串getName()
{
返回名称;
}
@凌驾
公共整数比较对象(对象其他对象)
{
Person other=(Person)otherObject;
if(name.compareTo(other.name)<0)返回-1;
如果(name.compareTo(other.name)>0)返回1;
返回0;
}
/**
返回对象的字符串表示形式。
@报税人姓名
*/
公共字符串toString()
{
返回“[name=“+name+”]”;
}
私有字符串名称;
}

您实际上缺少的是
compareTo
是您为对象提供的功能。在您的示例中,您为
Person
实例提供了与其他
Person
进行比较的能力,以实现该类元素的总排序

通常这种方法被JDK集合隐式使用,比如
列表
分类地图
等等,但是您有一个数组,这是一种基本类型,因此您应该查看
数组。sort(Object[])
,它将使用
Compariable
接口对其进行排序


提示:由于您的
compareTo
方法只处理
Person
字符串,因此您可以轻松返回其值,而不必检查其行为是否相同。

首先,将数组创建置于循环之外。第二,即使作业上说他们只会输入10个名字,你也应该假设更多——你只是在问那些无法识别的单词。考虑使用自动排序的集合。

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());
致:

要对集合进行排序(集合是一个没有重复项的集合),只需使用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());
TreeSet people=new TreeSet();//使用泛型表示“仅允许Person对象”
而(更多){
System.out.println(“请输入要退出的人员姓名或空行”);
字符串名称=in.nextLine();
if(name.equals(“”)
更多=错误;
其他的
人员。添加(新人员(姓名));
}
System.out.println(“First:+people.First());
System.out.println(“Last:+people.Last());

首先,在最初将更多设置为true之前,您的代码不会做任何事情。否则,当您尝试调用null对象上的tostring时,您将只得到一个null指针异常

另外,不要在while循环中创建person数组,否则每次都会重置它,从而破坏存储所有人的目的

此外,对于zoo类的对象foo和bar,您不能只执行以下操作:

foo.compareto(bar);
那就像是有一行代码,比如

-4;
Java不喜欢它

所以,用比较法来回答你的问题,就像预成型的真理测试一样。比如说

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

作业可能会说不要使用集合/数组列表等。不过,如果可以的话,我会使用一个类让java为您做这件事。
if(foo.compareto(bar) >0)
    /* foo is greater*/;
else if(foo.compareto(bar) = 0)
    /* foo and bar are equal*/;
else
    /* bar is greater*/;