Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring an array of linked list in C#

I got the compile error message "Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)" when I tried to declare an array of linked lists.

public LinkedList<LevelNode>[2] ExistingXMLList;

Also, if I wanted to create a small array of strings, isn't the following the correct way?

string [2] inputdata;
like image 663
xarzu Avatar asked Feb 27 '23 03:02

xarzu


2 Answers

You declare an array with just [].

LinkedList[] XMLList;

Then you instantiate it with the size.

XMLList = new LinkedList[2];

Or both at the same time:

LinkedList[] XMLList = new LinkedList[2];

To add LinkedLists to this array you type:

XMLList[0] = new LinkedList();
XMLList[1] = new LinkedList();
like image 88
Aaron Smith Avatar answered Mar 08 '23 08:03

Aaron Smith


try this:

LinkedList[] ExistingXMLList = new LinkedList[2];
like image 24
Daniel A. White Avatar answered Mar 08 '23 09:03

Daniel A. White