List
列表类型
This is a powerful and versatile way to store collections of items. You can think of it as a container that holds multiple elements in a specific order. Lists are dynamic, meaning their size can change, and they can store elements of different data types.
这是一个强大的和通用的方式来存储项目集合。您可以将其视为按特定顺序保存多个元素的容器。列表是动态的,这意味着它们的大小可以改变,并且它们可以存储不同数据类型的元素。
For instance, imagine you are managing a shopping list. You can create a Dart list to represent the items you need to buy:
例如,假设你正在管理一份购物清单。你可以创建一个Dart清单来表示你需要购买的物品:
dart
List<String> shoppingList = ['Apples', 'Bananas', 'Milk', 'Bread'];
In this example, we declared a list called shoppingList
, which can hold strings. It currently contains four items: 'Apples', 'Bananas', 'Milk', and 'Bread'. Lists are zero-indexed, so you can access individual items using their index, like shoppingList[0]
, which would return 'Apples'.
在这个例子中,我们声明了一个名为"shoppingList"的列表,它可以保存字符串。它目前包含四个项目:"苹果"、"香蕉"、"牛奶"和"面包"。列表是零索引的,所以你可以使用它们的索引访问单个项目,比如' shoppingList[0] ',它将返回' Apples '。
One of the most powerful features of lists is their ability to be modified. You can add new elements, remove existing ones, or update the values of specific items.
列表最强大的特性之一是它们的修改能力。您可以添加新元素、删除现有元素或更新特定项的值。
Let's say you want to add 'Eggs' to your shopping list:
假设你想把"鸡蛋"添加到你的购物清单中:
dart
shoppingList.add('Eggs');
Now, the list will include 'Eggs' as the last item.
现在,这个清单将把"鸡蛋"作为最后一项。
You can also remove items from the list using the remove
method:
你也可以使用' remove '方法从列表中删除项目:
dart
shoppingList.remove('Bananas');
This will remove 'Bananas' from the list. You can also update an item by accessing it using its index and assigning a new value:
这将从列表中删除"香蕉"。你也可以通过使用索引访问一个条目并赋一个新值来更新它:
dart
shoppingList[2] = 'Orange Juice';
Now, 'Milk' is replaced with 'Orange Juice'.
现在,"牛奶"被"橙汁"取代。
Lists are incredibly versatile and are widely used in Dart programming to manage collections of data efficiently. Whether you're handling shopping lists, to-do lists, or any other type of data that requires grouping, Dart lists will prove to be an essential tool in your programming journey.
列表具有令人难以置信的通用性,在Dart编程中被广泛用于有效地管理数据集合。无论您是在处理购物清单、待办事项清单还是需要分组的任何其他类型的数据,Dart列表都将证明是您编程过程中必不可少的工具。
示例代码:
dart
void main() {
// 定义集合
List<String> names = ["张三", "李四", "王五"];
// 追加
names.add("赵六");
// 删除
names.remove("张三");
// 修改
names[0] = "李思思";
print(names);
}
输出:
bash
[李思思, 王五, 赵六]
Map
字典类型
This is a fundamental way to store key-value pairs. Think of it as a real-world dictionary, where each word (key) has a corresponding definition (value). Maps are dynamic and unordered collections, meaning they don't have an inherent order, and you can add or remove key-value pairs as needed. Let's use a practical example to illustrate the concept of a map. Imagine you are building a simple contacts application, and you want to store the phone numbers of your friends.
这是存储键值对的基本方法。可以把它看作一个现实世界的字典,其中每个单词(键)都有相应的定义(值)。映射是动态和无序的集合,这意味着它们没有固有的顺序,您可以根据需要添加或删除键值对。让我们用一个实际的例子来说明地图的概念。假设您正在构建一个简单的联系人应用程序,并且希望存储朋友的电话号码。
You can use a Dart map to represent each friend's name as the key and their phone number as the value:
你可以使用Dart地图将每个朋友的名字表示为键,将他们的电话号码表示为值:
dart
Map<String, String> contacts = {
'John': '+2348012321244',
'Jane': '+987654321',
'Demilade': '+2347012873646',
};
In this example, we created a map called contacts
, which maps names (strings) to phone numbers (strings). Each entry is enclosed in curly braces {}
and consists of keyvalue pairs separated by a colon. The keys and values are separated by a comma.
在本例中,我们创建了一个名为"contacts"的映射,它将姓名(字符串)映射到电话号码(字符串)。每个条目都用大括号"{}"括起来,由以冒号分隔的键值对组成。键和值之间用逗号分隔。
You can access the phone number of a specific friend by using their name as the key:
您可以通过使用他们的名字作为键来访问特定朋友的电话号码:
dart
String johnPhoneNumber = contacts['John']; // Returns'+2348012321244'
If you want to add a new contact or update an existing one, you can simply assign a new value to the corresponding key:
如果你想添加一个新的联系人或更新一个现有的联系人,你可以简单地给相应的键分配一个新的值:
dart
contacts['Mandela'] = '+2348012321244'; // Adds a new contact with name Mandela and phone number '+555555555'
contacts['John'] = '+999999999'; // Updates John's phone number to '+999999999'
To remove a contact, you can use the remove
method:
要删除联系人,可以使用' remove '方法:
dart
contacts.remove('Jane'); // Removes the contact with the name 'Jane'
Maps are incredibly versatile and are widely used in Dart programming to store and manage data in a structured manner. They provide a flexible way to organize information, making them an essential tool for various applications, from simple data management to complex data structures.
映射功能非常广泛,在Dart编程中被广泛用于以结构化的方式存储和管理数据。它们提供了一种灵活的方式来组织信息,使它们成为各种应用程序(从简单的数据管理到复杂的数据结构)的基本工具。
示例代码:
dart
void main() {
// 定义字典
Map<String, String> contacts = {
"张三": "18888888888",
"李四": "18811118888",
"王五": "18888889999"
};
// 取值
// String? 表示可能为空
String? zsPhone = contacts["张三"];
print(zsPhone);
// 删除
contacts.remove("张三");
// 添加
contacts["赵六"] = "18877778888";
// 修改
contacts["赵六"] = "18877778889";
print(contacts);
}
输出:
bash
18888888888
{李四: 18811118888, 王五: 18888889999, 赵六: 18877778889}