Java 添加max方法以返回列表中最大的元素
我们的指导老师告诉我们添加一个max方法来返回列表中最大的元素,并编写方法max的定义,问题是因为它已经有一个maxListSize,它返回列表的最大大小,我需要再次添加一个max方法吗Java 添加max方法以返回列表中最大的元素,java,list,arraylist,Java,List,Arraylist,我们的指导老师告诉我们添加一个max方法来返回列表中最大的元素,并编写方法max的定义,问题是因为它已经有一个maxListSize,它返回列表的最大大小,我需要再次添加一个max方法吗 public abstract class ArrayListClass { protected int length; protected int maxSize; protected DataElement[] list; public ArrayListClass(){
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);
}
我认为你误解了你提供的代码 maxListSize实际上是可以构建的最大数组的大小:它包含的元素数只是一个getter,给出了maxSize属性 讲师希望您编写一个方法,给出数组中包含的最大元素,而不考虑数组的大小。你必须定义:我怎么能说这个元素比另一个元素大?
然后编写代码,就完成了。我不理解这个问题。你能详细说明一下吗?所以你不能完成家庭作业。我可以告诉你,这个方法很容易写。您只需遍历元素,并保留另一个变量,这样可以保存最大值。当您完成对列表的迭代时,您将拥有max size元素。问题还在于,它已经有了一个maxListSize来返回列表的最大大小。意思是?@nicomp我需要添加另一个max方法吗?因为已经有一个方法返回最大值size@Ralph.D我假设您的max方法应该返回最大数据元素,但您应该询问设置赋值的人。@Ralph.D否,max方法尚未返回此数据元素。maxListSize返回列表的最大大小,而不是最大的元素。由于你不理解作业内容,你应该向你的导师要求澄清。你刚刚把它扔在这里了。