Dart – 17 – Lists

Lists in Dart Programming

Lists are a fundamental data structure in Dart, as in many other programming languages. They are versatile and essential for storing and manipulating collections of items. In this discussion, we’ll explore the significance of lists in Dart, understand how they work, and see practical examples of their use.

Understanding Lists

A list in Dart is an ordered collection of values or objects. Each item in a list is associated with an index, starting from 0 for the first item. Lists can contain elements of the same type or a combination of different types.

Creating Lists

In Dart, you can create a list using a list literal or by instantiating a List object. List literals are enclosed in square brackets [], while List objects are created using constructors.

Here’s an example of creating lists in Dart:


// Using list literals
var fruits = ['apple', 'banana', 'cherry'];

// Using the List constructor
var numbers = List.from([1, 2, 3]);
    
Accessing List Elements

You can access elements in a list by their index. Dart uses zero-based indexing, so the first element is at index 0, the second at 1, and so on. You can use square brackets with the index to access the element.

Here’s how to access list elements in Dart:


var fruits = ['apple', 'banana', 'cherry'];
print(fruits[0]); // Outputs 'apple'
    
Modifying Lists

Lists in Dart are mutable, meaning you can change their contents after creation. You can modify individual elements, add new elements, or remove elements from a list.

Here are examples of modifying lists:


var numbers = [1, 2, 3];
numbers[1] = 4; // Modifying an element
numbers.add(5); // Adding a new element
numbers.remove(2); // Removing an element
    
List Methods

Dart provides a variety of built-in methods for working with lists. These methods make it easy to manipulate lists, including adding, removing, and searching for elements.

Here are some common list methods:


var numbers = [1, 2, 3];
numbers.add(4); // Add an element
numbers.remove(2); // Remove an element
numbers.contains(3); // Check if an element exists
numbers.length; // Get the length of the list
numbers.sort(); // Sort the list
    
Iterating Over Lists

To process each element in a list, you can use loops. Dart provides several loop constructs, such as for loops and forEach method, to iterate over the elements of a list.

Here’s how to iterate over a list in Dart:


var fruits = ['apple', 'banana', 'cherry'];
for (var fruit in fruits) {
    print(fruit);
}

fruits.forEach((fruit) {
    print(fruit);
});
    
List of Lists

Lists in Dart can contain elements of different types, including other lists. This feature allows you to create multi-dimensional lists, commonly used for representing matrices or tables.

Here’s an example of a list containing lists:


var matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
];
    
Conclusion

Lists are a fundamental and versatile data structure in Dart that allow you to store, access, and manipulate collections of elements. Whether you’re working with simple lists or complex data structures, understanding how to use lists effectively is crucial for developing Dart applications. With various methods and techniques, you can efficiently manage and work with lists to meet your programming needs.