简体   繁体   中英

How do you implement Comparable in this situation?

I've looked up examples as to how Comparable works and I somewhat understand how it would work but I don't know how I would use it in this situation.

I have an ArrayObject class implements Comparable and imported java.util.*; I also have an ArrayObjectDriver class that is a main method that calls upon methods I've coded in the ArrayObject class. One of the methods that it has to be able to do is to sort the array of objects in the main method. I know you have to use something with .compareTo but I'm not sure how I would do that in the ArrayObject class and call on it in the driver.

EDIT: ArrayObject code

public class ArrayObject implements Comparable
{
private Object[] arr;
private int actualSize;
ArrayObject()
{
    arr = new Object[10];
    actualSize = 0;
}

ArrayObject(int size)
{
    arr = new Object[size];
    actualSize = 0;
}

public void add(Object obj)
{
    if(actualSize>=arr.length)
        expandArray();
    arr[actualSize]=obj;
    actualSize++;
}

public void expandArray()
{
    int newSize = arr.length*2;
    Object[] biggerList = new Object[newSize];
    for(int i=0; i<arr.length; i++)
    {
        biggerList[i] = arr[i];
    }
    arr = biggerList;
}

public void add(Object obj, int index)
{
    if(index<actualSize)
    {
        shiftRight(index);
        arr[index]=obj;
        actualSize++;
    }
    // index is between [0 and actualSize)
}

private void shiftRight(int start)
{
    for(int i=actualSize; i>start; i--)
    {
        arr[i] = arr[i-1];
    }
    arr[start]=null;
}

private void shiftLeft(int start)
{
    for(int i=start; i<actualSize-1; i++)
    {
        arr[i] = arr[i+1];
    }
    arr[actualSize-1]=null;
}

public Object remove(int index)
{   // returning the object you are removing
    // ("the", "is", "are")
    if(index>=0&&index<actualSize)
    {
        Object obj = arr[index];
        arr[index] = null;
        // arr[index] = arr[actualSize-1];
        // what to do about the null?
        // Shift to the left by one
        shiftLeft(index);
        actualSize--;
        // ("the", null, "are")
        return obj;
    }
    return null;
}

public Object get(int index)
{
    if(index>=0&&index<actualSize)
        return arr[index];
    return null;
}

public int sizeOfContainer()
{
    return arr.length;
}

public int items()
{
    return actualSize;
}

public boolean searchArray(Object obj)
{
    for(int i=0; i<actualSize; i++)
    {
        if(arr[i].equals(obj))
            return true;
    }
    return false;
}

public int findObject(Object obj)
{
    if(searchArray(obj))
    {
        for(int i=0; i<actualSize; i++)
        {
            if(arr[i].equals(obj))
                return i;
        }
    }
    return -1;
}

public boolean isItEmpty()
{
    if(actualSize == 0)
        return true;
    return false;
}

public int removeSearch(Object obj)
{
    for(int i=0; i<actualSize; i++)
    {
        if(arr[i].equals(obj))
        {
            remove(i);
            return i;
        }
    }
    return -1;
}

public void clearArray()
{
    for(int i=0; i<actualSize; i++)
        arr[i] = remove(i);
}

public void printArr()
{
    System.out.println("Array Size: " + actualSize);
    for(int i=0; i<actualSize; i++)
    {
        System.out.println(arr[i]);
    }
}
}

ArrayObjectDriver code

public class ArrayObjectDriver
{
private static Scanner scanner;
public static void main(String[] args)
{
    scanner = new Scanner(System.in);
    ArrayObject array = new ArrayObject();
    int selection = selectionMenu();
    while(selection > 0)
    {
        if(selection == 1)
        {
            System.out.println("Enter your object: ");
            String str = scanner.next();
            array.add(str);
        }
        else if(selection == 2)
        {
            System.out.println("Enter your object: ");
            String str = scanner.next();
            System.out.println("Enter location: ");
            int n = scanner.nextInt();
            array.add(str, n);
        }
        else if(selection == 3)
        {
            System.out.println("Enter location: ");
            int n1 = scanner.nextInt();
            array.remove(n1);
        }
        else if(selection == 4)
        {
            System.out.println("Enter object: ");
            String str = scanner.next();
            int i = array.removeSearch(str);
            System.out.println("Object removed at index " + i);
        }
        else if(selection == 5)
        {
            array.clearArray();
            System.out.println("Cleared Array");
        }
        else if(selection == 6)
        {
            System.out.println("Enter object: ");
            String str = scanner.next();
            boolean result = array.searchArray(str);
            System.out.println("The object was found: " + result);
        }
        else if(selection == 7)
        {
            boolean result = array.isItEmpty();
            System.out.println("It is empty: + result);
        }
        else if(selection == 8)
        {
            array.expandArray();
            int i = array.sizeOfContainer();
            System.out.println("The new size of the array is: " + i);
        }
        else if(selection == 9)
        {
            System.out.println("Enter object: ")
            String str = scanner.next();
            int i = array.findObject(str);
            System.out.println("The object was found at index " + i);
        }
        else if(selection == 10)
            array.printArr();
        else if(selection == 11)
        {

        }
        else if(selection == 12)
            System.exit(0);
        System.out.println("");
        selection = selectionMenu();
    }
}

private static int selectionMenu()
{
    System.out.println("Menu: ");
    System.out.println("1. Add object to the end of the list");
    System.out.println("2. Add object at a specific location");
    System.out.println("3. Remove specific object at a location");
    System.out.println("4. Remove specific object that matches name");
    System.out.println("5. Empty the array");
    System.out.println("6. See if the array contains a certain object");
    System.out.println("7. See if the array is empty"); 
    System.out.println("8. Expand your array");
    System.out.println("9. Search for an item");
    System.out.println("10. Print array");
    System.out.println("11. Sort array");
    System.out.println("12. Exit");
    System.out.println("Enter option: ");
    int optionNumber = scanner.nextInt();
    return optionNumber;
}
}

Just make a sort method in your ArrayObject class and you can call that when user enters 11 .

In ArraysObject :

public void sort(){
    Arrays.sort(arr); //This is all you have to call to sort your array arr
}

In your ArrayObjectDriver :

else if(selection == 11)
{
    array.sort();
}

The Arrays class has useful methods for searching, manipulating, and sorting arrays including Arrays.sort and Arrays.binarySearch .

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