[Original] array go seven language learning

table of Contents

  • Array definition
  • Two-dimensional array
  • Copies the array, mass participation

Array definition

1. An array is a collection of elements of the same type.

A var [. 3] int 
// define an array 

// Go Array index starts from 0, so ⻓ length n of array subscript range: [0,. 1-n] 

// default group of integers initializes the elements 0 terminated string element in the array is initialized to a default ""

  

2. array initialization

A var [. 3] int 
A [0] = 10 
A [. 1] = 20 is 
A [2] = 30 
// initialize the array 

var A [. 3] = int [. 3] int {10, 20 is, 30} 
// defining array initialization 

a: = [3] int {10, 20 is, 30} 
// array initialization definition at 

A: = [...] {int 10, 20 is, 30} 
// define an array initialization 

a: = [3] int { 10} 
// the array initialization define 

a: = [3] int { 2:10} 
array initialization // define

  

3. The length of the array is part of the type of ⻓

A var [. 3] int 
A [0] = 10 
A [. 1] = 20 is 
A [2] = 30 
var B [. 5] int 
B = A 
// A, B are different types of arrays can not be assigned

  

4, len built-in functions

A var [. 3] int 
A [0] = 10 
A [. 1] = 20 is 
A [2] = 30 
fmt.Printf ( "len:% D \ n-", len (A)) 
// A, B are of different types the array can not be assigned

  

5. array traversal

var A [. 3] int 
A [0] = 10 
A [. 1] = 20 is 
A [2] = 30 
for I: = 0; I <len (A); I ++ { 
} 
// A, B is an array of different types of , can not be assigned

A var [. 3] int
A [0] = 10
A [. 1] = 20 is
A [2] = 30
for index, Val: Range A = {
}
// A, B are not different types of arrays, can not be assigned no

 

Two-dimensional array

1, two-dimensional array

var A [. 3] [2] int 
A [0] [0] = 10 
A [0] [. 1] = 20 is 
A [. 1] [0] = 30 
A [. 1] [. 1] = 30 
A [2] [ 0] = 30 
a [2] [. 1] = 30 
for index, Val: Range a = { 
} 
// a, B are not different types of arrays, can not be assigned no

  

2, two-dimensional array (b)

// array traversal writing two
testArra10 FUNC () { 
	// array a [length] Type 
	// definition is complete, the value of [0,0,0,100,300], to specify a particular index 
	A: = [. 5] {int. 3: 100,. 4: 300} 
	for index , value: Range A = { 
		fmt.Printf ( "A [% D] D =% \ n-", index, value) 
	} 
}

  

3, two-dimensional array (c)

// two-dimensional array definition 
FUNC testArra11 () { 
	var A [. 3] [2] int 
	A [0] [0] = 10 
	A [0] [. 1] = 20 is 
	A [. 1] [0] = 30 
	A [. 1 ] [. 1] = 40 
	A [2] [0] = 50 
	A [2] [. 1] = 60 

	fmt.Println (A) 

	// traverse the two-dimensional array 
	for I: = 0; I <. 3; I ++ { 
		for J : = 0; J <2; J ++ { 
			fmt.Printf ( "% D", A [I] [J]) 
		} 
		fmt.Println () 
	} 

	// traversal two 
	fmt.Println ( "OTHER Method") 
	for I , Val: Range A = { 
		//% V automatically match the output format, the line 
		fmt.Printf ( "row [D%] V =% \ n-", I, Val) 
		for J, val2: Range = {Val 
			fmt.Printf ( "(% D,% D) = D%", I, J, val2) 
		} 
	} 

}

  

 

Copies the array, mass participation

1, the array is a value type

a var [. 3] int 
a [0] 10 = 
a [. 1] = 20 is 
a [2] = 30 
B: = a 
// B a copy of all of the elements of the array 
B [0] = 1000 
fmt.Println (a , b)

  

2, an array type is a value, parameter passing function will copy

// value type, Copy full copy, change does not affect b A 
FUNC testArra12 () { 
	A: = [. 3] int {10, 20 is, 30} 
	b: A = 
	b [0] = 1000 
	fmt.Printf ( " V =% A \ n-", A) 
	fmt.Printf (" V b =% \ n-", b) 
} 

// int type are value types, copy full copy, change does not affect b A 
FUNC testArray13 () { 
	var int = A 1000 
	B: A = 
	B = 3000 
	fmt.Printf ( "% A = D = DB% \ n-", A, B) 
} 

// array for the parameter passing Copy 
FUNC Modify (B [. 3] int) { 
	B [0] = 1000 
}

  

example:

main Package 

Import ( 
	"FMT" 
	"Math / RAND" 
	"Time" 
) 

FUNC sumArray (A [10] int) {int 
	var SUM int = 0 
	// first traversal 
	for i: = 0; i < len (a) ; I ++ { 
		SUM = SUM + A [I] 
	} 

	// second traverse, the subscript shield _ 
	// _ for, Val: Range A = { 
	// Val SUM = SUM + 
	//} 

	return SUM 
} 

// random 10 numbers together 
FUNC testArraySum () { 

	// initialization random seed, time.Now () Unix () ns time. 
	rand.Seed (Time.now () the Unix ().) 

	var B [10] int 
	for I: 0 =; I <len (B); I ++ { 
		// B [I] = I 
		// RAND random 
		// generates a random number between 0 and 999 
		B [I] = rand.Intn (1000)  
		// generates a 0 the random number to the maximum Int
		// B [I ] = rand.Int ()
	}

	sum := sumArray(b)
	fmt.Printf("sum=%d\n", sum)
}

// 两个元素之和,等于8 target的下标
func TwoSum(a [5]int, target int) {
	for i := 0; i < len(a); i++ {
		other := target - a[i]
		for j := i + 1; j < len(a); j++ {
			if a[j] == other {
				fmt.Printf("(%d, %d)\n", i, j)
			}
		}
	}
}

//
func testTwoSum() {
	// var b [5]int = [5]int{1,3,5,8,7}
	// b := [5]int{1,3,5,8,7}
	b := [...]int{1, 3, 5, 8, 7}
	TwoSum(b, 8)
}

func main() {
	//sumArray()
	//testArraySum()
	testTwoSum()
}

  

 

Guess you like

Origin www.cnblogs.com/wangshuyang/p/11793024.html