Search code examples
javaarraylistcomparable

remove item from comparable array


I'm very new to java and my first question is what is this Comparable thing? and in my sample code, why Class Bag<T extends Comparable<T>>?? What's the purpose?

My second question is how to remove an item from this Comparable array. I have wrote the removeMin method, I have found the item, I named the variable "min", how do I go on and remove this min item from the arrayList? Thanks a lot!

Here's some partial code:

public class Bag<T extends Comparable<T>> implements Iterable<T> {
    private int MAX_ITEMS = 10; // initial array size
    private int size;
    private Comparable[] data;

    public Bag( ) {
        //data = (Comparable []) new Object[MAX_ITEMS];
        data = (T []) new Comparable[MAX_ITEMS];
        size = 0;
    }

      public Comparable removeMin(){
          Iterator<T> iterator=iterator();
          T min = iterator.next();
          while (iterator.hasNext())
          {  
             T next = iterator.next();
             if (min.compareTo(next) > 0) 
                min = next;
                ****************************************************
                How to remove this min item? 
                ****************************************************
                }
          return min;
          }

Solution

  • What is this Comparable thing?

    It is a generic interface that can be implemented by classes whose instances you need to be able to compare with each other. Comparable<T> has a single method int compareTo<other T>. The implementation of the method is supposed to compare this with the other instance

    In my sample code, why class Bag<T extends Comparable<T>>? What's the purpose?

    This is saying that your Bag class is a generic class with a type parameter T. The T extends Comparable<T> bits is saying that T must be some class that implements Comparable<T>.


    My second question is how to remove an item from this Comparable array.

    That is for you to figure out, based on your knowledge of how arrays work. The big constraint you have to deal with is that an array's size cannot be changed, so there is no magical operation that will simply remove something from the middle of the array.

    There are two approaches:

    • Create a new array containing just the elements that you don't want to be removed. (Hint: copy them)

    • Move the elements around in the existing array to "fill the hole" left when you conceptually remove an element. (Hint: notice the size field. What is it there for?)

    Assuming that you wrote the removeMin method from scratch, I think you have gone down a blind alley with trying to use an iterator there1. Try coding it to operate directly on the data array.

    The removeMin problem has two parts:

    1. Find the index in data for the smallest element
    2. Remove the element at that index

    Hint: the first part requires that you look at every element ... and this has to be done before you do the removal. This should be reflected in your code.


    1 - If you were using an ArrayList<T> or similar, then using data.iterator() would be a reasonable choice. The problem is that arrays don't directly support the Iterable API.

    Now if you are required to make your Bag class implement Iterable<T>, then you could use the iteration mechanism you implemented for that purpose in your removeMin method. But even there, the API is not ideal for the purpose. You would still need to iterate the collection twice. Once to find the smallest element, and a second time to remove it. And removal via an iterator means that the iterator would need to implement the optional Iterator.remove() method .... which brings you back full circle.