java - ArrayList内容访问后变成 'null'?

标签 java arraylist

我有一个对象 Activity初始化时,看起来像 Activity a = new Activity(String activityName, double calories) 。我有另一个对象,ActivityPerformed初始化时,看起来像 ActivityPerformed ap = new ActivityPerformed(Activity a, double hours )。我也有两个ArrayLists ( ActivityActivityPerformed 的数组)和 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>时,所有输入的 Activity 都在那里,但是当我打印 ArrayList<ActivityPerformed> 时它总是打印 double ( calorieshours )为 0.0,而 activityName为 null 但它打印了正确的项目数。

这是我添加到 ArrayList<ActivityPerformed> 的代码

/**
 * 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)); 
}

这是打印 ArrayList 的代码(这适用于 ArrayList,但也许它缺少我忽略的东西)

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;
}

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

最佳答案

罪魁祸首是 ActivityPerformed 构造函数中的这一行

 a = new Activity(name, calories);

它应该将传递的 Activity 分配给 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;

希望这有帮助;

关于java - ArrayList内容访问后变成 'null'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22123987/

相关文章:

java - Java 的 XMLEncoder 可以序列化具有不可序列化字段的对象吗?

java - 多线程同时向非同步的ArrayList对象添加元素可能会导致什么问题?

java - 从手机发送ArrayList<> DataItem到穿戴设备

java - 选择 TableView 行时启用禁用按钮

java - Play 2.3 验证 Json 请求体

java - 如何使用apache commons vfs从Windows读取远程文件到Linux?

java - Firebase 实时数据库不更新子项

java - 当文本上的 Edittext 更改时,Android 列表(RecyclerView 适配器)正在更改

java - 在 Java 的方法中将 Collection 的 Collection 联合成 Collection

java - 如何检测一个元素是否是数组中的最后一个元素?