Reputation: 43
I have the piece of code below which as it is, just adds the new element to the end but i want to be able to add each new element ordered in alphabetical order by the Destination name. Not sure if i would have to sort the list after addition or insert the new object by first checking and then adding it. In either case not sure how to go about doing it.
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.");
}
}
Upvotes: 0
Views: 623
Reputation: 5552
Adding a collection of objects in a specific order, that's what the PriorityQueue (Java Platform SE 7) is made for. It guarantees the order inside the queue. If you need to use an array at the end, you can always convert it back.
Use PriorityQueue<Destination>
instead of 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);
Now, refactor your add()
method. Use this priority queue for insertion without worrying the order since order is taken care by 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
}
...
}
What if you need an array again? No problem, you can use the following method to convert it back:
destinationsByName.toArray(new Destination[0]);
Upvotes: 1
Reputation: 1050
If you decide to use Arrays.sort
, it should be along these lines (includes example of comparator function using a lambda expression):
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.");
}
}
Upvotes: 1