Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How insert element in last list?

Tags:

c#

list

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 
like image 240
MohammadTofi Avatar asked Jan 20 '13 12:01

MohammadTofi


People also ask

How do I add an element to the last of a list?

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 .

How is insert () used in list?

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.

Does append () adds an object to the end of 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.


2 Answers

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

like image 63
Jesper Fyhr Knudsen Avatar answered Nov 07 '22 07:11

Jesper Fyhr Knudsen


Use:

List<double> m = new List<double>();
m.Add(1);
m.Add(2);
m.Add(3);
m.Add(7);
like image 30
devdigital Avatar answered Nov 07 '22 06:11

devdigital