Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.86 KB | None | 0 0
  1. // Bucket.java
  2. //
  3. // ICS 21: Lab Assignment 4
  4. //
  5. //
  6. // Originally implemented by Norman Jacobson
  7. // Minor modifications introduced by Alex Thornton, Summer 2009
  8. // Minor modifications for ICS 21 Fall 2009 by Norman Jacobson, September 2009
  9.  
  10.  
  11. import java.util.ArrayList;
  12.  
  13. //A Bucket is one of the sublists that make up a music list.  Each bucket
  14. //contains a list of music items whose titles begin with the same letter,
  15. //with the items in that bucket sorted in ascending order by title.
  16.  
  17. public class Bucket
  18. {
  19.     // The items in this bucket
  20.     private ArrayList<MusicItem> items;
  21.    
  22.    
  23.     // This constructor initializes the bucket to be empty
  24.     public Bucket()
  25.     {
  26.         items = new ArrayList<MusicItem>();
  27.     }
  28.    
  29.  
  30.     // addItem() places an item into a bucket in its proper (sorted) place.
  31.     //
  32.     // Note that the solution to this problem revolves around the fact that
  33.     // the bucket is, at any given time, sorted already.  So, the goal is to
  34.     // find the appropriate place to insert the new item.  The
  35.     // following algorithm (there are others) will accomplish that task:
  36.     //
  37.     // * Go through the list until you find an item whose title is greater
  38.     //   than the title of the item you want to add.
  39.     // * When you find such an item, add the new item just before the item
  40.     //   where you stopped searching.
  41.     // * If you get all the way through the list and don't find an item whose
  42.     //   title is greater than the title of the item you want to add, add the
  43.     //   new item to the end of the list.
  44.     public void addItem(MusicItem itemToAdd)
  45.     {
  46.         for (int i = 0; i < items.size(); ++i)
  47.         {
  48.             if (items.get(i).getTitle().compareTo(itemToAdd.getTitle()) > 0)
  49.             {
  50.                 items.add(i, itemToAdd);
  51.                 return;
  52.             }
  53.         }
  54.        
  55.         items.add(itemToAdd);
  56.     }
  57.    
  58.  
  59.     // getItems() returns the list of items in this bucket.
  60.     public ArrayList<MusicItem> getItems()
  61.     {
  62.         return items;
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement