简体   繁体   English

错误:找不到符号以及如何创建对象并将其添加到 arraylist

[英]error: cannot find symbol and how to create and add objects to an arraylist

I'm attempting to create objects of my Profile class and store them in an arrayList created in a ProfileCollector class I created.我正在尝试创建我的配置文件 class 的对象并将它们存储在我创建的 ProfileCollector class 中创建的 arrayList 中。

import java.util.ArrayList;

public class ProfileCollector
{
  private ArrayList<Profile> profileList;

  public ProfileCollector()
  {
    profileList = new ArrayList<Profile>();
    //peopleList = new ArrayList<String>();
  }
  public void addProfile(String initName, int initKcalTotal, int initProteinTotal, int initFatTotal){
    profileList.add(new Profile(initName, initKcalTotal, initProteinTotal, initFatTotal));

  }
}

Here is my profile class:这是我的个人资料 class:

import java.util.ArrayList;

public class Profile
{
  private ArrayList<DailyIntake> nutritionalStats;
  private String name;
  private int kcalTotal;
  private int proteinTotal;
  private int fatTotal;
  //These values represent nutritional requirements


  public Profile(String initName, int initKcalTotal, int initProteinTotal, int initFatTotal)
  {
    name = initName;
    kcalTotal = initKcalTotal;
    proteinTotal = initProteinTotal;
    fatTotal = initFatTotal;
  }

  public String getName(){
    return name;
  }
  public int getKcalTotal(){
    return kcalTotal;
  }
  public int getProteinTotal(){
    return proteinTotal;
  }
  public int getFatTotal(){
    return fatTotal;
  }

}

Here are the parts of my main.java that are important这是我的 main.java 中重要的部分

public static void main(String[] args) {
    ProfileCollector profiles = new ProfileCollector();
    //theres also a line that calls to a method which calls to another method with this line: profiles.addProfile(new Profile(name, optimumCalories, optimumProteins, optimumFats));
}

The error message is that the variable profiles is cannot be found.错误消息是找不到变量配置文件。 My question is also if I am correctly creating objects and adding them to an ArrayList.我的问题也是我是否正确创建对象并将它们添加到 ArrayList。 I didn't know if creating a class was the best way to go about this, but it was the way I've partial seen before.我不知道创建 class 是否是 go 的最佳方式,但这是我以前部分见过的方式。 Anything helps.任何事情都有帮助。

Here is the error message:这是错误消息:

error: cannot find symbol profiles.addProfile(new Profile(name, optimumCalories, optimumProteins, optimumFats));错误:找不到符号profiles.addProfile(new Profile(name,optimizeCalories,optimizeProteins,optimizeFats)); ^ symbol: variable profiles location: class Main ^ 符号:可变型材位置:class 主要

You can add a method in the ProfileCollector class, that accepts an Profile instance instead of 4 parameters.您可以在 ProfileCollector class 中添加一个方法,该方法接受一个 Profile 实例而不是 4 个参数。 With this class you should be able to be able to add a new profile to your list and you should also be able to add a profile by typing in the 4 parameters, that are required to create a new Profile instance.使用此 class,您应该能够将新配置文件添加到列表中,并且您还应该能够通过输入创建新配置文件实例所需的 4 个参数来添加配置文件。

import java.util.ArrayList;

public class ProfileCollector
{
    private ArrayList<Profile> profileList;

    public ProfileCollector()
    {
        profileList = new ArrayList<Profile>();
        //peopleList = new ArrayList<String>();
    }
    public void addProfile(String initName, int initKcalTotal, int initProteinTotal, int initFatTotal){
        profileList.add(new Profile(initName, initKcalTotal, initProteinTotal, initFatTotal));
    }

    public void addProfile(Profile profile) {
        profileList.add(profile);
    }
}

You need make sure to send by parameters the profiles variable to that function and then send it to the other function that you said it calls您需要确保通过参数将profiles变量发送到 function,然后将其发送到您所说的另一个 function

profiles.addProfile(new Profile(name, optimumCalories, optimumProteins, optimumFats));

Also you are giving a variable type Profile to the addProfile,maybe you need to send only the profile atributes此外,您正在为 addProfile 提供变量类型 Profile,也许您只需要发送配置文件属性

profiles.addProfile(name, optimumCalories, optimumProteins, optimumFats);

Or change the addProfile to have a Profile by parameters that is the best practice或者将 addProfile 更改为通过参数设置 Profile,这是最佳实践

public void addProfile(Profile profile) {
        profileList.add(profile);
    }

If this is not the answer could you add some code from that functions, if you don't want to share the code put ...如果这不是答案,您是否可以从该函数中添加一些代码,如果您不想共享放置的代码...

Both Answeres above are good.上面的两个答案都很好。

It is a compilation error so you need to learn how to interpret IDE error messages from the compiler.这是一个编译错误,因此您需要了解如何解释来自编译器的 IDE 错误消息 The problem is you are calling a method signature that does not exist.问题是您正在调用一个不存在的方法签名。

Eather define the method伊瑟定义方法

public void addProfile(Profile profile)

or call the existiong method:或调用现有方法:

profiles.addProfile("Profile1", 1650, 450, 350);

use "this" to refer to own properties in an Object.使用“this”来指代 Object 中的自己的属性。

 public void addProfile(String initName, int initKcalTotal, int initProteinTotal, int initFatTotal){
        this.profileList.add(new Profile(initName, initKcalTotal, initProteinTotal, initFatTotal));
    }

Keep it up!保持!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM