2013-03-01 101 views
1

我試圖按照Sort an arraylist of objects in java的建議在我的程序中實現排序方法,但無法實現此功能。我得到的錯誤 「無法找到符號 - 方法排序()」Java排序對象的陣列列表

Appologies即時通訊新到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; 
} 

} 

AddressBook類:

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(); 
} 

} 
+0

下一次,發佈代碼的**相關**部分,而不是發佈您編碼的所有內容。 – 2013-03-01 08:14:31

回答

3

您需要使用Collections.sort(List<T> list)

列表是一個接口。 ArrayList間接實現List接口。

+0

所以而不是entries.sort我會使用Collections.sort(列表列表)? – ToniHopkins 2013-03-01 08:21:20

+0

@ToniHopkins:是的。該方法修改相同的集合,使其不返回任何內容。一旦你調用該方法,你的集合將被分類。 – npinti 2013-03-01 08:27:41

+0

@npinti我只是得到一個錯誤,說找不到符號 - 變量列表 – ToniHopkins 2013-03-01 08:31:38

4

假設您指的是:entries.sort();,您需要致電Collections.sort(List<T> list)Collection.sort(List<T> list, Comparator<T> comparator)

兩者之間的區別在於第一個使用默認行爲,可以通過實現Comparable接口指定的compareTo方法在要分類的對象中指定默認行爲,而第二個允許您傳遞比較對象。這將允許您以不同的方式對相同的列表進行排序。

+0

爲什麼選擇投票? – npinti 2013-03-01 08:42:50

1
  1. 使詞條實現可比:

    公共類條目實現可比

  2. 加INT的compareTo(進入otherEntry)法(簡化僞代碼):

    if this > otherEntry return 1 
    else if this < otherEntry return -1 
    else return 0 
    
  3. 呼叫集合。排序(您的條目數組)

+0

我跟隨youradvice並創建了compareTo方法,但是我得到以下錯誤:二元運算符'>'的第一個類型的不良操作數類型:Entry;第二種類型:條目 – ToniHopkins 2013-03-01 09:02:45

+0

導致它不是有效的java代碼,它是僞代碼。你必須編寫自己的內容來比較兩個條目。 – 2013-03-01 09:16:58

+0

對不起,我應該說我用名字來嘗試和比較,並得到這個錯誤。我發現另一篇文章使用名稱在http://stackoverflow.com/questions/10017381/compareto-method-java下創建compareTo方法,所以我會嘗試使用這裏的示例。謝謝 – ToniHopkins 2013-03-01 09:26:58