Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在中添加新参数以使用Java ArrayList_Java_Arraylist - Fatal编程技术网

在中添加新参数以使用Java ArrayList

在中添加新参数以使用Java ArrayList,java,arraylist,Java,Arraylist,我是一名Java初学者,没有找到解决方案: public class Company{ private String name; private int age; public Company(String n, int a){ name = n; age = a; } /*get and set*/ public static void main(String[] args) { ArrayList<C

我是一名Java初学者,没有找到解决方案:

public class Company{
  private String name;
  private int age;

  public Company(String n, int a){
    name = n;
    age = a;            
  }

  /*get and set*/

  public static void main(String[] args) {
        ArrayList<Company> comp = new ArrayList<Company>();
        comp.add(new Company("Tom", 23));
        comp.add(new Company("John", 43));
        comp.add(new Company("Charles", 25));           
    }

}
<代码>公共类公司{ 私有字符串名称; 私人互联网; 上市公司(字符串n,整数a){ name=n; 年龄=a; } /*出发*/ 公共静态void main(字符串[]args){ ArrayList comp=新的ArrayList(); 公司增补(新公司(“Tom”,23)); 公司增补(新公司(“John”,43)); 公司增补(新公司(“查尔斯”,25岁)); } }
我想在for中添加参数,如
电子邮件
地址
,等等。但是
。添加
用于新元素,没有参数。

您需要阅读有关类的内容


使用
email
address
字段(1)扩展
Company
类,更新构造函数(2),并传递所需的参数(3)

如果已经有一个填充了特定字段信息的数组,则可以将
用于

final List<String> emails = Arrays.asList("first@gmail.com", "second@gmail.com");
for (final String email : emails) {
    comp.add(new Company(..., ..., ..., email));
}
final List emails=Arrays.asList(“first@gmail.com", "second@gmail.com");
用于(最终字符串电子邮件:电子邮件){
公司地址(新公司(…,…,…,电子邮件));
}

首先,您需要向类中添加变量(即每个类实例存储变量的位置,以及当前的名称或年龄)

然后,您必须修改构造函数(或创建另一个构造函数)才能获取这些参数,而且最好让getter和setter分别获取或设置这些参数,这样看起来就像这样

public class Company{
  private String name;
  private int age;
  private String email;
  private String address;

  public Company(String n, int a){
    name = n;
    age = a;            
  }

 public Company(String n, int a,String email, String address){
    name = n;
    age = a;            
    this.email = email;             //class instance object email is set to email from method parameter
   this.address = address;
  }

  /*get and set*/
  public void setEmail(String email){
    this.email=email; //explicit setter
  }

  public String getEmail(){
    return this.email;
  }

  public static void main(String[] args) {
        ArrayList<Company> comp = new ArrayList<Company>();
        comp.add(new Company("Tom", 23));  //will work, because I let your constructor
        comp.add(new Company("John", 43));
        comp.add(new Company("Charles", 25));           

        Company compToAdd= new Company("preparedFoo",22);
        compToAdd.setEmail("fooomail"); //will work because setter
        comp.add(compToAdd);           


        comp.add(new Company("FooName", 30,"fooMail","FooAddress"));   //will work because new constructor

    }

}
上市公司{
私有字符串名称;
私人互联网;
私人字符串电子邮件;
私有字符串地址;
上市公司(字符串n,整数a){
name=n;
年龄=a;
}
上市公司(字符串n、整数a、字符串电子邮件、字符串地址){
name=n;
年龄=a;
this.email=email;//类实例对象email设置为email from方法参数
this.address=地址;
}
/*出发*/
公用电子邮件(字符串电子邮件){
this.email=email;//显式setter
}
公共字符串getEmail(){
返回此电子邮件;
}
公共静态void main(字符串[]args){
ArrayList comp=新的ArrayList();
comp.add(新公司(“Tom”,23));//可以用,因为我让你的构造函数
公司增补(新公司(“John”,43));
公司增补(新公司(“查尔斯”,25岁));
compToAdd公司=新公司(“preparedFoo”,22);
compToAdd.setEmail(“fooomail”);//将起作用,因为setter
公司增加(公司增加);
comp.add(新公司(“FooName”,30,“fooMail”,“FooAddress”);//将工作,因为新的构造函数
}
}

添加这些属性(电子邮件、地址等)是的,我可以这样做,但我想知道我是否可以在for中这样做。@OscarLT你是说使用
for
循环吗?是的,拉维。存在这种可能性吗?不太清楚你要求的是什么。是关于在Company类中添加其他字段吗?如果是,只需谷歌“java类字段”。请记住:这个社区不是编程学校。你需要至少在一定程度上清楚地表达你的问题。并且你需要在发帖之前进行认真的研究。只在这里放弃你的家庭作业是不受欢迎的。注意-下面的迭代是foreach,for循环的更短键入
final List<String> emails = Arrays.asList("first@gmail.com", "second@gmail.com");
for (final String email : emails) {
    comp.add(new Company(..., ..., ..., email));
}
public class Company{
  private String name;
  private int age;
  private String email;
  private String address;

  public Company(String n, int a){
    name = n;
    age = a;            
  }

 public Company(String n, int a,String email, String address){
    name = n;
    age = a;            
    this.email = email;             //class instance object email is set to email from method parameter
   this.address = address;
  }

  /*get and set*/
  public void setEmail(String email){
    this.email=email; //explicit setter
  }

  public String getEmail(){
    return this.email;
  }

  public static void main(String[] args) {
        ArrayList<Company> comp = new ArrayList<Company>();
        comp.add(new Company("Tom", 23));  //will work, because I let your constructor
        comp.add(new Company("John", 43));
        comp.add(new Company("Charles", 25));           

        Company compToAdd= new Company("preparedFoo",22);
        compToAdd.setEmail("fooomail"); //will work because setter
        comp.add(compToAdd);           


        comp.add(new Company("FooName", 30,"fooMail","FooAddress"));   //will work because new constructor

    }

}