需要比较ArrayList中的对象以返回最高值

时间:2017-11-03 13:06:05

标签: java object arraylist compare

在这个项目中(使用BlueJ,因为我是初学者)我希望将Climbers添加到具有名称,性别和年龄的ArrayList中。登山者可以添加他们爬过的山峰。以山名和身高。

我需要在Climber类中添加一个方法来返回特定登山者爬过的最高峰。

要定义方法,我是否使用

定义它
public ArrayList<Mountain> getHighestMountain(Mountain mountainHeight)

我不确定如何检查山地类中的对象进行比较。仅供参考我尚未学习比较关键词,所以我希望不要使用它。

俱乐部课程:

import java.util.ArrayList;
import java.util.Scanner;
/**
 * Write a description of class Club here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Club
{
    // An ArrayList for storing climber details.
    private ArrayList<Climber> climbers;

    /**
    * Constructor for objects of class Club
    */
    public Club()
   {
       // Initialise instance variables.
       climbers = new ArrayList<Climber>();
    }

    public void addClimber(Climber newName)
    {
        climbers.add(newName);
    }


   public Climber getClimber(String name)
   {
       Climber foundClimber = null;
       int index = 0;
       boolean searching = true;

       while(searching && index < climbers.size()) {
           Climber climber = climbers.get(index);
           if(climber.getName().equals(name)) {
                   searching = false;
                   foundClimber = climber;
                }
                else {
                    System.out.println(name + " not found");
                    index++;
                }
            }
            return foundClimber;
        }

    public void displayClimberList()
    {
        for (int item = 0; item<climbers.size();
        item++) {
            Climber climber = climbers.get(item);
            System.out.println(climber.getName() + (" ") + climber.getAge() + (" ") 
            + climber.getGender());
        }
    }

}

登山者课程:

import java.util.ArrayList;
/**
 * Write a description of class Climber here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Climber
{
    // Instance variables.
    // The climber name.
    private String name;
    // The climber age
    private int age;
    // The climber gender.
    private String gender;
    private ArrayList<Mountain> mountains;


    /**
     * Constructor for objects of class Climber
     */
    public Climber (String newName, int newAge, String newGender)
    {
        // Initialise instance variables.
        name = newName;
        age = newAge;
        gender = newGender;
        mountains = new ArrayList<Mountain>();

    }

    /**
     * Accessor method for climber's name.
     */
    public String getName()
    {
        return name;
    }

    /**
     * Set the climber's name.
     */
    public void setName(String newName)
    {
        name = newName;
    }

    /**
     * Accessor method for climber's age.
     */
    public int getAge()
    {
        return age;

    }

    /**
     * Set the climber's age.
     */
    public void setAge(int newAge)
    {
        age = newAge;

    }

     /**
     * Set the climer's gender.
     */
   public String getGender()
   {
       return gender;
   } 

   /**
     * Accessor method for climber's gender.
     */
    public void getGender(String newGender)
    {
        gender = newGender;

    }

   public Mountain addMountain(Mountain mountain)
   {
       return mountain;
    } 

   public ArrayList<Mountain> getHighestMountain(Mountain mountainHeight)
   {
       double maxHeight = 1;
       int index = 1;
       for(int i = 0; i < mountainHeight.length; i++) {
           if(mountainHeight[i].getHeight()>maxHeight) {
               index = i;
            }
        }




    }
}

山地类:

/**
 * Write a description of class Mountain here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Mountain
{
    // Instance variables.
    private double height;
    private String name;

    /**
     * Constructor for objects of class Mountain
     */
    public Mountain(String mountainName, double mountainHeight)
    {
        // Initialise instance variables
        name = mountainName;
        height = mountainHeight;
    }

    /**
     * Accessor method for mountain name.
     */
    public String getName()
    {
        return name;
    }

    /**
     * Set the mountain name.
     */
    public void setName(String mountainName)
    {
        name = mountainName;
    }

    /**
     * Accessor method for mountain height.
     */
    public double getHeight()
    {
        // put your code here
        return height;
    }

    /**
     * Set the mountain height.
     */
    public void setHeight(double newHeight)
    {
        height = mountainHeight;
    }
}

我试图返回此值,但您可以看到它不完整。

提前致谢!

2 个答案:

答案 0 :(得分:0)

<强>评论

删除你的评论,他们没有添加任何价值。不要在代码中放置或保留默认注释,这会降低可读性。还应该避免澄清,尝试创建一个具有有意义名称的单独变量或方法。例如:

// check if the user is on the last page
if (!((count - i) < pageSize)) {
    addNextButton();
}

可能成为:

boolean userIsOnTheLastPage = (count - i) < pageSize;
if (!userIsOnTheLastPage) {
    addNextButton();
}

<强>接口

Java集合(如ArrayList)实现接口(例如List)。通常,您需要声明并传递接口,而不是您使用的实现。代码的其他部分应该只关心使用List功能,而不是如何实现它们。

所以替换;

private ArrayList<Climber> climbers;

private List<Climber> climbers;

也可以在方法中使用接口作为参数或返回类型。这种方法的结果是,如果您希望稍后使用不同的List实现,则只需在代码中的一个位置更改它。这是一种在代码中解耦的形式,这是一件好事。 (&#39;低耦合,高凝聚力&#39;)

<强> getClimber

您在getClimber中的搜索实现有点复杂。我这样做:

for (Climber climber : climbers) {
    if(climber.getName().equals(name)) {
        return climber;
    }   
}       
System.out.println(name + " not found");

我知道你可能会试图避免从迭代中返回,但在我看来,代码的可读性会大大降低。 顺便说一句,你的代码会被打印出来,而不是找不到&#39;对于每个不匹配的登山者。我认为这不是你想要的,所以如果找不到匹配的登山者,我的代码只打印一次。

<强> displayClimberList

public void displayClimbers() {
    for (Climber climber : climbers) {
        System.out.println(climber);
    }
}

System.out.println在其参数上调用toString(),因此我们可以将显示字符串格式化为Climber类:

public class Climber {

    private String name;
    private int age;
    private String gender;

    // ...

    public String toString() {
        return String.format("%s %s %s", name, age, gender);
    }
  • 如果不需要,请不要在变量/方法名称中提及类型。从方法签名中可以明显看出返回List的事实。
  • foreach在索引迭代时更具可读性,所以尽可能使用它。有时您无法避免使用索引,例如同时迭代两个列表但这不是这种情况。
  • printf(或String.format)使得输出字符串的格式化比字符串连接更容易。请参阅documentation of java.util.Formatter

<强>性别

我会将性别字段从String更改为枚举:

public class Climber {

    private Gender gender;

枚举可能如下所示:

public enum Gender {
    MALE, FEMALE;
}

(这是一个单独的顶级类,所以在一个名为Gender.java的文件中)。另请参阅the Oracle tutorial about enums

<强> addMountain

修复addMountain方法,使其实际添加一个山:

public void addMountain(Mountain mountain) {
    mountains.add(mountain);
} 

<强> getHighestMountain

getHighestMountain的逻辑是有缺陷的(这是你的问题),试试这个:

public List<Mountain> getHighestMountain() {
    Mountain highest = null;
    for(Mountain mountain : mountains) {
        if(highest == null || mountain.getHeight() > highest.getHeight()) {
            highest = mountain;
        }
     }
     return highest;
}

答案 1 :(得分:0)

变化:

//This does absolutely nothing. It takes a parameter and returns it.
public Mountain addMountain(Mountain mountain) {
    return mountain;
} 

public ArrayList<Mountain> getHighestMountain(Mountain mountainHeight) {
    double maxHeight = 1;
    int index = 1;
    for(int i = 0; i < mountainHeight.length; i++) {
        if(mountainHeight[i].getHeight()>maxHeight) {
            index = i;
        }
    }
}

要:

//This adds the parameter mountain to the list of mountains for this climber
public void addMountain(Mountain mountain) {
    mountains.add(mountain);
} 

//This loops over the mountains for this climber and returns the highest one. 
public Mountain getHighestMountain() {
    Mountain highestMountain = null;
    for(int i = 0; i < mountains.size(); i++) {
        if (highestMountain == null) {
            highestMountain = mountains.get(i);
        }
        else if(mountains.get(i).getHeight() > highestMountain.getHeight()) {
            highestMountain = mounts.get(i);
        }
    }
    return highestMountain;
}

您的代码示例显示缺乏对基本概念的理解,例如字段和变量,参数和返回类型等。我建议阅读语言基础知识here