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

时间:2016-02-26 12:28:17

标签: java list arraylist

我们的讲师告诉我们添加一个max方法来返回列表中最大的元素并写出方法max的定义,问题是因为它已经有一个maxListSize,它返回我需要的列表的最大大小再次使用aa 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);
}

1 个答案:

答案 0 :(得分:1)

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

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

您的教师希望您编写一个方法,使您的数组中包含最大元素,而不管数组的大小。你必须定义:“我怎么能说这个元素比另一个元素大?” 然后编写代码就完成了。