2015-11-25 20 views
-3

我查了一下关于Comparable是如何工作的例子,我有点理解它是如何工作的,但我不知道如何在这种情况下使用它。在这种情况下你如何实现Comparable?

我有一个ArrayObject class implements Comparable和进口java.util.*;我也有一个ArrayObjectDriver类,它是在我的ArrayObject类编码的方法调用一个主要方法。它必须能够做的一个方法是在主方法中对对象数组进行排序。我知道你必须使用.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; 
} 
} 
+0

'ArrayObject'类是什么样的? – gonzo

+1

......同样重要的是,什么时候一个'ArrayObject'是“大于”另一个的定义? – yshavit

+0

@gonzo它有一个默认的构造函数,例如将一个对象添加到数组中 – immmjimmy

回答

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有用的方法。