我有一个对象Activity
,在初始化时看起来像Activity a = new Activity(String activityName, double calories)
。我有另一个对象ActivityPerformed
,初始化时看起来像ActivityPerformed ap = new ActivityPerformed(Activity a, double hours
)。我还有两个ArrayLists
(一组Activity
和ActivityPerformed
)和ArrayList<ActivityPerformed>
依赖于Activity
中的信息,这就是为什么它必须通过通过第一个。
因此,理想情况下,信息流基本上等同于此:
Activity a = new Activity(String activityName, double calories);
ActivityPerformed ap = new Activity(String activityName, double calories,
double hours);
因为添加到ArrayList<ActivityPerformed>
需要使用其信息的Activity
(这就是ActivityPerformed
使用Activity a
代替String activityName, double calories
的原因)。
如何正确传递信息?当我打印ArrayList<Activity>
时,所有输入的活动都在那里,但是当我打印ArrayList<ActivityPerformed>
时,它始终会打印double
(两者 calories
和{ {1}})为0.0,hours
为null但是它会打印正确数量的项目。
这是我添加到activityName
ArrayList<ActivityPerformed>
这是打印ArrayList的代码(这适用于ArrayList,但也许它缺少我忽略的东西)
/**
* Searches for the Activity record associated with the given name
*
* @param name the name of the Activity record to look for
* @return the Activity record, or null if it does not exist
*/
public Activity getActivity(String name)
{
for(Activity a : activityBase) {
if(a.getName().equals(name))
return a;
}
return null;
}
/**
* Track that a given kind of activity has been performed for a given number
* of hours. If the given name does not exist in this ActivityBase, or a
* negative number of hours is given, an error message will appear.
*
* @param name the name of the activity performed
* @param hours the number of hours that the activity has been performed
*/
public void trackActivity(String name, double hours)
{
Activity a = getActivity(name);
activityPerformed.add(new ActivityPerformed(a, hours));
}
在ActivityPerformed.class下:
public String getActivityPerformed() {
String done = "";
for(ActivityPerformed a : activityPerformed) {
done += a.getHours();
done += " hours of ";
done += a.getName();
done += ", ";
done += a.getTotalCalories();
done += " calories.\n";
}
return done;
}
那么如何让public ActivityPerformed(Activity a, double hours){
a = new Activity(name, calories);
this.hours = hours;
this.name = name;
this.calories = calories;
}
public String getName(){
return this.name;
}
public double getTotalCalories(){
return this.calories;
}
public double getTotalCalories(){
return this.calories;
}
从ArrayList<ActivityPerformed>
获取活动并为其添加一个额外字段,并使其不打印ArrayList<Activity>
?
答案 0 :(得分:2)
Culprit是ActivityPerformed构造函数中的这一行
a = new Activity(name, calories);
它应该将传递的活动分配给var a
this.a = a;
this.name = a.getName(); // you do not need this as Activity has this information
this.calories = a.getCalories(); // you do not need this as Activity has this information
this.hours = hours;
希望这会有所帮助;
答案 1 :(得分:0)
看起来您没有将名称,卡路里等传递给ActivityPerformed构造函数。 您传递了整个活动a,然后在构造函数中将其ovverrode。 最好这样做:
公共类ActivityPerformed {
Activity a;
double hours;
public ActivityPerformed (Activity a, double hours) {
this.a = a;
this.hours = hours;
}
}
答案 2 :(得分:0)
很难说出你想要做什么,但这不可能是正确的:
public ActivityPerformed(Activity a, double hours){
a = new Activity(name, calories);
this.hours = hours;
this.name = name;
this.calories = calories;
}
创建新Activity
时,您必须将ActivityPerformed
传递给构造函数;要做到这一点,你必须已经创建了new Activity
。相反,上面的代码会抛弃Activity
,创建另一个 new Activity
,然后将新的Activity
分配到参数 { {1}},它也将变得无用(a
按值传递;因此分配给它不会影响方法之外的任何内容。)
如果您希望a
引用ActivityPerformed
,则需要在Activity
中对某个字段进行裁减才能引用该字段。然后在构造函数中分配它:
ActivityPerformed
无论如何,有很多信息缺失,我无法弄清楚你想要做什么。但这应该是一个开始。它可能无法解决所有问题。