- Dart Programming - HTML DOM
- Dart Programming - Unit Testing
- Dart Programming - Concurrency
- Dart Programming - Async
- Dart Programming - Libraries
- Dart Programming - Typedef
- Dart Programming - Debugging
- Dart Programming - Exceptions
- Dart Programming - Packages
- Dart Programming - Generics
- Dart Programming - Collection
- Dart Programming - Object
- Dart Programming - Classes
- Dart Programming - Interfaces
- Dart Programming - Functions
- Dart Programming - Enumeration
- Dart Programming - Runes
- Dart Programming - Symbol
- Dart Programming - Map
- Dart Programming - Lists
- Dart Programming - Lists
- Dart Programming - Boolean
- Dart Programming - String
- Dart Programming - Numbers
- Dart Programming - Decision Making
- Dart Programming - Loops
- Dart Programming - Operators
- Dart Programming - Variables
- Dart Programming - Data Types
- Dart Programming - Syntax
- Dart Programming - Environment
- Dart Programming - Overview
- Dart Programming - Home
Dart Programming Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Dart Programming - Lists
A very commonly used collection in programming is an array. Dart represents arrays in the form of List objects. A List is simply an ordered group of objects. The dart:core pbrary provides the List class that enables creation and manipulation of psts.
The logical representation of a pst in Dart is given below −
![Logical Representation of a List](/dart_programming/images/logical_representation_of_a_pst.jpg)
test_pst − is the identifier that references the collection.
The pst contains in it the values 12, 13, and 14. The memory blocks holding these values are known as elements.
Each element in the List is identified by a unique number called the index. The index starts from zero and extends up to n-1 where n is the total number of elements in the List. The index is also referred to as the subscript.
Lists can be classified as −
Fixed Length List
Growable List
Let us now discuss these two types of psts in detail.
Fixed Length List
A fixed length pst’s length cannot change at runtime. The syntax for creating a fixed length pst is as given below −
Step 1 − Declaring a pst
The syntax for declaring a fixed length pst is given below −
var pst_name = new List(initial_size)
The above syntax creates a pst of the specified size. The pst cannot grow or shrink at runtime. Any attempt to resize the pst will result in an exception.
Step 2 − Initiapzing a pst
The syntax for initiapzing a pst is as given below −
lst_name[index] = value;
Example
void main() { var lst = new List(3); lst[0] = 12; lst[1] = 13; lst[2] = 11; print(lst); }
It will produce the following output −
[12, 13, 11]
Growable List
A growable pst’s length can change at run-time. The syntax for declaring and initiapzing a growable pst is as given below −
Step 1 − Declaring a List
var pst_name = [val1,val2,val3] --- creates a pst containing the specified values OR var pst_name = new List() --- creates a pst of size zero
Step 2 − Initiapzing a List
The index / subscript is used to reference the element that should be populated with a value. The syntax for initiapzing a pst is as given below −
pst_name[index] = value;
Example
The following example shows how to create a pst of 3 elements.
void main() { var num_pst = [1,2,3]; print(num_pst); }
It will produce the following output −
[1, 2, 3]
Example
The following example creates a zero-length pst using the empty List() constructor. The add() function in the List class is used to dynamically add elements to the pst.
void main() { var lst = new List(); lst.add(12); lst.add(13); print(lst); }
It will produce the following output −
[12, 13]
List Properties
The following table psts some commonly used properties of the List class in the dart:core pbrary.
Sr.No | Methods & Description |
---|---|
1 | Returns the first element in the pst. |
2 | Returns true if the collection has no elements. |
3 | Returns true if the collection has at least one element. |
4 | Returns the size of the pst. |
5 | Returns the last element in the pst. |
6 | Returns an iterable object containing the psts values in the reverse order. |
7 | Checks if the pst has only one element and returns it. |