首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向arrylist ArrayList<Fitness>添加对象

向arrylist ArrayList<Fitness>添加对象
EN

Stack Overflow用户
提问于 2020-03-11 03:25:02
回答 2查看 304关注 0票数 0

因此,我很难想出如何将新对象添加到数组列表中,我们正在制作一个健身应用程序,现在我已经为我们的DailyExercise类提供了这个

代码语言:javascript
复制
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) {

}

//*************************************SETTERS*************************************//

代码语言:javascript
复制
/**
 * 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*************************************//

代码语言:javascript
复制
/**
 * 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)方法?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-03-11 03:57:03

addExercise()方法看起来还好吗?

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

代码语言:javascript
复制
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对象。

票数 1
EN

Stack Overflow用户

发布于 2020-03-11 03:54:18

在Fitness类中,重写等于()方法,并在其中给出与唯一字段(通常为ID )的比较,以便可以唯一地标识对象。然后在您的removeExercise方法中编写

代码语言:javascript
复制
int indx = exerciseList.indexOf(ex);
// Instead of indexOf you can also use contains method, which will give true/false. I suggested indexOf thinking you might need to update any observer about the removed indx.
if (indx > -1){
 // remove object from the index 
 exerciseList.remove(ex)
 // do your work.
} 

addExercise方法看起来不错。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60628842

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档