在java构造函数中声明ArrayList

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22736092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:34:35  来源:igfitidea点击:

declaring ArrayList in java Constructor

javaarraylistconstructor

提问by demuro1

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. 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. Thanks for your suggestions! I have an example of what I'm talking about below:

我正在做一个项目,我被教导在构造函数中实例化变量。我在使用 ArrayList 思想时遇到了一些麻烦。你能提出一些最佳实践吗,我是否需要用实例变量定义 ArrayList 或者我可以在构造函数中做到这一点。感谢您的建议!我有一个我在下面谈论的例子:

//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

采纳答案by casperw

Change

改变

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

to

list = new ArrayList<String>();

回答by skiwi

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> listclass variable, meaning that you are creating a newinstance of list, rather than that you are initializing the listinstance 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.

我个人更喜欢在声明时初始化它,所以你以前有。我更喜欢这样使代码更简洁,如果你自学那个习惯,你很可能最终不会忘记初始化它。

回答by Connor Cartwright

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).

使它成为一个字段会很有用,因为您将能够创建访问器/修改器方法,以便从不同的类中检索和使用 List,而无需将其声明为 public(这很少是一件好事)。

回答by Matej ?pilár

java offers you also an Initializing Fields

java 还为您提供了一个初始化字段

http://docs.oracle.com/javase/tutorial/java/javaOO/initial.htmlsee Initializing Instance Members

http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html参见初始化实例成员

回答by faeophyta

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

一般的做法是在构造函数前声明,在构造函数中初始化。下面是一个例子:
class myClass ArrayList<String> strings public myClass() { strings=new ArrayList<String>(); }

回答by Roshan Shahukhal

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