Java 如何在空类构造函数中初始化 List<E>?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2882908/
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
How to initialize List<E> in empty class constructor?
提问by rfgamaral
The following code obviously doesn't work because List<E>
is abstract:
以下代码显然不起作用,因为它List<E>
是抽象的:
public class MyList {
private List<E> list;
public MyList() {
this.list = new List<E>();
}
}
How can I initialize MyList
class with an empty constructor if I need the list
variable to be a LinkedList
or a ArrayList
depending on my needs?
MyList
如果我需要根据我的需要将list
变量设为 aLinkedList
或 a ,如何使用空构造函数初始化类ArrayList
?
采纳答案by kgiannakakis
There are better alternatives for what you are trying to achieve:
对于您要实现的目标,有更好的选择:
- Create a base class (abstract?) and override it twice, once for ArrayList and one for LinkedList
- Inject the appropriate list to your class (dependency injection)
- 创建一个基类(抽象?)并覆盖它两次,一次用于 ArrayList,一次用于 LinkedList
- 将适当的列表注入你的类(依赖注入)
回答by Martin Tilsted
boolean shouldThisBeAnArrayList=true; // Set to false to use LinkedList
if(shouldThisBeAnArrayList) {
this.list = new ArrayList<E>();
}
else {
this.list=new LinkedList<E>();
}
回答by Thomas
I'm not sure whether this is what you're asking...
我不确定这是否是您要问的...
public class MyList {
private List<E> list;
public MyList() {
if (myNeeds)
this.list = new LinkedList<E>();
else
this.list = new ArrayList<E>();
}
}
回答by El Guapo
I would think you could do the following:
我认为您可以执行以下操作:
public class MyList<T> {
private List<T> list;
public MyList() {
this.list = new ArrayList<T>();
}
}
回答by Carl Manaster
You need to determine what "your needs" are in the default case - LinkedList or ArrayList. If you can't - say, if the need changes depending on something that happens over the object's lifetime, then the list needs to change, too.
您需要确定默认情况下的“您的需求”是什么 - LinkedList 或 ArrayList。如果你不能——比如说,如果需要根据对象生命周期中发生的事情而改变,那么列表也需要改变。
回答by Robert Kova?evi?
List is an interface and as such, cannot be constructed. Only implementations of said interface can be constructed (e.g. ArrayList). Also, you need to know the type (E) at construction.
List 是一个接口,因此无法构造。只能构造所述接口的实现(例如 ArrayList)。此外,您需要在构造时知道类型 (E)。
This should work:
这应该有效:
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class MyList<E> {
private List<E> list;
public MyList(boolean linked) {
if (linked) {
list = new LinkedList<E>();
} else {
list = new ArrayList<E>();
}
}
}
回答by Dirk
Why not use a protected (and possibly abstract
method) like:
为什么不使用受保护的(可能还有abstract
方法),例如:
public abstract class MyList<T> {
protected final List<T> list;
public MyList() {
list = createList();
}
public MyList(boolean preferLinked) {
list = preferLinked? new LinkedList<T>() : new ArrayList<T>();
}
// Allows client code which subclasses from MyList to override the
// default behaviour
protected List<T> createList() {
return new ArrayList<T>();
}
}
回答by Rbacarin
As I understand, you cannot use just a empty constructor, because you have a decision node in your model, when you need to choose between the type of the list, so, you will have to tell the program any way what kind of list will be. This seems to be the best solution in my opinion:
据我所知,你不能只使用一个空的构造函数,因为你的模型中有一个决策节点,当你需要在列表类型之间进行选择时,你必须以任何方式告诉程序什么样的列表是。在我看来,这似乎是最好的解决方案:
public class MyList {
private List<E> list;
public MyList() {
this.list = new LinkedList<E>();
}
//an overload for another type,
public MyList(bool INeedArray) {
if (INeedArray)
this.list = new ArrayList<E>();
}
}
回答by Ethode
public class MyList<T> {
private List<T> list = new ArrayList<T>();
}
This is what I use in classes.. I have for a long initialized what I could when defining the private variable it self.
这就是我在类中使用的内容。我已经在很长一段时间内初始化了自己定义私有变量时所能做的事情。