How to initialize List <E> in an empty class constructor?

The following code obviously does not work, because List<E> is abstract:

 public class MyList { private List<E> list; public MyList() { this.list = new List<E>(); } } 

How can I initialize the MyList class with an empty constructor if I need a list variable like LinkedList or ArrayList depending on my needs?

+4
source share
9 answers

There are better alternatives to what you are trying to achieve:

  • Create a base class (abstract?) And redefine it twice, once for ArrayList and one for LinkedList
  • Enter the appropriate list in your class (dependency injection)
+2
source

I'm not sure what you are asking ...

 public class MyList { private List<E> list; public MyList() { if (myNeeds) this.list = new LinkedList<E>(); else this.list = new ArrayList<E>(); } } 
+7
source

Why not use a protected (and possibly abstract ) method, for example:

 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>(); } } 
+2
source
 boolean shouldThisBeAnArrayList=true; // Set to false to use LinkedList if(shouldThisBeAnArrayList) { this.list = new ArrayList<E>(); } else { this.list=new LinkedList<E>(); } 
+1
source

You need to determine which "your needs" are in the default case - LinkedList or ArrayList. If you canโ€™t - say, if the need changes depending on what happens during the life of the object, then the list must also change.

+1
source

A list is an interface and as such cannot be built. Only implementations of the specified interface can be built (for example, ArrayList). In addition, you need to know type (E) when building.

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

I think you could do the following:

 public class MyList<T> { private List<T> list; public MyList() { this.list = new ArrayList<T>(); } } 
0
source

As I understand it, you cannot use only an empty constructor, because you have a node solution in your model when you need to choose between a list type, so you will definitely need to tell the program which list will be. In my opinion this is the best solution:

 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>(); } } 
0
source
 public class MyList<T> { private List<T> list = new ArrayList<T>(); } 

This is what I use in classes. I have a long initialization that I could have when defining the personal variable it self.

0
source

Source: https://habr.com/ru/post/1310512/


All Articles