2016-02-26 128 views
-4

我們的教師告訴我們添加一個max方法來返回列表中最大的元素並寫入方法max的定義,問題是因爲它已經有一個maxListSize,它返回列表的最大尺寸是否需要再次使用aa max方法?添加max方法返回列表中的最大元素

public abstract class ArrayListClass { 
    protected int length; 
    protected int maxSize; 
    protected DataElement[] list; 

    public ArrayListClass(){ 
     length = 0; 
     maxSize = 100; 
     list = new DataElement[maxSize]; 
    } 

    public ArrayListClass(int size){ 
     if(size < 0){ 
      System.out.println("The array size must be positive. Creating an array of size 100..."); 
     }else{ 
      maxSize = size; 
     } 
     length = 0; 
     list = new DataElement[maxSize]; 
    } 
    //copy constructor 
    public ArrayListClass(ArrayListClass otherList){ 
     maxSize = otherList.maxSize; 
     length = otherList.length; 
     list = new DataElement[maxSize]; 

     for(int j = 0; j < length; j++){ 
      list[j] = otherList.list[j].getCopy(); 
     } 
    } 

    public boolean isEmpty(){ 
     return (length == 0); 
    } 

    public boolean isFull(){ 
     return (length == maxSize); 
    } 
    /* 
    * method that returns the number of elements 
    * in the list 
    */ 
    public int listSize(){ 
     return length; 
    } 
    /* 
    * method that returns the maximum size 
    * of the list 
    */ 
    public int maxListSize(){ 
     return maxSize; 
    } 

    /* 
    * method that prints the elements of the list 
    */ 
    public void print(){ 
     for(int index = 0; index < length; index++){ 
      System.out.print(list[index].getCopy() + " "); 
     } 
     System.out.println(); 
    } 
    /* 
    * method that determines whether an item is the 
    * same as the item in the list at the position 
    * specified by location 
    */ 
    public boolean isItemAtEqual(int location, DataElement item){ 
     return (list[location].equals(item)); 
    } 
    /* 
    * method that inserts insertItem in the list 
    * at the position specified by location 
    */ 
    public void insertAt(int location, DataElement insertItem){ 
     if(location < 0 || location >= maxSize){ 
      System.out.println("The position of the item to be inserted is out of range."); 
     }else{ 
      if(length >= maxSize){ 
       System.out.println("Cannot insert in a full list"); 
      }else{ 
       for(int index = length; index > location; index--){ 
        list[index] = list[index-1]; 
       } 
       list[location] = insertItem.getCopy(); 

       length++; 
      } 
     } 
    } 

    public void insertEnd(DataElement insertItem){ 
     if(length >= maxSize){ 
      System.out.println("Cannot insert in a full list"); 
     }else{ 
      list[length] = insertItem.getCopy(); 
      length++; 
     } 
    } 

    public void removeAt(int location){ 
     if(location < 0 || location >= length){ 
      System.out.println("The location of the item to be removed is out of range."); 
     }else{ 
      for(int index = location; index < length; index++){ 
       list[index] = list[index + 1]; 
      } 
      list[length-1] = null; 
      length--; 
     } 
    } 
    /* 
    * method that retrieves the element from the list 
    * at the position specified by location 
    */ 
    public DataElement retrieveAt(int location){ 
     if(location < 0 || location >= length){ 
      System.out.println("The location of the item to be retrieved is out of range."); 
      return null; 
     }else{ 
      return list[location].getCopy(); 
     } 
    } 

    public void replaceAt(int location, DataElement repItem){ 
     if(location < 0 || location >= length){ 
      System.out.println("The location of the item to be replaced is out of range."); 
     }else{ 
      list[location].makeCopy(repItem); 
     } 
    } 

    public void clearList(){ 
     for(int index = 0; index < length; index++){ 
      list[index] = null; 
     } 
     length = 0; 
    } 

    public void copyList(ArrayListClass otherList){ 
     if(this != otherList){ 
      for(int index = 0; index < length; index++){ 
       list[index] = null; 
      } 

      maxSize = otherList.maxSize; 
      length = otherList.length; 
      list = new DataElement[maxSize]; 
      for(int index = 0; index < length; index++){ 
       list[index] = otherList.list[index].getCopy(); 
      } 
     } 
    } 


    public abstract int seqSearch(DataElement searchItem); 

    public abstract void insert(DataElement insertItem); 

    public abstract void remove(DataElement removeItem); 

    public abstract void removeAll (DataElement removeAllItem); 
} 
+0

我不明白這個問題。你能詳細說明嗎? – nicomp

+2

SO不適合你完成作業。我可以告訴你,這個方法很容易寫。你只需要通過元素,並保留另一個變量,它可以保存最大值。當您完成迭代列表時,您將擁有最大尺寸的元素。還有什麼'問題是因爲它已經有一個maxListSize來返回列表的最大尺寸.'意思是? –

+0

@nicomp我是否需要添加另一個最大方法?既然已經有一個方法返回最大尺寸 –

回答

1

我想你錯過了你提供的代碼。

maxListSize()實際上是可以構建的最大的數組的大小(它只是一個getter,給出了maxSize屬性)。

你的教練要你寫給你包含數組中最大元素,你的陣列,無論大小的方法。你必須定義:「我怎麼能說這個元素比另一個元素大?」 然後寫你的代碼,你就完成了。