This commit is contained in:
krahets
2023-07-25 16:42:55 +08:00
parent 0760e0865e
commit 902087ec81
23 changed files with 154 additions and 177 deletions
+7 -9
View File
@@ -543,15 +543,13 @@ comments: true
```csharp title="list.cs"
/* 通过索引遍历列表 */
int count = 0;
for (int i = 0; i < list.Count(); i++)
{
for (int i = 0; i < list.Count; i++) {
count++;
}
/* 直接遍历列表元素 */
count = 0;
foreach (int n in list)
{
foreach (int n in list) {
count++;
}
```
@@ -1029,13 +1027,13 @@ comments: true
raise IndexError("索引越界")
return self.__nums[index]
def set(self, num: int, index: int) -> None:
def set(self, num: int, index: int):
"""更新元素"""
if index < 0 or index >= self.__size:
raise IndexError("索引越界")
self.__nums[index] = num
def add(self, num: int) -> None:
def add(self, num: int):
"""尾部添加元素"""
# 元素数量超出容量时,触发扩容机制
if self.size() == self.capacity():
@@ -1043,7 +1041,7 @@ comments: true
self.__nums[self.__size] = num
self.__size += 1
def insert(self, num: int, index: int) -> None:
def insert(self, num: int, index: int):
"""中间插入元素"""
if index < 0 or index >= self.__size:
raise IndexError("索引越界")
@@ -1070,7 +1068,7 @@ comments: true
# 返回被删除元素
return num
def extend_capacity(self) -> None:
def extend_capacity(self):
"""列表扩容"""
# 新建一个长度为原数组 __extend_ratio 倍的新数组,并将原数组拷贝到新数组
self.__nums = self.__nums + [0] * self.capacity() * (self.__extend_ratio - 1)
@@ -1583,7 +1581,7 @@ comments: true
/* 列表扩容 */
public void extendCapacity() {
// 新建一个长度为 numsCapacity * extendRatio 的数组,并将原数组拷贝到新数组
System.Array.Resize(ref nums, numsCapacity * extendRatio);
Array.Resize(ref nums, numsCapacity * extendRatio);
// 更新列表容量
numsCapacity = nums.Length;
}