std.ArrayList 是 Zig 标准库中最常用的可变长连续数组容器。
std.ArrayList(T) 默认属于非托管(unmanaged)类型,使用 .empty 进行初始化,在进行追加、扩容或释放等需要内存分配的操作时显式传入分配器(Allocator):
append(allocator, item):向末尾添加单个元素;appendSlice(allocator, items):追加元素切片;insert(allocator, index, item):在指定位置插入元素(O(N));orderedRemove(index):移除指定索引处的元素并保持剩余元素顺序(O(N));swapRemove(index):将最后一个元素移至待删除位置进行快速移除(O(1),不保证顺序);pop():弹出末尾元素;deinit(allocator):释放分配的连续内存。
//! Demonstrate dynamic array operations with std.ArrayList in Zig.
const std = @import("std");
pub fn main(init: std.process.Init) !void {
const gpa = init.gpa;
// std.ArrayList(T) is unmanaged by default and initialized with .empty.
var list: std.ArrayList(i32) = .empty;
defer list.deinit(gpa);
// Append single elements
try list.append(gpa, 10);
try list.append(gpa, 20);
// Append multiple elements at once
try list.appendSlice(gpa, &.{ 30, 40, 50 });
std.debug.print("Initial items: {any}\n", .{list.items});
try std.testing.expectEqual(5, list.items.len);
// Insert at index 1: moves elements to make room (O(N))
try list.insert(gpa, 1, 15);
std.debug.print("After inserting 15 at index 1: {any}\n", .{list.items});
// Remove element at index 1 preserving order (O(N))
const removed = list.orderedRemove(1);
try std.testing.expectEqual(15, removed);
// Remove element at index 0 without preserving order (O(1), swaps with last element)
const swapped = list.swapRemove(0);
try std.testing.expectEqual(10, swapped);
std.debug.print("After swapRemove(0): {any}\n", .{list.items});
// Pop the last element
const popped = list.pop();
try std.testing.expectEqual(40, popped.?);
std.debug.print("Final items: {any}\n", .{list.items});
}