-
List
6-
Lecture1.1
-
Lecture1.2
-
Lecture1.3
-
Lecture1.4
-
Lecture1.5
-
Lecture1.6
-
Useful functions in List
In this lesson, we will discuss about few useful functions related to List.
contains()
Search elements in the list and return the result in terms of true or false.
void main() { List<String> movies=["Avengers","IronMan","Harry Potter-I","Harry Potter-II"]; print(movies.contains("IronMan")); }
OUTPUT
true
indexOf()
Similar to contain function is also the search element remind in the list but returns the index of the found item.
void main() { List<String> movies=["Avengers","IronMan","Harry Potter-I","Harry Potter-II"]; print(movies.indexOf("IronMan")); }
OUTPUT
1
insert()
Insert a new element In the list at a particular index that we define.
void main() { List<String> movies=["Avengers","IronMan","Harry Potter-I","Harry Potter-II"]; movies.insert(2,"Venom"); print(movies); }
OUTPUT
[Avengers, IronMan, Venom, Harry Potter-I, Harry Potter-II]
sublist()
Returns a part of the list from the index number that we mention. if only the starting number was mentioned list is made from the starting index till the end.
void main() { List<String> movies=["Avengers","IronMan","Harry Potter-I","Harry Potter-II"]; List newList=movies.sublist(2); print(newList); }
OUTPUT
[Harry Potter-I, Harry Potter-II]
0