How to new a nested structure dynamically?

Yu Fang :

from How to get all types of the inner structures of a nested type?, I know how to get the inner structures of a nested data type. But, how to new this type dynamically?

That is, I can create a nested data type with static code, say,

LinkedList<LinkedList<LinkedList<Object>>> obj=new LinkedList<>();

But, if the layers of the inner of the structure is variable, how to do that with a program? Any ideas are appreciated.

Edit: For example,

public void foo(LinkedList<LinkedList<LinkedList<Object>>> inputParameter);  
//this line has to be changed because the number of inner LinkedList is variable.
LinkedList<LinkedList<LinkedList<Object>>> list=new LinkedList<>();
//call    
foo(list);
PiRocks :

If I understand your question correctly you want to something like:

int n = 10;
LinkedList<linked list nested n times, aka 10 times> = new LinkedList<>();
n++;
LinkedList<linked list nested n times, aka 11 times> = new LinkedList<>();

Please don't do this. The only use case I can imagine for this is something like storing a tensor/ndarray, in which case there are number of libraries for this, and linked lists are probably not the best choice of data-structure here.

To initialize an empty LinkedList use new LinkedList<>(); This will create an empty linked list. LinkedList does not know anything about the kind of elements it contains at runtime. In fact in certain older jdks classes did not have these kind of type parameters.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=27107&siteId=1