dousi4257 2014-08-08 21:08
浏览 75
已采纳

向外扩展切片和底层数组

I have an array and a slice pointing to it, like shown as follows:

package main

import "fmt"

func main() {

    array_str := []string{"0a","1b","2c","3d","4e"}
    slice_str:=array_str[1:4]
    fmt.Println("Initially :")
    fmt.Println("Printing 1 :Array :",array_str)
    fmt.Println("Printing 1 :Slice:",slice_str)

    //Step 1.Changing Slice and it get reflected in array
    fmt.Println("
After Alteration:")
    slice_str[0]="alterd_1b"
    fmt.Println("Printing 2 :Array :",array_str)
    fmt.Println("Printing 2 :Slice:",slice_str)
    fmt.Println("len of  slice_str:",len(slice_str)," cap of         slice_str:",cap(slice_str),"len of  array_str:",len(array_str))

    //Step 2.appending to slice and it get reflected

    slice_str = append(slice_str,"apnded_elemnt")
    fmt.Println("
After Apending:")
    fmt.Println("Printing 3 :Array :",array_str)//"4e" is replaced with "apnded_elemnt" in array !!
    fmt.Println("Printing 3 :Slice:",slice_str)
    fmt.Println("len of  slice_str:",len(slice_str)," cap of   slice_str:",cap(slice_str),"len of  array_str:",len(array_str))

    //Step 3.Again appending to slice so that lentght of slice is growing further to  underlaying array
    slice_str = append(slice_str,"outgrown_elemnt")
    fmt.Println("
After OUT GROWING:")
    fmt.Println("Printing 4 :Array :",array_str)//obeviously out grown elemnt not added to array that is fine
    fmt.Println("Printing 4 :Slice:",slice_str)
    fmt.Println("len of  slice_str:",len(slice_str)," cap of slice_str:",cap(slice_str),"len of  array_str:",len(array_str))
    //How Capacity Become 8 here ???

    //Step 4 .Now Changing Slice element which is in Range of array to verify it reflect on array:
    fmt.Println("
After Post out grown Alteration:")
    slice_str[0]="again_alterd_1b"
    fmt.Println("Printing 2 :Array :",array_str)//Change in slice is not reflectd in array .Why ?
    fmt.Println("Printing 2 :Slice:",slice_str)
}

Playground: http://play.golang.org/p/3z52HXHQ7s

Questions:

  1. In Step 3: why does cap of the slice jumped from 4 to 8?

  2. In Step 4: after the slice is out grown, changes to the element of the slice, which is in the range of the array, is not reflected to the array and vice versa. Why is it not happening after it is grown out? What actually happens when the slice grows out?

  • 写回答

2条回答 默认 最新

  • douzi0609 2014-08-08 22:43
    关注

    See here: http://blog.golang.org/slices

    Short answers: 1) it grows by doubling (while short). If you append once you might append a second time too and this avoids allocations. 2) That's how slice growing works. An array cannot grow, so a new, larger array is allocated, the old one copied and you are handed a slice pointing to the larger copy.

    (The documentation on the golang.org website is really helpful, readable, short and precise. I'd like to recommend to look at golang.org first before asking here.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 Keil编译时报错"no source": Error: #5: cannot open source
  • ¥50 操作系统时间无法更新
  • ¥20 Windows11, usb转hdmi,外接显示器无反应
  • ¥20 怎么在JavaFx的TableView中动态地添加数据。
  • ¥15 MFC里的工具栏按钮图标使用外部图片
  • ¥15 如何在 llama.cpp 服务器中实现用户登录功能的优化方案?(语言-c++)
  • ¥15 有会用octave 的吗,急!代做!好偿!
  • ¥15 有一套同城小程序源码,Uniapp前端,php+html+mysql后端 ,能不能教我搭建起来可以运行,我不知道怎样操作
  • ¥15 mac调用java.io接口无法在根目录生成文件
  • ¥15 java微服务节点假死,网关路由时长延迟