Slicing Slice theory knowledge
- It is not an array by itself, it points to the underlying array
- As an alternative to programming arrays, you can associate local or all of the underlying array
- As a reference type
- You can create directly or get a build from the underlying array
- Use Len () to get the number of elements, and the cap () to get the capacity
- General use make () to create
- If multiple slice point to the same underlying array, one of the value changes affects all
- Make ([]t, Len, CAP)
- Where the cap can be omitted, and the value of Len is the same
- Len represents the number of elements stored, and the CAP represents the capacity
The relationship between slice and the underlying array
Reslice
- Reslice indexes are subject to slice slices
- Index can not exceed the value of the capacity cap () of the slice slice
- Index out-of-bounds does not cause redistribution of the underlying array but throws an error
Append
- You can append elements to the slice tail
- One slice can be appended to the other slice tail
- Returns the original slice if the final length does not exceed the capacity appended to the slice
- If the capacity of the appended slice is exceeded, the array is reassigned and the original data is copied
Copy
Slice Slice Practice Package Mainimport "FMT" Func Main () {//============ first create an array =//Create an array, must indicate the number of age: = [4]int{} FM T.println (age)//========= declares a slice slice type =====//See, slice type, is not need to specify the number of//remember, slice type of the bottom, is also the array var s1 []float64 Fmt. Println (S1)//======== mode one: Use an existing array to initialize the S1 sparknodecpu: = [...] float64{4.5, 3.4, 2.3, 9.8, ten} S1 = Sparknodecpu[3:5]//from the subscript 3 element, start acquiring FMT. Println ("------------->\t", s1) S1 = sparknodecpu[1:]//From the subscript 1 element, get to the last FMT. Println (S1) S1 = Sparknodecpu[:4]//Specifies the last acquired element of FMT. Println (S1)//======== mode two: Use the Make keyword to create a slice//make ([]t, Len, CAP)//[]t, which represents the type//len of the slice, the number of current elements, or the number of initializations//c AP, because the bottom of the slice is actually an array, and the array in memory is a contiguous space,//such as make ([]int, 3,10) in order to improve efficiency, generally first in memory to create//a piece of contiguous memory size, 10, if you have more than 10 elements of the number,/ /go language, will default to your current memory space, from 10, increased to 20, re-find a contiguous memory in the space, allocate 20 space//If more than 20, the allocation of 40, so go on. So it's best that you know how much memory you need s2: = make ([]int, 3, +) FMT. Println (len (S2), Cap (S2))//3 S2A: = Make ([]int, 3)//can also not specify CAP, in this case, the default value of the CAP is Len's length fmt. Println (Len (s2a), Cap (S2A))//3 3//=======================reslice======== Practice =====//Declare a slice type a: = []rune{' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' j ', ' k ', ' u '}//based on the original slice A, start cutting//At this point, you have to pay attention to the newly generated slice S3 capacity//The following operation is Reslice s3: = A[2:5]/ /indicated, starting from subscript 2, to 5 end, maximum cannot exceed the cap capacity of the cut slice 7 s3a: = S3[3:5] FMT. Println ("Length: \ T", Len (S3), "\ t capacity cap:\t", Cap (S3))//3 7 FMT. Println ("Length: \ T", Len (s3a), "\ t capacity cap:\t", Cap (S3A))//2 4 fmt. Println (String (s3a))//=======================append======== practice ===== Slice1: = Make ([]int, 3, 6)// The default value is all 0 fmt. Printf ("Print out Slice1 memory address: \ t%p\n", Slice1)//0xc042078090//Append 3 elements First Slice1 = append (Slice1, 1, 3, 5)//print out Slice1 The value, and the memory address of FMT. Printf ("%v%p\n", Slice1, Slice1)//[0 0 0 1 3 5] 0xc042078090//Continue appending elements, viewing, memory address, whether the change has occurred Slice1 = append (Slice1, 4, 5, 6)//After Slice1 capacity has been exceeded, the memory address FMT is reassigned. Printf ("%v%p\n", Slice1, Slice1)//[0 0 0 1 3 5 4 5 6] 0xc042056060 FMT. Println ("======================================= ")//test, when multiple slices point to the same underlying array, and when multiple slices have a common element, if one of the elements changes,//other slices also change appleslic E: = []int{3, 4, 5, 9, 8, 7} BananaS: = Appleslice[3:6] Oranges: = Appleslice[2:5]//bananas oranges There are two common elements Fmt. Println (Appleslice)//[3 4 5 9 8 7] FMT. Println (BananaS)//[9 8 7] FMT. Println (oranges)//[5 9 8] FMT. Println ("=======================================")//assuming the elements in the bananas slice, the 1th element, has changed,//test, Appleslice,oranges Has it changed?//I changed the element labeled 1 with a value of 8 and changed 8 to 110, while in the other slices 8 of the values were changed to 110 bananas[1] = 110//The element labeled 1 is set to the FMT. Println (Appleslice)//[3 4 5 9 7] FMT. Println (BananaS)//[9 7] FMT. Println (oranges)//[5 9] fmt. Println ("=======================================")//Note that at this point, Oranges,oranges is still performing the underlying array Appleslice//Also pay attention to the following scenario//exploit Append Method BananaS = Append (BananaS, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7)//At this time, BananaS is no longer pointing to Appleslice, because more than The capacity of the appleslice//instead, pointing to the new address, at which point IWe modify BananaS bananas[1] = 1110 FMT. Println (Appleslice)//[3 4 5 9 110 7]//saw the put, just modified their values, Appleslice,oranges did not change the FMT. Println (BananaS)//[9 1110 7 7 7 7 7 7 7 7 7 7 7 7 7 7] FMT. Println (oranges)//[5 9] fmt. Println ("=======================================")//=======================copy======== exercise =====//Copy (A, b) is that Copy the elements of B into a ftpnum: = []int{4, 6, 7, 2, 3, 8, 9} sftpnum: = []int{1, 2, 3}//Will SFTP copy (Ftpnum, sftpnum)//Note , after copy, it will be the original value of the FMT. Println (Ftpnum) fmt. PRINTLN (Sftpnum)//Intercept copy copy (Sftpnum[1:2], Ftpnum[2:3]) fmt. Println (Ftpnum) fmt. Println (Sftpnum)}
The Go language slice slice exercise