Why create arraylist outside default constructor but then assign it in the construtor? What does this do?

huangapple go评论70阅读模式
英文:

Why create arraylist outside default constructor but then assign it in the construtor? What does this do?

问题

为什么在默认构造函数外部创建ArrayList,然后在构造函数中赋值呢?这样做有什么作用?不能在构造函数内部或外部完全创建它吗?

这里是我看到的一个示例:

public class Tickets {

    public ArrayList <movie> movies;  

    public Tickets() {
        movies = new ArrayList <movie>(); //将movie赋值给ArrayList
    }
}
英文:

Why create arraylist outside default constructor but then assign it in the construtor? What does this do? Couldnt you create it all within the constructor or outside of it?

Here is a piece example i saw:

public class Tickets {

public ArrayList &lt;movie&gt; movies;  


public Tickets() {
	
	movies = new ArrayList &lt;movie&gt;(); //assigning movie to array list
}

答案1

得分: 0

三个明显的部分:变量声明,实例化和引用赋值

你问道:

> 为什么在默认构造函数外部创建ArrayList,然后在构造函数中分配它?

你的问题基于一个误解。

这行代码:

public ArrayList &lt;movie&gt; movies;  

... 创建一个 ArrayList 对象。这行代码定义了一个变量,它 持有,但是现在还没有持有,一个指向 ArrayList 对象的 引用。没有引用意味着这个变量的值是 null

这行代码创建了一个容器,但并没有填充它。

你的 new ArrayList &lt;movie&gt;() 代码创建了一个 ArrayList 对象。这个对象存在于内存中。这段代码返回对该新对象的 引用(实际上是它在内存中的 地址,一个 指针)。

你的 movies = 代码捕获了返回的引用(准内存位置),并将其存储在你的变量中,以便稍后在你的其他代码中使用。


在你的具体例子中,你也可以将变量声明与实例化和引用分配结合起来。那时就不需要你的构造函数了,因为它没有执行其他操作。

在实例化时你可以省略参数化的类型。编译器可以从你在声明对象引用变量时指定的类型中推断出来。所以使用 new ArrayList&lt;&gt;() 而不是 new ArrayList&lt;movie&gt;()

如果你想阻止用另一个实例替换你的列表实例,可以添加关键字 final

public class Tickets {

    public final ArrayList &lt;movie&gt; movies = new ArrayList &lt;&gt; () ;

}

顺便说一下,你的 movie 类应该拼写为 Movie。类名以大写字母开头,变量名以小写字母开头。所以应该是 Movie 而不是 movie

当你不需要明确引用更具体子类型的特性时,最好将变量声明为更一般的超类型。所以,使用 List,而不是 ArrayList

public class Tickets {

    public final List &lt; Movie &gt; movies = new ArrayList &lt;&gt; () ;

}
英文:

Three distinct parts: variable declaration, instantiation, and reference assignment

You asked:

>Why create arraylist outside default constructor but then assign it in the construtor?

Your question is based on a misunderstanding.

The line:

public ArrayList &lt;movie&gt; movies;  

… does not create an ArrayList object. That line 👉 defines a variable that will hold, but does not yet hold, a reference to an ArrayList object. Holding no reference means the value of this variable is null.

That line created a receptacle, but did not fill it.

Your new ArrayList &lt;movie&gt;() code creates an ArrayList object. That object lives in memory. That code returns a reference to that new object (effectively its address in memory, a pointer).

Your movies = code 👉 captures that returned reference (the quasi memory location) and stores it in your variable, for later use in your other code.


In your specific example, you could just as well have combined the variable declaration with the instantiation and the reference assignment. No need for your constructor then, as it performed no other work.

You can omit the parameterized type when instantiating. The compiler can deduce that from the type you specified when declaring the object reference variable. So new ArrayList&lt;&gt;() rather than new ArrayList&lt;movie&gt;().

Add the keyword final if you want to prevent replacement of your list instance with another.

public class Tickets {

    public final ArrayList &lt;movie&gt; movies = new ArrayList &lt;&gt; () ;

}

By the way, your movie class should be spelled Movie. Class names start with an uppercase letter, variable names start with a lowercase letter. So Movie, not movie.

And generally best to declare your variables as a more general super-type when you don't need to explicitly refer to features exclusive to the more specific sub-type. So, List, not ArrayList.

public class Tickets {

    public final List &lt; Movie &gt; movies = new ArrayList &lt;&gt; () ;

}

huangapple
  • 本文由 发表于 2023年5月21日 07:24:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297713.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定