添加max方法返回列表中的最大元素

问题描述:

我们的教师告诉我们添加一个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我是否需要添加另一个最大方法?既然已经有一个方法返回最大尺寸 –

我想你错过了你提供的代码。

maxListSize()实际上是可以构建的最大的数组的大小(它只是一个getter,给出了maxSize属性)。

你的教练要你写给你包含数组中最大元素,你的阵列,无论大小的方法。你必须定义:“我怎么能说这个元素比另一个元素大?” 然后写你的代码,你就完成了。