Go 语言程序从数组中移除前 N 个元素


在本教程中,我们将编写一个 Go 语言程序,用于从数组中移除前 N 个元素。我们可以使用 Go 语言内置函数或使用 for 循环来实现。第一种方法在功能上比第二种方法更高效,但我们将在本程序中讨论这两种方法。

方法 1:使用内部函数从整数数组中移除前 N 个元素

在本方法中,我们将编写一个 Go 语言程序,使用 append() 库函数从数组中移除前 N 个元素。

语法

func make ([] type, size, capacity)

Go 语言中的 **make** 函数用于创建数组/映射,它接受要创建的变量类型、其大小和容量作为参数。

func append(slice, element_1, element_2…, element_N) []T

append 函数用于向数组切片添加值。它接受多个参数。第一个参数是要添加值的数组,后面跟着要添加的值。然后,该函数返回包含所有值的数组的最终切片。

算法

**步骤 1** - 首先,我们需要导入 fmt 包。

**步骤 2** - 然后,创建一个函数来移除给定索引之前的元素。此函数接受数组作为参数并返回最终数组。

**步骤 3** - 现在,我们需要启动 main() 函数。

**步骤 4** - 在这里,使用 make() 函数初始化一个整数数组,并将值追加到数组中。此外,在屏幕上打印数组。

**步骤 5** - 在一个变量中存储要删除元素之前的索引。将该索引处的元素存储在一个变量中。

**步骤 6** - 现在,通过将数组和索引作为参数传递给函数并存储获得的数组来调用 delFirstElem() 函数。

**步骤 7** - 在屏幕上打印最终数组。

示例

使用内部函数从整数数组中移除前 N 个元素的 Go 语言程序

package main
import "fmt"

// function to remove the first element from the array
func delFirstElem(array []int, index int) []int {
   return append(array[index:])
}
func main() {
   
   // initializing an array
   array := make([]int, 0, 5)
   array = append(array, 1, 2, 3, 4, 5)
   fmt.Println("The given array is:", array)
   var index int = 2
   elem := array[index]
   result := delFirstElem(array, index)
   fmt.Println()
   fmt.Println("The provided index is:", index)
   fmt.Println("Array obtained after deleting elements before", elem, "is:\n", result)
}

输出

The given array is: [1 2 3 4 5]
The provided index is: 2
Array obtained after deleting elements before 3 is: [3 4 5]

方法 2:使用用户定义函数从数组中移除前 N 个元素

在本方法中,我们将编写一个 Go 语言程序,在程序的 main() 部分从数组中移除前 N 个元素。

算法

**步骤 1** - 首先,我们需要导入 fmt 包。

**步骤 2** - 现在,我们需要启动 main() 函数。

**步骤 3** - 在这里,使用 make() 函数初始化一个整数数组,并将值追加到数组中。此外,使用 fmt.Println() 函数在屏幕上打印数组。

**步骤 4** - 在一个变量中存储要删除元素之前的索引。将该索引处的元素存储在一个变量中。

**步骤 5** - 现在,要重新切片数组,请使用 : 运算符并将提供索引之前的元素存储在一个变量中。

**步骤 6** - 此外,在屏幕上打印最终数组。

示例

使用用户定义函数从数组中移除前 N 个元素的 Go 语言程序

package main
import "fmt"
func main() {
   
   // initializing an array
   array := make([]int, 0, 5)
   array = append(array, 11, 20, 13, 44, 56)
   fmt.Println("The given array is:", array)
   var index int = 3
   elem := array[index]
   result := array[index:]
   fmt.Println()
   fmt.Println("The provided index is:", index)
   fmt.Println("Array obtained after deleting elements before", elem, "is:\n", result)
}

输出

The given array is: [11 20 13 44 56]
The provided index is: 3
Array obtained after deleting elements before 44 is:
[44 56]

方法 3:使用 For 循环从整数数组中移除前 N 个元素

在本方法中,我们将使用 for 循环从 Go 编程语言中数组的开头移除特定索引之前的元素。

算法

**步骤 1** - 首先,我们需要导入 fmt 包。

**步骤 2** - 现在,我们需要启动 main() 函数。

**步骤 3** - 在这里,使用 make() 函数初始化一个整数数组,并将值追加到数组中。此外,在屏幕上打印数组。

**步骤 4** - 在一个变量中存储要删除元素之前的索引和该索引处的元素。

**步骤 5** - 现在,使用 for 循环迭代数组并将索引之后位置的元素存储在新数组中。

**步骤 6** - 最后,在屏幕上打印最终数组。

示例

使用 For 循环从整数数组中移除前 N 个元素的 Go 语言程序

package main
import "fmt"
func main() {
   array := make([]int, 0, 8)
   var result []int
   array = append(array, 11, 20, 13, 44, 56, 96, 54, 97)
   fmt.Println("The given array is:", array)

   // getting the index
   var index int = 3

   // re-slicing the array
   for i := 0; i < len(array); i++ {
      if i >= index {
         result = append(result, array[i])
      }
   }
   elem := array[index]
   fmt.Println()
   fmt.Println("The provided index is:", index)
   fmt.Println("Array obtained after deleting elements before", elem, "is:\n", result)
}

输出

The given array is: [11 20 13 44 56 96 54 97]
The provided index is: 3
Array obtained after deleting elements before 44 is:
[44 56 96 54 97]

结论

我们已经成功编译并执行了一个 Go 语言程序,用于从数组中移除前 N 个元素,并附带示例。

更新于: 2023年1月10日

989 次浏览

开启你的 职业生涯

通过完成课程获得认证

立即开始
广告
© . All rights reserved.