簡體   English   中英

按順序(字母順序)添加對象的Java數組

[英]Perform in-order(alphabetical) add in java array of objects

我下面有一段代碼,就是將新元素添加到末尾,但我希望能夠按目的地名稱按字母順序添加每個新元素。 不知道添加后我是否必須對列表進行排序,還是先檢查然后添加新對象即可插入新對象。 無論哪種情況,都不確定如何去做。

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.");
       } 
   }

如果決定使用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.");
       } 
   }

以特定順序添加對象的集合,這就是創建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]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM