GO語言基礎之數組_Golang

來源:互聯網
上載者:User

1.申明一個數組 var a[2] int 或者 a:=[2]int{1,2}

2.數組索引
數組就是索引的來建立如下圖

我們再來一個測試

3.go語言可以自動計算數組的長度,譬如你知道數組有幾個可以如下申明

複製代碼 代碼如下:

a:=[...]int{1,2,3,45}

4.指標數組

複製代碼 代碼如下:

a:=[3]int{1,2,3}
var p * [3]int = &a //這種是指標數組 我們看到可以直接輸出指向數組的指標
x , y :=1 ,3
a := [...]*int{&x ,&y}
str.Println(a) //輸出這樣[0xc080000068 0xc080000070]的地址 這就是數組指標

可以用new關鍵字申明

複製代碼 代碼如下:

p := new([10]int)
fmt.Println(p)  //&[0 0 0 0 0 0 0 0 0 0] 輸出一個指標

多維陣列跟其他語言一樣

複製代碼 代碼如下:

c := [3][2]int{{1: 2}, {2, 1}, {2, 2}}
fmt.Println(c) //輸出[[0 2] [2 1] [2 2]]

冒泡演算法之go語言版

複製代碼 代碼如下:

package main

import "fmt"

func main() {
 a := [...]int{3, 2, 5, 8, 6}
 fmt.Println(a)
 num := len(a)
 for i := 0; i < num; i++ {
  for j := i + 1; j < num; j++ {
   if a[i] < a[j] {
    temp := a[i]
    a[i] = a[j]
    a[j] = temp
   }
  }
 }
 fmt.Println(a)
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.