1

Sort an arraylist of objects in Javaのアドバイスに従って、プログラムに sort メソッドを実装しようとしていますが、これを機能させることができませんでした。「シンボルが見つかりません - メソッド sort()」というエラーが表示されます

謝罪私はJavaが初めてです..

エントリークラス:

/**
 * The Entry class represents a persons address entry into the system.
 * It holds the persons name and address information
 * 
 */
public class Entry
{
// the person's full name
private String name;
// the steet namd or number
private String street;
// the town
private String town;
// postcode
private String postcode;

/**
 * Create a new entry with a given name and address details.
 */
public Entry(String strName, String strStreet, String strTown, String strPostcode)
{
    name = strName;
    street = strStreet;
    town = strTown;
    postcode = strPostcode;
}

/**
 * Return the address for this person. The address shows the
 * street, town and postcode for the person
 * @return address
 */
public String getAddress()
{
    return name + ", " + street + ", " + town + ", " + postcode;
}

}

アドレスブック クラス:

import java.util.*;

/**
 * The AddressBook class represents an address book holding multiple persons details. It stores
 * the name, street, town, postcode and number of entries.
 */
public class AddressBook
{
private ArrayList < Entry > entries;

/**
 * Create a an AddressBook with no limit on the number of entries
 */
public AddressBook()
{
    entries = new ArrayList < Entry >();
}

 /**
 * Return the number of address book entries
 * @return the entry amount
 */
 public String getAddressBook()
{
    String listEntries = "Address Book\n";

    listEntries = listEntries + "\nNumber of entries: " + numberOfEntries() +"\n";

    entries.sort();

    for (int i = 0; i < entries.size(); i++) {
           System.out.println(entries.get(i));
          }
    return listEntries;
}

AddressBookTextUI クラス:

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
* Provides a text based user interface for the AddressBook project.
* 
*/
public class AddressBookTextUI {
private AddressBook addressBook;
Scanner myScanner;

/**
 * Constructor for objects of class TextUI
 */
public AddressBookTextUI()
{
    addressBook = new AddressBook();
    myScanner = new Scanner(System.in);

}



private void fullCommand()
{
    System.out.println(addressBook.getAddressBook());
    clearScreen();
}

}
4

3 に答える 3

4

this: を参照していると仮定すると、 orentries.sort();を呼び出す必要があります。Collections.sort(List<T> list)Collection.sort(List<T> list, Comparator<T> comparator)

2 つの違いは、最初の動作はデフォルトの動作を使用することです。これはcompareTo、インターフェイスで指定されたメソッドを実装することで並べ替えるオブジェクトで指定Comparableできますが、2 番目の動作ではコンパレータ オブジェクトを渡すことができます。これにより、同じリストを別の方法で並べ替えることができます。

于 2013-03-01T08:11:59.550 に答える
3

を使用する必要がありますCollections.sort(List<T> list)

リストはインターフェースです。ArrayList 間接的に List インターフェイスを実装します。

于 2013-03-01T08:10:56.077 に答える
1
  1. Make Entry は Comparable を実装します。

    public クラス Entry は Comparable を実装します

  2. int compareTo(Entry otherEntry) メソッドを追加します (簡略化された疑似コード):

    if this > otherEntry return 1
    else if this < otherEntry return -1
    else return 0
    
  3. Collections.sort(エントリの配列) を呼び出します

于 2013-03-01T08:17:00.387 に答える