gpt4 book ai didi

java - 将对象添加到数组列表 ArrayList

转载 作者:行者123 更新时间:2023-12-02 08:53:27 24 4
gpt4 key购买 nike

所以我在尝试弄清楚如何将新对象添加到数组列表中时遇到了麻烦,我们正在制作一个健身应用程序,现在这就是我的 DailyExercise

import java.util.ArrayList;

/**
* (10pts) This class represents the daily exercise plan. It has the list of
* Fitness exercises the user plans to do, the duration s/he willing to
* workout, and the targeted calorie loss for the day. The implementation of
* this class will most likely involve the use of some kind of ArrayList to
* store all of the Fitnesses for the daily workout. In addition, this class
* must contain:
*
* @author jaybu
*
*/

public class DailyExercise {

ArrayList<Fitness> exerciseList = new ArrayList<Fitness>();
private int duration;
private double calorieTarget;
Profile profile;

/**
* A constructor which accepts the list of exercises,
* number of minutes to workout and the amount of calories to be burnt.
* @param exerciseList
* @param duration
* @param calorieTarget
* @param profile
*/
public DailyExercise(ArrayList<Fitness> exerciseList, int duration, double calorieTarget, Profile profile) {
exerciseList = new ArrayList<Fitness>();
this.duration = duration;
this.calorieTarget = calorieTarget;
this.profile = profile;
}



/**
* A constructor which sets duration to 1 hour and calorieTarget to 500.
* @param exerciseList
* @param profile
*/

public DailyExercise(ArrayList<Fitness> exerciseList, Profile profile) {
this.exerciseList = exerciseList;
this.duration = 60;
this.calorieTarget = 500.00;
this.profile = profile;
}


/**
* add a new Fitness in the exerciseList.
* @param ex
*/
public void addExercise(Fitness ex) {
exerciseList.add(ex);

}


/**
* removes an Exercise from the exerciseList.If the exercise does not
* exist, it will leave the exerciseList unchanged.
* @param ex
*/
public void removeExercise(Fitness ex) {

}

//********************************************设置者******** ******************************//

/**
* A setter method which sets the exerciseList of the DailyExercise.
* @param list
*/
public void setExercise(ArrayList<Fitness> list) {
this.exerciseList = list;
}


/**
* A setter method which sets the duration of the DailyExercise.
* @param duration
*/
public void setDuration(int duration) {
this.duration = duration;
}




/**
* A setter method which sets the amount of calorie to be burnt of the
* DailyExercise.
* @param target
*/
public void setCalorieTarget(double target) {
this.calorieTarget = target;
}



/**
* A setter method which sets the profile of the user.
* @param profile
*/
public void setProfile(Profile profile) {
this.profile = profile;
}

//******************************************** setter/getter ******** ******************************//

/**
* A getter method which returns the exerciseList of the DailyExercise.
* @return
*/
public ArrayList<Fitness> getExerciseList(){
return exerciseList;
}



/**
* A getter method which returns the duration of the DailyExercise.
* @return
*/
public int getDuration() {
return duration;
}



/**
* A getter method which returns the amount of calorie to be burnt of
* the DailyExercise.
* @return
*/
public double getCalorieTarget() {
return calorieTarget;
}



/**
* A getter method which returns the profile of the user.
* @return
*/
public Profile getProfile() {
return profile;
}



/**
* returns an array of Fitness exercises from the exerciseList that
* fullfills all the target muscle groups (targetMuscle) the user wants
* to work on for that specific day. The method will return null if
* there is no exercise that targets all the muscle groups.
* @param targetMuscle
* @return
*/
public Fitness[] getExercises(Muscle[] targetMuscle) {
return null;
}
}

问题:如何完成 addExercise(Fitness ex)removeExercise(Fitness ex) 方法?

最佳答案

addExercise() 方法看起来不错吗?

您可以使用 Iterator 在 removeExercise() 方法中删除特定的健身锻炼 -

public void removeExercise(Fitness ex) {
Iterator itr = exerciseList.iterator();
while (itr.hasNext()) {
Fitness fitness = (Fitness) itr.next();
if (fitness.equals(ex))
itr.remove();
}
}
PS。重写 equals() 方法以正确识别您的 Fitness 对象。

关于java - 将对象添加到数组列表 ArrayList<Fitness>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60628842/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com