I have thought and thought but the sorting alphabetically doesnt work. There are two classes Record and CDRack. I can't use Collections.sort and class Record doesnt implement Comparable(and I can't change it).
One suggestion is that I could make my own class which implements Comparator.
import java.util.*;
public class AlphabeticComparator implements Comparator{ public int compare(Object o1, Object o2) { String s1 = (String)o1; String s2 = (String)o2; return s1.toLowerCase().compareTo( s2.toLowerCase()); } }
But how an earth do I "tell" to CDRack class that it would use the class AlphabeticComparator? All methods except the sortAlphabetically works. And how to capture "l.toArray(mRecords)" from organize() so that I could use it also in sortAlphabetically()?
------------------------------------------------------- /** Class CDRack represents collections of compact discs. Discs are located in the rack in slots numbered from zero upwards. The discs are represented by Record objects and empty slots by null values. */
public class CDRack extends Object {
private Record[] mRecords; private int size;
/**Creates a new, empty CD rack. Parameters: size - the size of the new rack, i.e. the number of slots it has */ public CDRack(int size) { mRecords = new Record[size]; this.size = size; }
/** "Organizes" the discs in the rack so that they will be located in consecutive slots starting at slot number zero, and any and all empty slots will be at the "end of the rack". After calling this method, the discs are in an undefined (i.e. arbitrary) order - the only thing this method guarantees is that there aren't any empty slots in between full ones. */
public void organize() {
// Turn array into a list - more flexible List l = Arrays.asList(aanilevyt);
// Remove all nulls from a copy of the list which supports removal. l = new ArrayList(l); while (l.remove(null)) /*do nothing*/;
// Clear the original array. for (int i = 0; i < mRecords.length; i++){ mRecords<i> = null; } // Put the non-nulls back.
l.toArray(mRecords); }
/**"Organizes" the discs in the rack to the beginning of the rack (see the method organize) and sorts them in alphabetical order. Recordings by the same artist are placed in alphabetical order by disc name. */ public void sortAlphabetically() { }