Quantcast
Channel: Sorting an ArrayList in Java - Stack Overflow
Viewing all articles
Browse latest Browse all 3

Sorting an ArrayList in Java

$
0
0

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.


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images