【GO Programming Language】Array

array



insert image description here


1. What is an array

Go language provides a data structure of array type.

  • An array is a numbered, fixed-length sequence of data items of the same unique type, which can be any primitive type. Such as integer, string or custom type.

  • Array elements can be read (or modified) by index (position), the index starts from 0, the first element index is 0, the second index is 1, and so on. The subscript value of the array starts from 0 and ends with the length minus one.

Once an array is defined, its size cannot be changed.
insert image description here
Array declaration

The array declaration in Go language needs to specify the element type and the number of elements. The syntax format is as follows:

var 变量名 [大小] 变量类型
package main

import "fmt"

func main() {
    
    
	// 定义一个数组  var 变量名 [大小]变量类型
	var nums [4]int

	// 数组的下标是从 0 开始的
	nums[0] = 1
	nums[1] = 2
	nums[2] = 3
	nums[3] = 4
	fmt.Printf("%T\n", nums)
	// 通过下标来获取数组元素
	fmt.Println(nums[3])
	fmt.Println(nums[2])
	fmt.Println(nums[1])
	fmt.Println(nums[0])

	// 数组常用的方法
	// 因为数组是固定长度的,所以长度和容量是相同的
	length := len(nums)
	capacity := cap(nums)
	fmt.Println(length)   // 输出长度
	fmt.Println(capacity) // 输出容量

	// 修改数组的值
	fmt.Println(nums)
	nums[0] = 6
	fmt.Println(nums)

}

insert image description here

数组的定义:

  • An array is an ordered collection of data of the same type
  • An array describes several data of the same type, sorted and combined according to a certain sequence
  • Among them, each piece of data is called an array element, and each array element can be accessed through a subscript

数组的基本特点:

  • Its length is fixed. Once an array is created, its size cannot be changed.
  • Its elements must be of the same type, mixed types are not allowed

2. Initialize the array

The elements of {} in the initialization array cannot be larger than the number in [].

package main

import "fmt"

func main() {
    
    
	// 常规的初始化
	var arr1 = [4]int{
    
    1, 2, 3, 4}

	fmt.Println(arr1)
	fmt.Println(arr1[0])

	// 快速定义数组
	arr2 := [4]int{
    
    5, 6, 7, 8}
	fmt.Println(arr2)
	fmt.Println(arr2[0])

	// 如果不确定数组长度
	// 可以使用 ...  代替数组的长度
	// 编译器会根据元素个数自行推断数组的长度
	arr3 := [...]string{
    
    "hello", "world", "!"}
	fmt.Println(arr3)
	fmt.Println(len(arr3))
	fmt.Println(cap(arr3))
	
	// 如果设置了数组的长度,我们可以通过指定下标来初始化元素
	arr4 := [4]int{
    
    1: 9, 3: 10}
	fmt.Println(arr4)

}

output
insert image description here

Third, the traversal of the array

package main

import "fmt"

/*
数组的遍历:
依次访问数组中的元素
方法1, arr[0],arr[1],arr[2]....
方法2:通过循环,配合下标
for i:=0;i<len(arr);i++{
	arr[i]
}
方法3: range
range:词义“范围”
不需要操作数组的下标,到达数组的末尾,自动结束 for range循环
每次从数组中获取下标和对应位置的数值
*/

func main() {
    
    
	// 常规的初始化
	var arr1 = [4]int{
    
    1, 2, 3, 4}

	fmt.Println(arr1[0])
	fmt.Println(arr1[1])
	fmt.Println(arr1[2])
	fmt.Println(arr1[3])

	fmt.Println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
	//采用for循环遍历输出
	for i := 0; i < 4; i++ {
    
    
		fmt.Println(arr1[i])
	}

	// 快捷键生成 arr1.for
	// for range 可以得到 下标(index) 和 下标对应的值(value)
	for index, value := range arr1 {
    
    
		fmt.Println(index, value)
	}
}

output
insert image description here

4. Array type

package main

import "fmt"

func main() {
    
    
	num := 8
	var arr1 = [4]int{
    
    1, 2, 3, 4}
	var arr2 = [4]string{
    
    "hello", "wrold", "!"}

	fmt.Printf("%T", arr1)
	fmt.Println()
	fmt.Printf("%T", arr2)

	// 值类型:拷贝 arr
	num2 := num
	fmt.Println(num, num2)
	num2 = 10
	fmt.Println(num, num2)
	fmt.Println(">>>>>>>>>>>>>>>>>>>>>>>>>>")

	arr3 := arr1
	fmt.Println(arr1)
	fmt.Println(arr3)
	fmt.Println(">>>>>>>>>>>>>>>>>>>>>>>>>>")
	// 改变arr3并没有影响到arr1,数组也是值传递
	arr3[0] = 66
	fmt.Println(arr1)
	fmt.Println(arr3)

}

output
insert image description here

Five, array sorting

  • Array sorting: Let the elements in the array have a certain order.
arr :=[5]int{
    
    3,5,4,2,1}

升序:1,2,3,4,5
降序:5,4,3,2,1

chestnut:

Sorting Algorithms: Bubble Sort, Insertion Sort, Selection Sort, Hill Sort, Heap Sort, Quick Sort...

  • Bubble Sort

Compare two adjacent elements in turn, and exchange them if they are in order (such as from small to large).

package main

import "fmt"

func main() {
    
    
	//3,5,4,2,1
	//第一轮比较:3,4,2,1,5
	//第二轮比较:3,2,1,4,5
	//第三轮比较:2,1,3,4,5
	//第四轮比较:1,2,3,4,5
	//第五轮比较:1,2,3,4,5
	arr := [5]int{
    
    3, 5, 4, 2, 1}
	//比较过后的数,在后面的比较还需要重新比较,这是多余的,增加了执行比较的次数
	for j := 0; j < len(arr); j++ {
    
    
		for i := 0; i < len(arr)-1; i++ {
    
    
			if arr[i] > arr[i+1] {
    
    
				arr[i], arr[i+1] = arr[i+1], arr[i]

			}
		}
		fmt.Println(arr)
	}

}

insert image description here

Reduce the number of comparisons performed after optimization

package main

import "fmt"

func main() {
    
    
	//3,5,4,2,1
	//第一轮比较:3,4,2,1,5
	//第二轮比较:3,2,1,4,5
	//第三轮比较:2,1,3,4,5
	//第四轮比较:1,2,3,4,5
	//第五轮比较:1,2,3,4,5
	arr := [5]int{
    
    3, 5, 4, 2, 1}

	for j := 1; j < len(arr); j++ {
    
    
		//两两比较,比较完成的数不用比较,即可减少执行比较的次数
		for i := 0; i < len(arr)-j; i++ {
    
    
			if arr[i] > arr[i+1] {
    
    
				arr[i], arr[i+1] = arr[i+1], arr[i]

			}
		}
		fmt.Println(arr)
	}

}

insert image description here

package main

import "fmt"

func main() {
    
    
	//3,5,4,2,1
	//第一轮比较:3,4,2,1,5
	//第二轮比较:3,2,1,4,5
	//第三轮比较:2,1,3,4,5
	//第四轮比较:1,2,3,4,5
	//第五轮比较:1,2,3,4,5
	arr := []int{
    
    3, 5, 4, 2, 1, 6, 9, 8, 7, 0}
	bubbleSort(arr)

}
// 函数的封装
func bubbleSort(arr []int) {
    
    
	//需要比较的次数的一组数的个数-1
	for j := 1; j < len(arr); j++ {
    
    
		//两两比较,比较完成的数不用比较,即可减少执行比较的次数
		for i := 0; i < len(arr)-j; i++ {
    
    
			if arr[i] > arr[i+1] {
    
    
				arr[i], arr[i+1] = arr[i+1], arr[i]

			}
		}
		fmt.Println(arr)
	}
}

Ascending output
insert image description here
Ascending and descending output

package main

import "fmt"

func main() {
    
    
	//3,5,4,2,1
	//第一轮比较:3,4,2,1,5
	//第二轮比较:3,2,1,4,5
	//第三轮比较:2,1,3,4,5
	//第四轮比较:1,2,3,4,5
	//第五轮比较:1,2,3,4,5
	arr := []int{
    
    3, 5, 4, 2, 1, 6, 9, 8, 7, 0}
	bubbleSort(arr, "desc")

}

func bubbleSort(arr []int, c string) {
    
    
	//需要比较的次数的一组数的个数-1
	for j := 1; j < len(arr); j++ {
    
    
		//两两比较,比较完成的数不用比较,即可减少执行比较的次数
		for i := 0; i < len(arr)-j; i++ {
    
    
			if c == "asc" {
    
    
				if arr[i] > arr[i+1] {
    
    
					arr[i], arr[i+1] = arr[i+1], arr[i]
				}
			}

			if c == "desc" {
    
    
				if arr[i] < arr[i+1] {
    
    
					arr[i], arr[i+1] = arr[i+1], arr[i]
				}
			}
		}
		fmt.Println(arr)
	}
}

Descending output
insert image description here

Six, multidimensional array

The Go language supports multi-dimensional arrays, and the following are common ways to declare multi-dimensional arrays:

var 变量名 [size1] [size2]...[size] 变量类型

A two-dimensional array is the simplest multi-dimensional array. A two-dimensional array is essentially composed of a one-dimensional array. The definition of a two-dimensional array is as follows:

var 变量名 [x][y] 变量类型

A two-dimensional array can be considered as a table, where x is the row and y is the column. The following table demonstrates that a two-dimensional array arr has three rows and four columns:

arr column 0 column 1 column 2 column 3
row 0 arr[0] [0] arr[0] [1] arr[0] [2] arr[0] [3]
line 1 arr[1] [0] arr[1] [1] arr[1] [2] arr[1] [3]
line 2 arr[2] [0] arr[2] [1] arr[2] [2] arr[2] [3]

Output array elements by array subscript

package main

import "fmt"

func main() {
    
    
	arr := [3][4]int{
    
    
		{
    
    1, 2, 3, 4},    // row1
		{
    
    5, 6, 7, 8},    // row2
		{
    
    9, 10, 11, 12}, // row3
	}
	// 访问二维数组的行列
	fmt.Println(arr[0][0])
	fmt.Println(arr[0][1])
}

By traversing the output array elements

package main

import "fmt"

func main() {
    
    
	arr := [3][4]int{
    
    
		{
    
    1, 2, 3, 4},    // row1
		{
    
    5, 6, 7, 8},    // row2
		{
    
    9, 10, 11, 12}, // row3
	}
	 访问二维数组的行列
	//fmt.Println(arr[0][0])
	//fmt.Println(arr[0][1])
	for i := 0; i < 3; i++ {
    
    
		for j := 0; j < 4; j++ {
    
    
			fmt.Println(arr[i][j])
		}
	}
	for i, v := range arr {
    
    
		fmt.Println(i, v)
		for _, v2 := range v {
    
    
			fmt.Println(v2)
		}

	}
}

insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/guanguan12319/article/details/130598287