From 459f9be39ad1fcae0fa21706bd8e73631c15f604 Mon Sep 17 00:00:00 2001 From: xiao_hei_9527 <93132495+xiaohei1263@users.noreply.github.com> Date: Mon, 28 Nov 2022 19:23:46 +0800 Subject: [PATCH] Create array.go --- .../go/chapter_array_and_linkedlist/array.go | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 codes/go/chapter_array_and_linkedlist/array.go diff --git a/codes/go/chapter_array_and_linkedlist/array.go b/codes/go/chapter_array_and_linkedlist/array.go new file mode 100644 index 000000000..4fffdb68b --- /dev/null +++ b/codes/go/chapter_array_and_linkedlist/array.go @@ -0,0 +1,67 @@ +package main + +import ( + "fmt" + "math/rand" + "time" +) + +func randomAccess(nums []int) int { + //随机访问元素 + rand.Seed(time.Now().UnixNano()) + RandomIndex := rand.Intn(len(nums)) //在区间 [0, len(nums)) 中随机抽取一个数字 + return nums[RandomIndex] +} +func extend(nums []int, enlarge int) []int { + //初始化一个扩展长度后的数组 + res := make([]int, len(nums)+enlarge) + //将原数组中的所有元素复制到新数组 + copy(res, nums) + //返回扩展后的新数组 + return res +} +func insert(nums []int, index, num int) { + //把索引 index 以及之后的所有元素向后移动一位 + copy(nums[index+1:], nums[index:]) + //将 num 赋给 index 处元素 + nums[index] = num +} +func remove(nums []int, index int) { + //把索引 index 之后的所有元素向前移动一位 + copy(nums[index:], nums[index+1:]) +} +func traverse(nums []int) { + count := 0 + //通过索引遍历数组 + for i := 0; i < len(nums); i++ { + count++ + fmt.Printf("%d ", nums[i]) + } + fmt.Println() + //直接遍历数组 + for _, i := range nums { + count++ + fmt.Printf("%d ", i) + } +} +func find(nums []int, target int) int { + //在数组中查找指定元素 + for i := range nums { + if nums[i] == target { + return i + } + } + return -1 +} +func main() { + //初始化数组 + arr := []int{1, 2, 3, 4, 5} + //var nums []int + + arr = extend(arr, 2) + + insert(arr, 2, 99) + remove(arr, 2) + traverse(arr) + fmt.Printf("%d", find(arr, 5)) +}