简体   繁体   English

在java构造函数中声明ArrayList

[英]declaring ArrayList in java Constructor

I am working on a project, and I was taught to instantiate variables in constructors. 我正在研究一个项目,并且我被教导在构造函数中实例化变量。 I'm having some trouble doing this with an ArrayList thought. 我在使用ArrayList思想时遇到了一些麻烦。 Can you suggest some best practices, do I need to define the ArrayList with the instance variables or can I do it in the constructor. 您能否提出一些最佳实践,我是否需要使用实例变量定义ArrayList,或者我可以在构造函数中执行此操作。 Thanks for your suggestions! 谢谢你的建议! I have an example of what I'm talking about below: 我有一个我正在谈论的内容的例子:

//imports
import java.util.*;
import java.lang.*;

public class ArrayListConstructorDemo
{
//instance variables/attributes

String string;
List<String> list;// for example does this line need to say List<String> list = new ArrayList<String>();

//constructors
public ArrayListConstructorDemo()
{
    String string = "null";
    List<String> list = new ArrayList<String>();//is there anyway I can do this here instead of 6 lines up?
}//end default constructor
public ArrayListConstructorDemo(String string,List<String> list)
{
    this.string = string;
    this.list = list;
}//end generic constructor

//observers/getters/accessors
 public String getString(){return string;}//end method getString()
 public List<String> getList(){return list;}//end method getList()

//transformers/setters/mutators
     public void setTable(String string){this.string = string;}
     public void setValues(String list)
     {

    //  for(String s : test) 
    //  {
            list.add(this.list);
    //  }
     }
public String toString()
{
    return "this is a generic toString method for the class ArrayListConstructorDemo";
}//end toString

public static void main(String[] args)  
{
    ArrayListConstructorDemo alcd = new ArrayListConstructorDemo();
    System.out.println(alcd.list.size());

//test Lists in general
    List<String> bleh = new ArrayList<String>();
    bleh.add("b1");
    System.out.println(bleh.get(0));
}//end method main()
}//end class ArrayListConstructorDemo

Change 更改

List<String> list = new ArrayList<String>();

to

list = new ArrayList<String>();

If you want to declare it in the constructor, then you (most likely) want to declare the outer field, so you want: 如果你想在构造函数中声明它,那么你(很可能)想要声明外部字段,所以你想要:

list = new ArrayList<String>();

Currently you are shadowing the List<String> list class variable, meaning that you are creating a new instance of list , rather than that you are initializing the list instance variable. 目前,您正在隐藏List<String> list类变量,这意味着您正在创建list实例,而不是正在初始化list实例变量。

I personally prefer initializing it at declaration time though, so what you previously had. 我个人更喜欢在申报时初始化它,所以你以前有过。 I prefer this to make the code more concise and you most likely won't end up forgetting to initialize it if you teach yourself that habbit. 我更喜欢这样做,使代码更简洁,如果你自学habbit,你最有可能不会忘记初始化它。

If you want to just declare it in the constructor you can have the code: 如果你想在构造函数中声明它,你可以拥有代码:

     ArrayList<String> name = new ArrayList<String>();

Otherwise you can declare it as a field, and then initialize it in the constructor. 否则,您可以将其声明为字段,然后在构造函数中初始化它。

   private ArrayList<String> name;

And then in the constructor: 然后在构造函数中:

    name = new ArrayList<String>();

Making it a field would be useful, as you would then be able to create accessor/mutator methods in order to retrieve and use the List from different classes, without having to declare it public (which is rarely a good thing). 使它成为一个字段会很有用,因为您可以创建accessor / mutator方法,以便从不同的类中检索和使用List,而不必将其声明为public(这很少是一件好事)。

Generally the practice is to declare before the constructor, and initialize in the constructor. 通常,实践是在构造函数之前声明,并在构造函数中初始化。 Here's an example: 这是一个例子:
class myClass ArrayList<String> strings public myClass() { strings=new ArrayList<String>(); }

java offers you also an Initializing Fields java还为您提供了初始化字段

http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html see Initializing Instance Members http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html请参阅初始化实例成员

How can you do this ?? 你怎么能这样做?

public void setValues(String list) {

    // for(String s : test)
    // {
    list.add(this.list);
    // }
}

There is no method like add() to manipulate Strings, Instead you would have done this : 没有像add()这样的方法来操作字符串,相反,你会这样做:

public void setValues(List<String> list) {

    // for(String s : test)
    // {
    list.add(this.list);
    // }
}

And regarding declaring ArrayList in the constructors you can do like this : 关于在构造函数中声明ArrayList,您可以这样做:

String string;
List<String> list;// for example does this line need to say List<String>
                    // list = new ArrayList<String>();

// constructors
public ArrayListConstructorDemo() {
    string = "null";
    list = new ArrayList<String>();// is there anyway I can do this here
                                    // instead of 6 lines up?
}// end default constructor

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

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