-
List
6-
Lecture1.1
-
Lecture1.2
-
Lecture1.3
-
Lecture1.4
-
Lecture1.5
-
Lecture1.6
-
Add and Remove items in List
Till now we already have the data to store in List
at the time of declaration. now we want to add data dynamically during execution, for that we use add(). The syntax is like this
void main() { List<String> movies=List(); print("1. $movies"); movies.add("Venom"); print("2. $movies"); movies.add("Hulk"); print("3. $movies"); }
OUTPUT
1. [] 2. [Venom] 3. [Venom, Hulk]
So, as you can see here whenever data is added the sequence is maintained. Now in the same program let’s try to remove data. If you have data types like integer, boolean, String then you can directly mention the item name in remove()
, you can also remove items by measuring the index number in removeAt(index)
void main() { List<String> movies=List(); print("1. $movies"); movies.add("Venom"); print("2. $movies"); movies.add("Hulk"); print("2. $movies"); movies.remove("Venom"); // remove by itemName print("3. $movies"); movies.removeAt(0); // remove by index print("4. $movies"); }
OUTPUT
1. [] 2. [Venom] 2. [Venom, Hulk] 3. [Hulk] 4. []
0