在这种情况下,您如何实施 Comparable?

How do you implement Comparable in this situation?

我查看了有关 Comparable 如何工作的示例,我有点了解它的工作原理,但我不知道在这种情况下我将如何使用它。

我有一个 ArrayObject class implements Comparable 并导入了 java.util.*; 我还有一个 ArrayObjectDriver class 这是调用我在 ArrayObject class。它必须能够执行的方法之一是在 main 方法中对对象数组进行排序。我知道你必须使用 .compareTo 的东西,但我不确定我将如何在 ArrayObject class 中做到这一点并在驱动程序中调用它。

编辑:ArrayObject 代码

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代码

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;
}
}

只需在您的 ArrayObject class 中创建一个 sort 方法,您就可以在用户输入 11 时调用它。

ArraysObject中:

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

在你的ArrayObjectDriver中:

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

数组 class 具有用于搜索、操作和排序数组的有用方法,包括 Arrays.sortArrays.binarySearch