Add Your Heading Text Here

From, now on we are gonna discuss about the data structures(as name suggests structures used for manipulating different data).

I think, there are 2 data structures a programmer must learn for manipulating and storing the data which are List and HashMap.

So, in this tutorial we are gonna talk in deep about the list data structure.

List is also the built in data type for dart programming language.

Lists is the very common data structure used in almost all of the languages.

Lists is the very handy way to manage data of different objects.

List is the collection of ordered or group of objects of the same data type in general.

In dart, arrays are not used. Instead Arrays will act as the List Objects.

Even dart virtual machine predicts the list as the array objects.

The basic list structure can be described as follow:

For ex :

var list = [10, 20, 30]; // list with only 3 Elements

Above example has 3 list elements 10, 20, 30.

List has the zero based index, which means above list has value 10 at the index 0, value 20 at the index 1 and value 30 at the index 2.

So, which means particular values stored in the list are elements.

While, each elements stored in the particular position in the list is Index.

and therefore, size of the list = [size of the elements] – 1
and size of the list = [size of the last index] // as we know that index starts from 0 to expand up to [total elements-1]

List Syntax :

var list_name=new List(list_size);

With above syntax, fixed size list can be created and we can store data elements up to the specified list size.

we cannot shrink or expand the list size.

List Initialisation :

list_name[index]=value;

the List initialisation is used to assign the values to the list elements.

For ex :

void main()
	{
		var avengers=new List(3);
  		avengers[0]="Hulk";
  		avengers[1]="Thor";
  		avengers[2]="Ironman";
  		print(avengers);
	}

Output :

[Hulk, Thor, Ironman]

The size of the above list is 3 if you try to add 4th element in the list it wiil give you ( Index out of range) exception.

We can also initialise the list at the time of declaration as following :

For ex :

void main(){ 
    var player_list = ["Roger","Rafael","Novak"];    			 
    print(player_list); 		
}

Output :

[Roger, Rafael, Novak]

we can also, create the dynamic list with no specified size. in which, we can add the elements in the list according to our neeeds using add() function defined in List class.

For ex :

void main() { 
   	var languages = new List(); 
   	languages.add("C"); 
	languages.add("Dart");
  	languages.add("Java");
 	print(languages); 
}

Output :

[C, Dart, Java]

List creates an unspecifeid length list using the empty List() Constructor from the List Class.

Lists class have many pre-defined properties and functions to perform efficient tasks for manipulating the data inside the List Structure.

you can also store the data of group of objects with different data types like, String, boolean, integer etc.

For ex :

void main() { 
   	var dataList = new List(); 
   	dataList.add("Toastguyz"); 
   	dataList.add(true);
   	dataList.add(123456);
   	print(dataList); 
}

Output :

[Toastguyz, true, 123456]

List Common Properties

For ex :

main() {
  var list=new List<String>();
  list.add("Sachin");
  list.add("Sehwag");
  list.add("Dhoni");
  list.add("Kohli");
  list.add("rohit");

  print(list.length);
  print(list.reversed);
  print(list.first);
  print(list.hashCode);
  print(list.isEmpty);
  print(list.isNotEmpty);
  print(list.iterator);
  print(list.last);
  print(list.runtimeType);
}

Output :

5
(rohit, Kohli, Dhoni, Sehwag, Sachin)
Sachin
42153199
false
true
Instance of ‘ListIterator’
rohit
List<String>

List Common Methods

For ex :

main() {
  var listValues = [2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009];
  var list = new List<int>();
  list.add(107);
  list.add(108);
  list.add(109);
  list.add(110);
  list.add(101);
  list.add(102);
  list.add(103);
  list.add(104);
  list.add(105);

  list.removeLast();
  print(list);

  list.sort();
  print(list);

  list.insert(6, 106);
  print(list);

  list.setRange(2, 7, listValues);
  print(list);

  list.remove(110);
  print(list);

  list.asMap();
  print(list);

  print(list.indexOf(109));
  print(list.sublist(3));
  print(list.contains(2004));
  print(list.skip(5));
  print(list.elementAt(2));

  list.addAll(listValues);
  print(list);

  list.shuffle();
  print(list);

  //  print(list.clear());
}

Output :

[107, 108, 109, 110, 101, 102, 103, 104]
[101, 102, 103, 104, 107, 108, 109, 110]
[101, 102, 103, 104, 107, 108, 106, 109, 110]
[101, 102, 2001, 2002, 2003, 2004, 2005, 109, 110]
[101, 102, 2001, 2002, 2003, 2004, 2005, 109]
[101, 102, 2001, 2002, 2003, 2004, 2005, 109]
7
[2002, 2003, 2004, 2005, 109]
true
(2004, 2005, 109)
2001
[101, 102, 2001, 2002, 2003, 2004, 2005, 109, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009]
[2005, 2003, 2001, 101, 2003, 2002, 2004, 2008, 2006, 2007, 109, 2002, 2001, 2005, 2009, 2004, 102]

Again, I must say that list is the important topic to have grip on it. so, make more and more practice with lists to be a better Programmer.

Feel free to ask your questions in the comment section. Keep reading and commenting your suggestions to make toastguyz a better site to learn different programming languages.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Show Buttons
Hide Buttons