登录 主页

go数组(包含日志的获取数组的长度与删除的使用)

2023-03-22 04:18PM

包含日志:

package main

import (
    "fmt"
)

func main(){
  // initialize an array
  originalArray := [4]string{"a", "b", "c", "d"}
  fmt.Println("原数组为:", originalArray)

  // initialize the index of the element to delete
  i := 2

  fmt.Println(len(originalArray))
  // check if the index is within array bounds
  if i < 0 || i >= len(originalArray) {
    fmt.Println("The given index is out of bounds.")
  } else {
    // delete an element from the array
    newLength := 0
    for index := range originalArray {
      fmt.Println("新的长度", newLength, "i是", i, "index是", index, "第", index + 1, "次循环")
      if i != index {
        fmt.Println("赋值前=== originalArray[newLength]: ", originalArray[newLength])
        fmt.Println("赋值前=== originalArray[index]: ", originalArray[index])
        originalArray[newLength] = originalArray[index]
        fmt.Println("赋值后=== originalArray[newLength]: ", originalArray[newLength])
        fmt.Println("赋值后=== originalArray[index]: ", originalArray[index])
        newLength++
       } else {
         fmt.Println("===================== 什么都不做")
      }
    }
    // reslice the array to remove extra index
    newArray := originalArray[:newLength]
    //fmt.Println(originalArray)
    fmt.Println("The new array is:", newArray)
    fmt.Println("The new length is:", newLength)
  }
}

结果:

原数组为: [a b c d]
4
新的长度 0 i是 2 index是 0 第 1 次循环
赋值前=== originalArray[newLength]:  a
赋值前=== originalArray[index]:  a
赋值后=== originalArray[newLength]:  a
赋值后=== originalArray[index]:  a
新的长度 1 i是 2 index是 1 第 2 次循环
赋值前=== originalArray[newLength]:  b
赋值前=== originalArray[index]:  b
赋值后=== originalArray[newLength]:  b
赋值后=== originalArray[index]:  b
新的长度 2 i是 2 index是 2 第 3 次循环
===================== 什么都不做
新的长度 2 i是 2 index是 3 第 4 次循环
赋值前=== originalArray[newLength]:  c
赋值前=== originalArray[index]:  d
赋值后=== originalArray[newLength]:  d
赋值后=== originalArray[index]:  d
The new array is: [a b d]
The new length is: 3

简洁版(不包含日志):

package main

import (
    "fmt"
)

func main(){

    // initialize an array
    originalArray := [4]string{"a", "b", "c", "d"}
    fmt.Println("The original array is:", originalArray)

    // initialize the index of the element to delete
    i := 2

    // check if the index is within array bounds
    if i < 0 || i >= len(originalArray) {
        fmt.Println("The given index is out of bounds.")
    } else {
        // delete an element from the array
        newLength := 0
        for index := range originalArray {
          if i != index {
            originalArray[newLength] = originalArray[index]
            newLength++
          }
        }   

        // reslice the array to remove extra index
        newArray := originalArray[:newLength]
        fmt.Println("The new array is:", newArray)
        fmt.Println("The new length is:", newLength)
    }   
}

结果为:

The original array is: [a b c d]
The new array is: [a b d]
The new length is: 3

返回>>

登录

请登录后再发表评论。

评论列表:

目前还没有人发表评论