Arrays and slices 4

Traverse sections:

Slice traversal and arrays, there are two ways

  1) for a conventional manner to traverse cycle

  2) for-range traverse slice structure


Both approaches in the following code:

Case presentation:

FUNC main () {

  // loop through the use of conventional sections for
  var ARR [. 5] int = [...] {int 10,20,30,40,50}
  Slice: ARR = [. 1:. 4] @ 20 is, 30, 40
  for I: = 0; I <len (Slice); I ++ {
  fmt.Printf ( "Slice [% V]% = V", I, Slice [I])
}

fmt.Println ()
  // used for - range traverse a slice
  for I, V: = Range Slice {
    fmt.Printf ( "% I = V VV =% \ n-", I, V)
  }
}

 

Notes and details slices description:

When slice initialization var slice = arr [startIndex: endIndex ]
Description: From the subscript of the array arr startIndex, taking the index of the element endIndex, free arr [endIndex].

When sliced ​​initialization, it is still not out of range. Range between [0-len (arr)], but may grow dynamically.

  1) var slice = arr [0 : end] can be abbreviated var Slice = ARR [: End]
  2) var Slice = ARR [Start: len (ARR)] can be abbreviated: var Slice = ARR [Start:]
  . 3) var Slice = arr [0: len (arr )] can be abbreviated: var slice = arr [:]


cap is a built-in function, capacity for statistical sections, ie, the maximum number of elements can be stored.

After slicing the definition, can not be used, because an empty itself, the need to let an array reference, or make use of a space for the slices

We can continue to slice sliced

 

Append a built-in function, can be dynamically added to the slice

func main() {

  // append with built-in functions can be dynamically added to sliced

  was slice3 = [] {int 100, 200, 300}

  // to slice3 additional specific elements directly append

  slice3 = append(slice3, 400, 500, 600)

  // append by the slice slice3 added to slice3

  slice3 = append(slice3, slice3...)

  fmt.Println("slice3=", slice3)
}

The underlying principle slice append operations analysis:

  1) the nature of the operation is to append a slice of an array expansion

  2) go bottom creates a new array newArr (according to size after expansion)

  3) The elements contained in the original slice copied to the new array newArr

  4) slice reference again to newArr

  5) Note newArr is at the bottom to maintain, the programmer is not visible

  6) Case demonstration shows:

 

 

Slice copy operation:

Slice using built-in functions to complete copy copy exemplified:

func main() {

  var slice4 []int = []int {1,2,3,4,5}
  var slice5 = make([]int, 10)
  copy(slice5, slice4)
  fmt.Println("slice4=", slice4)
  fmt.Println("slice5=", slice5)
}


The above description of the code:

(1) copy (para1, para2): Data slice type is type parameter.

(2) according to the code view of the above, the data space of Slice 4 and are independent of Slice 5, independently of each other phase, i.e. slice4 [0] = 999, slice5 [0] 1 remains.


Questions: The following code has no error:

A var [] = int [] int {1,2,3,4,5}
var = Slice the make ([] int,. 1)
fmt.Println (Slice) 0 //
Copy (Slice, A) // This is this report is not long enough wrong, although only one length. How much length, copy it over several values
fmt.Println (slice) // 1

Note: The above code is no problem, you can run, the final output is the result of [1]

 

 

 

 

 

 

 

 

 

 

 

string and slice:

. 1) underlying string is an array of byte string thus slicing processing may be performed

Case presentation:

func main() {

  str := "hello@atguigu"
  slice := str[6:]
  fmt.Println("slice=",slice)
}

2) string in the form of memory chips and to "abcd" shown a schematic view of the memory

 

3) string is immutable, it can not be said str [0] = 'z' way to modify the string

4) If you need to modify the string, first string -> [] byte / or [] Rune -> Modify -> rewrite string converted into

func main() {

  str := "hello@atguigu"
  slice := str[6:]
  fmt.Println("slice=",slice)

  // str [0] = 'z' [compiled by not being given, because the string is immutable]

  // arr1 := []byte(str)
  // arr1[0] = 'z'
  // str = string(arr1)
  // fmt.Println("str=", str)

  // details, we turn to [] after byte, can handle English and numbers, but can not handle Chinese
  // The reason is [] byte by byte to handle, and a Chinese character is three bytes, so there will be garbled
  // the solution is transformed into the string [] Rune to as [] Rune is processed by character, compatible with Chinese characters
  of arr1: = [] Rune (STR)
  of arr1 [0] = 'North'
  STR = string (of arr1)
  FMT .Println ( "STR =", STR)
}

 

Guess you like

Origin www.cnblogs.com/green-frog-2019/p/11355508.html