2017-02-12 57 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]);