mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-05 03:30:30 +08:00
fix(Dart): Avoid using num as a variable name (#946)
This commit is contained in:
@@ -29,14 +29,14 @@ List<int> extend(List<int> nums, int enlarge) {
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 在数组的索引 index 处插入元素 num */
|
||||
void insert(List<int> nums, int num, int index) {
|
||||
/* 在数组的索引 index 处插入元素 _num */
|
||||
void insert(List<int> nums, int _num, int index) {
|
||||
// 把索引 index 以及之后的所有元素向后移动一位
|
||||
for (var i = nums.length - 1; i > index; i--) {
|
||||
nums[i] = nums[i - 1];
|
||||
}
|
||||
// 将 num 赋给 index 处元素
|
||||
nums[index] = num;
|
||||
// 将 _num 赋给 index 处元素
|
||||
nums[index] = _num;
|
||||
}
|
||||
|
||||
/* 删除索引 index 处元素 */
|
||||
@@ -55,12 +55,12 @@ void traverse(List<int> nums) {
|
||||
count += nums[i];
|
||||
}
|
||||
// 直接遍历数组元素
|
||||
for (int num in nums) {
|
||||
count += num;
|
||||
for (int _num in nums) {
|
||||
count += _num;
|
||||
}
|
||||
// 通过 forEach 方法遍历数组
|
||||
nums.forEach((num) {
|
||||
count += num;
|
||||
nums.forEach((_num) {
|
||||
count += _num;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -13,8 +13,8 @@ void main() {
|
||||
print('列表 nums = $nums');
|
||||
|
||||
/* 访问元素 */
|
||||
int num = nums[1];
|
||||
print('访问索引 1 处的元素,得到 num = $num');
|
||||
int _num = nums[1];
|
||||
print('访问索引 1 处的元素,得到 _num = $_num');
|
||||
|
||||
/* 更新元素 */
|
||||
nums[1] = 0;
|
||||
|
||||
@@ -29,22 +29,22 @@ class MyList {
|
||||
}
|
||||
|
||||
/* 更新元素 */
|
||||
void set(int index, int num) {
|
||||
void set(int index, int _num) {
|
||||
if (index >= _size) throw RangeError('索引越界');
|
||||
_arr[index] = num;
|
||||
_arr[index] = _num;
|
||||
}
|
||||
|
||||
/* 尾部添加元素 */
|
||||
void add(int num) {
|
||||
void add(int _num) {
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
if (_size == _capacity) extendCapacity();
|
||||
_arr[_size] = num;
|
||||
_arr[_size] = _num;
|
||||
// 更新元素数量
|
||||
_size++;
|
||||
}
|
||||
|
||||
/* 中间插入元素 */
|
||||
void insert(int index, int num) {
|
||||
void insert(int index, int _num) {
|
||||
if (index >= _size) throw RangeError('索引越界');
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
if (_size == _capacity) extendCapacity();
|
||||
@@ -52,7 +52,7 @@ class MyList {
|
||||
for (var j = _size - 1; j >= index; j--) {
|
||||
_arr[j + 1] = _arr[j];
|
||||
}
|
||||
_arr[index] = num;
|
||||
_arr[index] = _num;
|
||||
// 更新元素数量
|
||||
_size++;
|
||||
}
|
||||
@@ -60,7 +60,7 @@ class MyList {
|
||||
/* 删除元素 */
|
||||
int remove(int index) {
|
||||
if (index >= _size) throw RangeError('索引越界');
|
||||
int num = _arr[index];
|
||||
int _num = _arr[index];
|
||||
// 将索引 index 之后的元素都向前移动一位
|
||||
for (var j = index; j < _size - 1; j++) {
|
||||
_arr[j] = _arr[j + 1];
|
||||
@@ -68,7 +68,7 @@ class MyList {
|
||||
// 更新元素数量
|
||||
_size--;
|
||||
// 返回被删除元素
|
||||
return num;
|
||||
return _num;
|
||||
}
|
||||
|
||||
/* 列表扩容 */
|
||||
@@ -115,8 +115,8 @@ void main() {
|
||||
print('删除索引 3 处的元素,得到 nums = ${nums.toArray()}');
|
||||
|
||||
/* 访问元素 */
|
||||
int num = nums.get(1);
|
||||
print('访问索引 1 处的元素,得到 num = $num');
|
||||
int _num = nums.get(1);
|
||||
print('访问索引 1 处的元素,得到 _num = $_num');
|
||||
|
||||
/* 更新元素 */
|
||||
nums.set(1, 0);
|
||||
|
||||
Reference in New Issue
Block a user