Java ArrayList内容变为';空';进入时?

Java ArrayList内容变为';空';进入时?,java,arraylist,Java,Arraylist,我有一个对象Activity,初始化时看起来像Activity a=new Activity(String activityName,double carries)。我还有另一个对象,ActivityPerformed,它在初始化时看起来像ActivityPerformed ap=newactivityperformed(活动a,双小时)。我还有两个ArrayLists(一个Activity和ActivityPerformed的数组),并且ArrayList依赖于Activity中的信息,这就是

我有一个对象
Activity
,初始化时看起来像
Activity a=new Activity(String activityName,double carries)
。我还有另一个对象,
ActivityPerformed
,它在初始化时看起来像
ActivityPerformed ap=newactivityperformed(活动a,双小时
)。我还有两个
ArrayLists
(一个
Activity
ActivityPerformed
的数组),并且
ArrayList
依赖于
Activity
中的信息,这就是为什么它必须首先通过该数组

因此,基本上,信息流将理想地使其等效于:

Activity a = new Activity(String activityName, double calories);
ActivityPerformed ap = new Activity(String activityName, double calories, 
                                 double hours);
因为添加到
ArrayList
需要使用其信息的
活动
(这就是为什么
ActivityFormed
使用
ActivityA
而不是
String activityName,双倍卡路里

如何使信息正确传递?当我打印
ArrayList
时,所有输入的活动都在那里,但是当我打印
ArrayList
时,它总是将
双倍的
(卡路里
小时数
)打印为0.0,
activityName
打印为空,但它会打印正确的项目数

这是我添加到
ArrayList

这是打印ArrayList的代码(这在ArrayList上有效,但可能它缺少了我忽略的东西)

在ActivityPerformed.class下:

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
ArrayList
获取活动,并向其附加一个额外字段,使其不打印
null

罪魁祸首是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;

希望这有帮助

看起来您没有将名称、卡路里等传递给ActivityPerformed构造函数。 您传递了整个活动a,然后在构造函数中传递它。 最好是:

公共类ActivityFormed{

Activity a;
double hours;

public ActivityPerformed (Activity a, double hours) {

    this.a = a;
    this.hours = hours;

}

}

很难说出你想做什么,但这不可能是正确的:

public ActivityPerformed(Activity a, double hours){
    a = new Activity(name, calories);
    this.hours = hours;
    this.name = name;
    this.calories = calories;
}
创建新的
ActivityPerformed
时,必须将
活动
传递给构造函数;为此,您必须已经创建了一个
新活动
。相反,上面的代码将该
活动
丢弃,创建另一个
新活动
,然后将该新
活动
分配到参数
a
,在该参数中它也将变得无用(
a
通过值传递;因此分配给它不会影响方法之外的任何内容)

如果希望
ActivityPerformed
引用
Activity
,则需要在
ActivityPerformed
中去掉字段以引用它。然后在构造函数中分配它:

public class ActivityPerformed {

    Activity activity;
    double hours;

    public ActivityPerformed(Activity a, double hours) { 
        activity = a;       // sets up the reference to the Activity
        this.hours = hours;
        //this.name = name;  I don't know what this would have done, since "name"
        //     and "calories" weren't parameters to the constructor.  Most likely,
        //     this was a useless statement that assigned a field to itself.  Do you
        //     need to store these fields in the ActivityPerformed, or do you just
        //     want to use the "activity" reference to get to them?  
        //this.calories = calories;

总之,有很多信息丢失了,我不知道你想做什么。但这应该是一个开始。它可能不会解决所有问题。

顶部的示例中说
ActivityPerformed=newactivity(stringactivityname…)
;这是打字错误吗?
ActivityPerformed=新活动(字符串activityName,双倍卡路里,双倍小时)不是有效的Java。根据您的描述,假设
ActivityPerformed
是一个类名,那么它和等号之间缺少一个标识符。然后,分配本身是不好的,因为您不能将
活动
引用分配给
活动类型格式的
引用。@ajb不,这只是我希望信息如何从活动传递到活动类型格式的想法,有点像我希望发生的事情的要点(但不是将它们分开)
public ActivityPerformed
显示我的实际代码。@JimGarrison噢,忘了添加“ap”。修正了,但这不是我正在使用的代码,只是我想要发生的想法。谢谢你向我解释,我真的很感激。下面的Sanjeev向我展示了如何从活动中获得名称和热量(这是我真正需要的),但这个解释为我澄清了一切!
public ActivityPerformed(Activity a, double hours){
    a = new Activity(name, calories);
    this.hours = hours;
    this.name = name;
    this.calories = calories;
}
public class ActivityPerformed {

    Activity activity;
    double hours;

    public ActivityPerformed(Activity a, double hours) { 
        activity = a;       // sets up the reference to the Activity
        this.hours = hours;
        //this.name = name;  I don't know what this would have done, since "name"
        //     and "calories" weren't parameters to the constructor.  Most likely,
        //     this was a useless statement that assigned a field to itself.  Do you
        //     need to store these fields in the ActivityPerformed, or do you just
        //     want to use the "activity" reference to get to them?  
        //this.calories = calories;