This commit is contained in:
krahets
2024-03-31 03:53:04 +08:00
parent 87af663929
commit c23e576da4
68 changed files with 2139 additions and 22 deletions

View File

@@ -133,7 +133,22 @@ comments: true
=== "Kotlin"
```kotlin title="list.kt"
/* 初始化列表 */
// 无初始值
var nums1 = listOf<Int>()
// 有初始值
var numbers = arrayOf(1, 3, 2, 5, 4)
var nums = numbers.toMutableList()
```
=== "Ruby"
```ruby title="list.rb"
# 初始化列表
# 无初始值
nums1 = []
# 有初始值
nums = [1, 3, 2, 5, 4]
```
=== "Zig"
@@ -262,7 +277,19 @@ comments: true
=== "Kotlin"
```kotlin title="list.kt"
/* 访问元素 */
val num = nums[1] // 访问索引 1 处的元素
/* 更新元素 */
nums[1] = 0 // 将索引 1 处的元素更新为 0
```
=== "Ruby"
```ruby title="list.rb"
# 访问元素
num = nums[1]
# 更新元素
nums[1] = 0
```
=== "Zig"
@@ -493,7 +520,41 @@ comments: true
=== "Kotlin"
```kotlin title="list.kt"
/* 清空列表 */
nums.clear();
/* 在尾部添加元素 */
nums.add(1);
nums.add(3);
nums.add(2);
nums.add(5);
nums.add(4);
/* 在中间插入元素 */
nums.add(3, 6); // 在索引 3 处插入数字 6
/* 删除元素 */
nums.remove(3); // 删除索引 3 处的元素
```
=== "Ruby"
```ruby title="list.rb"
# 清空列表
nums.clear
# 在尾部添加元素
nums << 1
nums << 3
nums << 2
nums << 5
nums << 4
# 在中间插入元素
nums.insert 3, 6
# 删除元素
nums.delete_at 3
```
=== "Zig"
@@ -690,7 +751,32 @@ comments: true
=== "Kotlin"
```kotlin title="list.kt"
/* 通过索引遍历列表 */
var count = 0
for (i in nums.indices) {
count += nums[i]
}
/* 直接遍历列表元素 */
for (num in nums) {
count += num
}
```
=== "Ruby"
```ruby title="list.rb"
# 通过索引遍历列表
count = 0
for i in 0...nums.length
count += nums[i]
end
# 直接遍历列表元素
count = 0
for num in nums
count += num
end
```
=== "Zig"
@@ -809,7 +895,17 @@ comments: true
=== "Kotlin"
```kotlin title="list.kt"
/* 拼接两个列表 */
val nums1 = intArrayOf(6, 8, 7, 10, 9).toMutableList()
nums.addAll(nums1) // 将列表 nums1 拼接到 nums 之后
```
=== "Ruby"
```ruby title="list.rb"
# 拼接两个列表
nums1 = [6, 8, 7, 10, 9]
nums += nums1
```
=== "Zig"
@@ -910,7 +1006,15 @@ comments: true
=== "Kotlin"
```kotlin title="list.kt"
/* 排序列表 */
nums.sort() // 排序后,列表元素从小到大排列
```
=== "Ruby"
```ruby title="list.rb"
# 排序列表
nums = nums.sort { |a, b| a <=> b }
```
=== "Zig"
@@ -2171,6 +2275,100 @@ comments: true
}
```
=== "Ruby"
```ruby title="my_list.rb"
### 列表类 ###
class MyList
attr_reader :size # 获取列表长度(当前元素数量)
attr_reader :capacity # 获取列表容量
### 构造方法 ###
def initialize
@capacity = 10
@size = 0
@extend_ratio = 2
@arr = Array.new capacity
end
### 访问元素 ###
def get(index)
# 索引如果越界,则抛出异常,下同
raise IndexError, "索引越界" if index < 0 || index >= size
@arr[index]
end
### 访问元素 ###
def set(index, num)
raise IndexError, "索引越界" if index < 0 || index >= size
@arr[index] = num
end
### 在尾部添加元素 ###
def add(num)
# 元素数量超出容量时,触发扩容机制
extend_capacity if size == capacity
@arr[size] = num
# 更新元素数量
@size += 1
end
### 在中间插入元素 ###
def insert(index, num)
raise IndexError, "索引越界" if index < 0 || index >= size
# 元素数量超出容量时,触发扩容机制
extend_capacity if size == capacity
# 将索引 index 以及之后的元素都向后移动一位
for j in (size - 1).downto(index)
@arr[j + 1] = @arr[j]
end
@arr[index] = num
# 更新元素数量
@size += 1
end
### 删除元素 ###
def remove(index)
raise IndexError, "索引越界" if index < 0 || index >= size
num = @arr[index]
# 将将索引 index 之后的元素都向前移动一位
for j in index...size
@arr[j] = @arr[j + 1]
end
# 更新元素数量
@size -= 1
# 返回被删除的元素
num
end
### 列表扩容 ###
def extend_capacity
# 新建一个长度为原数组 extend_ratio 倍的新数组,并将原数组复制到新数组
arr = @arr.dup + Array.new(capacity * (@extend_ratio - 1))
# 更新列表容量
@capacity = arr.length
end
### 将列表转换为数组 ###
def to_array
sz = size
# 仅转换有效长度范围内的列表元素
arr = Array.new sz
for i in 0...sz
arr[i] = get i
end
arr
end
end
```
=== "Zig"
```zig title="my_list.zig"