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.");
}
}
Arrays.sort, but you will have to implement aComparator.