简体   繁体   中英

Sorting an ArrayList in Java

So I'm having trouble figuring out how to update a TextArea with information that I submit from an generics arraylist. As of now the program creates a new Order:

Order d1 = new Order();

Then the user selects some data and pushes an add button, and the order is added to a TextArea. The problem I have is that I have to add the order to the correct spot in the list and update it each time. I"m only sorting it by one item. I'm not really sure how to do that using the CompareTo method.

 public void actionPerformed(ActionEvent event) 
 {
     ArrayList<Drink> DrinkArray = new ArrayList<Drink>();

     if (event.getSource() == addcoffeeButton)
     {

         String coffeesize = (String) sizecoffeelist.getSelectedItem();
         double coffeeprice = Double.parseDouble(pricecoffeeTextfield.getText());

         String coffeetype = (String) cuptypecoffeelist.getSelectedItem();
         String coffeecaffeine = (String) caffeineList.getSelectedItem();
         String coffeeroom = (String) roomforcreamList.getSelectedItem();
         String coffeeadditional = additionalflavorList.getText();
         if  ((coffeeadditional.isEmpty()))
             coffeeadditional = "No Additional Flavor";

         Drink d1 = new Coffee(coffeesize, coffeeprice, coffeetype, coffeecaffeine, coffeeroom, coffeeadditional);


          DrinkArray.add(d1);
          orderTextArea.append(d1);

So I would have to add the drink to the correct spot before adding it to the array and printing to the text area, but I'm not quite sure how to do that.

I'll assume that Drink implements Comparable . Look at the javadocs if you don't know what that means.

If that's true, you can do this:

List<Drink> drinks = new ArrayList<Drink>();
// add Drinks
Collections.sort(drinks); // now they're sorted according to your Comparable.

You can also instantiate a Comparator and pass it to the sorts method.

Something like this (make the getValue() function whatever you want):

public class DrinkComparator implements Comparator<Drink> {
    public int compare(Drink d1, Drink d2) {
       if (d1.getValue() < d2.getValue()) { 
           return -1;
       } else if (d1.getValue() > d2.getValue()) {
           return 1;
       } else {
           return 0;
       }
    } 

    public boolean equals(Object obj) {
        return this.compare(this, (Drink)obj) == 0;
    }  
}

You basically need to pre-determine the insertion point where the "object" would be inserted...

Take a look at Collections.binarySearch(List<T>, T)

From the Java Docs

Returns:

the index of the search key, if it is contained in the list; otherwise, (-(insertion point) - 1) . The insertion point is defined as the point at which the key would be inserted into the list: the index of the first element greater than the key, or list.size() if all elements in the list are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM