Arrays and slices

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. [Original link] (https://yc90s.github.io/2017/11/20/%E6%95%B0%E7%BB%84%E4%B8%8E%E5%88%87%E7%89%87/) # #数组 # # # definition An array is a set of data of the same type, which is * * Value type * *, which accesses the element value from a 0-based subscript index. After initialization * * The array length is fixed * * and cannot be modified. When a parameter is passed in as a method, an array is copied instead of referencing the same pointer. The length of the array is also part of its type. # # # Initialization-an array of length 5 whose element values are: 1, 2, 3, 4, 5 "' [5]int{1, 2, 3, 4, 5} '-an array of length 5 with element values of 1, 2, 0, 0, 0, respectively. Elements that do not specify an initial value at initialization will be assigned the default values for their element type, int is the 0,string "" "" [5]int{1, 2} "-an array of length 5, whose length is determined by the number of elements specified at initialization" [...] Int{1, 2, 3, 4, 5} "-An array of length 5 whose element values are: 0, 0, 1, 2, 3. The corresponding values in the 2, 3, and 4 indexes were specified at initialization: 1, 2, 3 "' [5]int{2:1, 3:2, 4:3} '-an array of length 5 with element values of: 0, 0, 1, 0, 3. Because a value of 3 is specified for the maximum index of 4, the number of elements initialized depends on its length of 5 "[...] Int{2:1, 4:3} "# # # # # # # # # # # definition slices are built on arrays, but provide stronger functionality. The storage structure of a slice is the pointer to the underlying array, the slice length, the slice capacity, and the slice data. The slice length refers to the maximum subscript +1 that has been assigned a value, which can be obtained by Len (). Capacity refers to the maximum number of elements that a slice can currently hold, which can be obtained through the CAP (). * * The slice is a reference type * *, and * * The length and capacity of the tile can be modified * *, when the capacity of the tile changes, the memory may be reassigned, * * Causes the association of the slice and the original underlying array to break **### initialization-direct initialization, [] is the slice type, the initialization value is 1, 2, 3. Its cap=len=3 ' s: = []int{1, 2, 3} '-initialized by an array or other slice, is its reference ' s: = Arr[startindex:endindex] '--initialize with make () to specify Len and Cap ' s : = Make ([]iNT, Len, cap) ' ' slices re-allocating memory, causing the slices to be associated with the original underlying array in case of disconnection requires special attention, consider the following code: "' arr1: = [3]int{1, 2, 3}arr2: = [3]int{1, 2, 3}sli1: = Arr1[:1]s Li2: = arr2[:1]sli1[0] = 0sli2[0] = 0sli1 = Append (Sli1, 5) sli2 = Append (Sli2, 5, 5, 5) fmt. Println ("arr1:", arr1) fmt. Println ("arr2:", arr2) fmt. Println ("Sli1:", Sli1) fmt. Println ("Sli2:", Sli2) the output after execution is > arr1: [0 5 3]> arr2: [0 2 3]> sli1: [0 5]> Sli2: [0 5 5 5] sli1 to slice append Instead of re-allocating memory, it is also a reference to the original array, so the append operation on SLI1 is still applied to the array arr1, while the append operation on the slice Sli2 re-allocates memory so that its association with the original array is broken. So the append operation on the sli2 does not affect the array arr2## array and the slice Difference # # # array length is immutable, the length of the slice and the size of the variable group will be determined at initialization, and the length of the slice can be dynamically changed # # # array is a value type, The slice is a reference type with a piece of code explaining "func Testarr (Arg [3]int) {arg[0] = 0fmt. Println ("Testarr:", Arg)}func Testsli (arg []int) {if Len (arg) = = 0 {arg = append (arg, 0)} else {arg[0] = 0}fmt. Println ("Testsli:", Arg)}func main () {arr: = [3]int{1, 2, 3}fmt. Println ("Before Testarr ():", arr) Testarr (arr) fmt. Println ("After Testarr ():", arr) SLI: = Arr[:]fmt. Println ("Befor testsli ():", SLI) Testsli (SLI) FMT. Println ("After Testsli ():", SLI)} "" Program output after execution > before Testarr (): [1 2 3]> Testarr: [0 2 3]> after Testarr (): [1 2 3]&gt ; Befor testsli (): [1 2 3]> Testsli: [0 2 3]> after Testsli (): [0 2 3]testarr () function modify the arr parameter passed in, and the Testsli () letter is not valid for the ARR variable. Modification of the number of SLI variables write back to the SLI variable 223 reads  
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.