at the moment I'm using a List<short>
as a buffer to hold things for a while while a calculation is made to each value based on other values further down the buffer. I then realised that this probably wasn't very effecient as I have been told that List<>
is a linked list so every time I do whatever = myList[100];
the poor thing is having to jump down all the other nodes first to get to the value I want. I dont want to use a regular Array because I have got loads of Add()
and Remove()
s kicking around in other places in the code. So I need a class that inherits IList<T>
but uses a regular array data structure. Does anyone know a class in .net that works this way so I dont have to write my own? I tried using ArrayList but it 'aint generic!
What is a List? A list is an ordered data structure with elements separated by a comma and enclosed within square brackets. For example, list1 and list2 shown below contains a single type of data. Here, list1 has integers while list2 has strings. Lists can also store mixed data types as shown in the list3 here.
The C Standard does not provide data structures like linked list and stack. Some compiler implementations might provide their own versions but their usage will be non portable across different compilers.
A list is an abstract data type that describes a linear collection of data items in some order, in that each element occupies a specific position in the list. The order could be alphabetic or numeric or it could just be the order in which the list elements have been added.
In C language, a linked list can be implemented using structure and pointers . struct LinkedList{ int data; struct LinkedList *next; }; The above definition is used to create every node in the list. The data field stores the element and the next is a pointer to store the address of the next node.
List<T>
doesn't use a linked list implementation. Internally it uses an array, so it appears to be exactly what you need. Note that, because it's an array, Remove/insert could be an expensive operation depending on the size of the list and the position item being removed/inserted - O(n). Without knowing more about how you are using it, though, it's hard to recommend a better data structure.
Quoting from the Remarks section of the docs.
The List(T) class is the generic equivalent of the ArrayList class. It implements the IList(T) generic interface using an array whose size is dynamically increased as required.
List<T>
is backed by an array, not a linked list. Indexed accesses of a List<T>
happen in constant time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With