Use complex array - array reverse
Requirements: five randomly generated number, and reverse print
By reversing find the law
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
Thinking //
// 1. Five randomly generated number, rand.Intn () function
// 2. When we get the random number, it int array into an array
// 3 The reverse printing, len is the number of exchanges / 2, a reciprocal of the first and second switching element,
// penultimate 2 and the second switching elements
the intArr var [. 5] int
len: = len (the intArr)
// for each generated random number is not the same, we need to give a seed value
rand.Seed (Time.now () UnixNano ().)
for I: = 0 ; I <len; I ++ {
the intArr [I] = rand.Intn (100) // 0 <= n-<100
}
fmt.Println ( "pre-exchange = ~", the intArr)
TEMP: = 0 // make a temporary variable
I for: = 0; I <len / 2; I ++ {
TEMP = the intArr [len -. 1 - I] // the value assigned to a genus inverted temporary variable
intArr [len - 1 - i] = intArr [i] / / n the value is assigned to the genus genus inverted position
intArr [i] = temp // the value assigned to the genus inverted value of the temporary variable is assigned to a position directly genus
}
fmt.Println ( "after switching ~ =", intArr )
}
Why do I need a slice?
Look at a demand: we need to hold an array of student achievement, but the number of students is uncertain, how do I ask? Solution: Use sliced
Slice the basic description:
1) English is the slice sliced
2) is a reference to the array slice, slice is a reference type and therefore, during transfer, the transfer mechanism compliance reference.
3) the use of similar sections and array traverse slice, slice access request elements and slicing length len (slice) are the same.
4) the length of the sections can be varied, so that a slice arrays may vary dynamically.
The basic syntax 5) sections defined:
var slice name [] types
such as: var a [] int
quick start slice:
FUNC main () {
the intArr var [. 5] int = [...] {int. 1, 22 is, 33 is, 66, 99}
// declare / define a slice
//1.slice slice name is
//2.intArr[1:3] represents refer to the array slice intArr
@ 3 starting reference intArr array subscript of 1, the final subscript 3 (but does not contain 3).
slice: intArr = [1: 3]
fmt.Println ( "= intArr" , the intArr)
fmt.Println ( "slice elements are =", slice) // 22,33
fmt.Println ( "the number of slice elements =", len (slice)) // 2
fmt.Println ( "slice of capacity = ", cap (slice)) // slice capacity can be dynamically changed.
}
Slice in memory in the form of:
1) to the front of the case to analyze slice layout in memory
2) the underlying data structure of a slice will be understood to be a structure struct
3) address output slices and reference
Slice in three ways using:
Mode 1:
The first way: the definition of a slice, slice and then let go already created a reference to an array, such as the front of the case is this.
Option 2:
The second way: to create a slice by make
The basic syntax: var slice name [] type = make ([] type, len, [cap])
Parameters: type: Type is the data len: Size cap: capacity specified slice, optional. If the distribution cap, requires cap> = len
Case presentation:
Summary of the above code:
1) Create a way to make a slice by slice you can specify the size and capacity
2) If no element is assigned to each slice, it will use the default value [int, float => 0, string => "", bool => false]
3) corresponding to the array of slices created by the make make way maintained by the underlying foreign invisible. That can only be accessed by slice to each element
Mode 3:
The third way: the definition of a slice directly to the specified array, using the principles make a similar fashion.
Case presentation:
func main() {
var slice []int = []int {1,3,5}
fmt.Println(slice)
}
func main() {
var strSlice []string = []string {"tom", "jack", "marry"}
fmt.Println("strSlice=", strSlice)
fmt.Println("strSlice size=", len(strSlice)) //3
fmt.Println("strSlice cap=", cap(strSlice)) //3
}
Use slices difference analysis:
Modes 1 and 2 of the difference:
Mode 1 is a direct reference to the array, the array is pre-existing, the programmer is visible.
方式2是通过make 来创建切片,make也会创建一个数组,是由切片在底层进行维护,程序员是看不见的。
make创建切片的示意图: