因此,我在尝试弄清楚如何向数组列表添加新对象时遇到了麻烦,我们正在制作一个适应性应用程序,现在这是我为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;
}


// ************************************* GETTERS ********** **************************** //

/**
 * 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 ArrayList <Fitness>,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60628842/

10-12 16:22