List in Dart
List in Dart#
About#
- Lists in Dart are ordered collections of items.
- They can store elements of the same type or mixed types.
- Lists are zero-indexed, meaning the first element is at index 0.
- Dart provides various list operations and methods for manipulation.
Main Topics#
-
List Creation
- Definition: How to create and initialize lists in Dart.
- Example:
void main() { // Creating an empty list List<String> fruits = []; // Creating a list with initial values List<String> vegetables = ['Carrot', 'Potato', 'Spinach']; }
-
List Operations
- Definition: Methods for adding, removing, and accessing elements in a list.
- Example:
void main() { List<String> fruits = ['Apple', 'Banana']; // Adding an element fruits.add('Orange'); // Removing an element fruits.remove('Banana'); // Accessing elements print(fruits[0]); // Outputs: Apple }
-
List Types
- Definition: Fixed-length vs. growable lists.
- Example:
void main() { // Fixed-length list List<int> fixedList = List.filled(3, 0); fixedList[0] = 1; // Growable list List<int> growableList = []; growableList.add(1); growableList.add(2); print(fixedList); // Outputs: [1, 0, 0] print(growableList); // Outputs: [1, 2] }
-
List Iteration
- Definition: Techniques for looping through list elements.
- Example:
void main() { List<String> fruits = ['Apple', 'Banana', 'Orange']; // Using a for loop for (int i = 0; i < fruits.length; i++) { print(fruits[i]); } // Using forEach fruits.forEach((fruit) { print(fruit); }); }
-
List Methods
- Definition: Common methods for manipulating lists such as
add()
,remove()
,insert()
, andsort()
. - Example:
void main() { List<String> fruits = ['Apple', 'Banana', 'Orange']; // Adding an element fruits.add('Grapes'); // Removing an element fruits.remove('Banana'); // Inserting an element at a specific index fruits.insert(1, 'Blueberry'); // Sorting the list fruits.sort(); print(fruits); // Outputs: [Apple, Blueberry, Grapes, Orange] }
- Definition: Common methods for manipulating lists such as
How to Use#
- Creating a List: Use square brackets
[]
or theList
constructor. - Accessing Elements: Use index notation (e.g.,
list[index]
) to get or set elements. - Modifying Lists: Add, remove, or change elements using list methods.
- Iterating: Use loops or methods like
forEach
to process list elements.
How It Works#
- Creation: Lists can be created with initial values or as empty.
- Operations: Modify lists by accessing elements with indices and using methods for adding, removing, or updating.
- Iteration: Loop through lists using
for
,forEach
, or other iteration methods. - Types: Fixed-length lists have a set size, while growable lists can expand dynamically.
Example of List Operations:
void main() {
// Creating a growable list
List<String> fruits = ['Apple', 'Banana'];
// Adding elements
fruits.add('Orange');
// Removing an element
fruits.remove('Banana');
// Accessing elements
print(fruits[0]); // Outputs: Apple
// Iterating through the list
fruits.forEach((fruit) {
print(fruit); // Outputs: Apple, Orange
});
}