2017-02-12 104 views
0

我有下面的代碼片段,它只是將新元素添加到結尾,但我希望能夠添加按字母順序排列的每個新元素按目的地名稱排序。不知道我是否需要在添加後對列表進行排序,或者先插入新對象,然後再添加它。在任何一種情況下都不確定如何去做。執行按順序(按字母順序)添加在對象的java數組

public void add() 
    { 
     int newRating =-1; 
     in = new Scanner(System.in); 
     if((lastElement+1) < MAX_ELEMENT) //MAX_ELEMENT =100 
     { 
      System.out.print("Enter the Name: "); 
      newDestination = in.nextLine(); 

      System.out.print("Enter the type of Vacation(Single character code: "); 
      validCharacterCode(); 

      while(newRating < MIN_RATING || newRating > MAX_RATING) 
      { 
       System.out.print("Enter the Rating(1-5): "); 
       newRating = in.nextInt(); 
      } 
      lastElement++; 
      aDestination[lastElement] = new Destination(newDestination,newVacationType,newRating); 

     } 
     else 
     { 
      System.out.print("Cannot add new elements: "); 
      System.out.println("List already has " + MAX_ELEMENT + " elements."); 
     } 
    } 
+0

如果您選擇插入你的元素,然後將所得數組進行排序,我建議你插入排序,它具有良好的性能,當列表已經將近排序(這是你的名單將是)。 –

+0

如果你不想自己實現排序,你可以看看'Arrays.sort',但是你將不得不實現一個'Comparator'。 –

+0

謝謝,我想實現我自己的排序和插入排序聽起來像我要去的。但我不知道如何去做這件事。我只是一個初學者,我想通過每個元素來檢查每個目標值的第一個字符並進行相應的排序,但這聽起來有點複雜,我認爲 –

回答

1

如果你決定使用Arrays.sort,它應該是沿着這些線路(包括例如比較器功能的使用lambda表達式):

public void add() 
    { 
     String newDestination; 
     int newRating =-1; 
     in = new Scanner(System.in); 
     if((lastElement+1) < MAX_ELEMENT) //MAX_ELEMENT =100 
     { 
      System.out.print("Enter the Name: "); 
      newDestination = in.nextLine(); 

      System.out.print("Enter the type of Vacation(Single character code: "); 
      String newVacationType = in.nextLine(); 

      while(newRating < MIN_RATING || newRating > MAX_RATING) 
      { 
       System.out.print("Enter the Rating(1-5): "); 
       newRating = in.nextInt(); 
      } 
      lastElement++; 

      aDestination[lastElement] = new Destination(newDestination,newVacationType,newRating); 
      Arrays.sort(aDestination, 0, lastElement, (o1, o2) -> o1.destination.compareTo(o2.destination)); 

     } 
     else 
     { 
      System.out.print("Cannot add new elements: "); 
      System.out.println("List already has " + MAX_ELEMENT + " elements."); 
     } 
    } 
1

添加對象的集合在一個特定的順序,這是PriorityQueue (Java Platform SE 7)是爲什麼製作的。它保證隊列內的訂單。如果你需要在最後使用數組,你總是可以將其轉換回來。

使用PriorityQueue<Destination>代替Destination[]

Comparator<Destination> byName = new Comparator<>(
{ 
    @Override 
    public int compare(Destination d1, Destination d2) 
    { 
     return d1.getName().compareTo(d2.getName()); 
    } 
}); 
int initialCapacity = 10; 
PriorityQueue<Destination> destinationsByName = new PriorityQueue<>(initialCapacity, byName); 

現在,重構你的add()方法。使用插入此優先級隊列沒有因爲訂單是照顧由destinationsByName令人擔憂的順序:

public void add() 
{ 
    int newRating = -1; 
    in = new Scanner(System.in); 
    if ((lastElement+1) < MAX_ELEMENT) //MAX_ELEMENT =100 
    { 
     ... 
     Destination d = new Destination(...); 
     destinationsByName.add(d); 
     // no need to sort again 
    } 
    ... 
} 

如果你再次需要一個數組?沒問題,你可以用下面的方法將其轉換回:

destinationsByName.toArray(new Destination[0]);