在这种情况下如何实现Comparable?

时间:2015-11-25 18:51:54

标签: java arrays comparable

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

我有一个ArrayObject class implements Comparable并导入了java.util.*;我还有一个ArrayObjectDriver类,它是一个调用我在ArrayObject类中编码的方法的主要方法。它必须能够做的一种方法是在main方法中对对象数组进行排序。我知道你必须使用.compareTo的内容,但我不确定如何在ArrayObject类中执行此操作并在驱动程序中调用它。

编辑: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;
}
}

1 个答案:

答案 0 :(得分:0)

只需在sort课程中设置ArrayObject方法即可在用户输入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();
}

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