I have a list m
. I want to insert an element to the end of this List
. Please tell me how I can do this.
public List<double> m = new List<double>();
m[0].Add(1);
m[1].Add(2);
m[2].Add(3);
I want the following output if I add element 7 to the end:
1 2 3 7
If we want to add an element at the end of a list, we should use append . It is faster and direct. If we want to add an element somewhere within a list, we should use insert .
The insert() method inserts a new item in a specified index, the append() method adds a new at the last index of a list, while the extend() method appends a new data collection to a list.
The difference between the two methods is that . append() adds an item to the end of a list, whereas . insert() inserts and item in a specified position in the list.
m.Add(7);
will add it to the end of the list.
What you are doing is trying to call the method Add on a double
Use:
List<double> m = new List<double>();
m.Add(1);
m.Add(2);
m.Add(3);
m.Add(7);
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