In Dart, a list is an ordered collection of objects. Lists are one of the most commonly used data structures in programming because they allow you to store and manipulate a collection of items in a single variable. This section will cover the basics of lists, including how to create, access, modify, and iterate through lists.

Key Concepts

  1. Creating Lists
  2. Accessing List Elements
  3. Modifying Lists
  4. Iterating Through Lists
  5. Common List Methods
  6. Practical Examples
  7. Exercises

  1. Creating Lists

In Dart, you can create a list using the List class or the list literal syntax.

Using List Class

List<int> numbers = List<int>.empty(growable: true);
numbers.add(1);
numbers.add(2);
numbers.add(3);
print(numbers); // Output: [1, 2, 3]

Using List Literal

List<int> numbers = [1, 2, 3];
print(numbers); // Output: [1, 2, 3]

Explanation

  • List<int>: Specifies that the list will contain integers.
  • List<int>.empty(growable: true): Creates an empty list that can grow.
  • [1, 2, 3]: List literal syntax to create a list with initial values.

  1. Accessing List Elements

You can access elements in a list using their index. Dart uses zero-based indexing.

List<String> fruits = ['Apple', 'Banana', 'Cherry'];
print(fruits[0]); // Output: Apple
print(fruits[1]); // Output: Banana
print(fruits[2]); // Output: Cherry

Explanation

  • fruits[0]: Accesses the first element of the list.
  • fruits[1]: Accesses the second element of the list.
  • fruits[2]: Accesses the third element of the list.

  1. Modifying Lists

You can modify elements in a list by assigning a new value to a specific index.

List<String> fruits = ['Apple', 'Banana', 'Cherry'];
fruits[1] = 'Blueberry';
print(fruits); // Output: [Apple, Blueberry, Cherry]

Explanation

  • fruits[1] = 'Blueberry': Changes the second element of the list to 'Blueberry'.

  1. Iterating Through Lists

You can iterate through a list using a for loop or a forEach method.

Using a For Loop

List<String> fruits = ['Apple', 'Banana', 'Cherry'];
for (int i = 0; i < fruits.length; i++) {
  print(fruits[i]);
}

Using forEach Method

List<String> fruits = ['Apple', 'Banana', 'Cherry'];
fruits.forEach((fruit) {
  print(fruit);
});

Explanation

  • for (int i = 0; i < fruits.length; i++): Iterates through the list using a for loop.
  • fruits.forEach((fruit) { ... }): Iterates through the list using the forEach method.

  1. Common List Methods

Here are some common methods you can use with lists:

Method Description
add(element) Adds an element to the end of the list.
remove(element) Removes the first occurrence of the element.
contains(element) Checks if the list contains the element.
length Returns the number of elements in the list.
clear() Removes all elements from the list.

Examples

List<int> numbers = [1, 2, 3];
numbers.add(4);
print(numbers); // Output: [1, 2, 3, 4]

numbers.remove(2);
print(numbers); // Output: [1, 3, 4]

print(numbers.contains(3)); // Output: true

print(numbers.length); // Output: 3

numbers.clear();
print(numbers); // Output: []

  1. Practical Examples

Example 1: Sum of List Elements

List<int> numbers = [1, 2, 3, 4, 5];
int sum = 0;
for (int number in numbers) {
  sum += number;
}
print('Sum: $sum'); // Output: Sum: 15

Example 2: Filtering Even Numbers

List<int> numbers = [1, 2, 3, 4, 5, 6];
List<int> evenNumbers = numbers.where((number) => number.isEven).toList();
print('Even Numbers: $evenNumbers'); // Output: Even Numbers: [2, 4, 6]

  1. Exercises

Exercise 1: Create and Print a List

Task: Create a list of your favorite fruits and print each fruit using a for loop.

Solution:

List<String> favoriteFruits = ['Mango', 'Pineapple', 'Strawberry'];
for (int i = 0; i < favoriteFruits.length; i++) {
  print(favoriteFruits[i]);
}

Exercise 2: Modify and Print a List

Task: Create a list of numbers from 1 to 5. Change the third element to 10 and print the list.

Solution:

List<int> numbers = [1, 2, 3, 4, 5];
numbers[2] = 10;
print(numbers); // Output: [1, 2, 10, 4, 5]

Exercise 3: Sum of List Elements

Task: Create a list of integers and calculate the sum of all elements using a forEach loop.

Solution:

List<int> numbers = [1, 2, 3, 4, 5];
int sum = 0;
numbers.forEach((number) {
  sum += number;
});
print('Sum: $sum'); // Output: Sum: 15

Conclusion

In this section, you learned about lists in Dart, including how to create, access, modify, and iterate through them. You also explored some common list methods and practical examples. Lists are a fundamental data structure in Dart, and mastering them will help you handle collections of data efficiently. In the next section, we will dive into another important collection type: Sets.

© Copyright 2024. All rights reserved