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

标签 java object arraylist

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

相关文章:

java - 将对象拆分为最基本的部分

c++ - 在不调用默认构造函数的情况下声明对象

android - 在 AutoCompleteTextview 中获取过滤后的数组大小

java - 从远程服务器运行 jsp 页面

java - 使用 Jsoup 从 HTML 标签中逐一提取文本

java - 一起使用 JDBC 和 Hibernate 4 进行 Spring 事务管理

java - Android 上的致命异常主要运行应用程序?

javascript - 无法将 javascript 中的 json api 正确打印到 html

java - 在 Java 中存储数据的最佳方式是什么? (数组与数组列表)

java - Java中数组列表元素的排序