加入收藏 | 设为首页 | 会员中心 | 我要投稿 PHP编程网 - 钦州站长网 (https://www.0777zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

Go - 多维数组(15.1/24)

发布时间:2022-11-28 13:00:09 所属栏目:PHP教程 来源:
导读:  Go语言支持多维数组,这是一个多维数组定义的通常模式 -

  var variable_name [SIZE1][SIZE2]...[SIZEN] variable_type
  下面的例子定义了三维整型数组:5,10,4 -

  var threedim [5][10][4]i
  Go语言支持多维数组,这是一个多维数组定义的通常模式 -
 
  var variable_name [SIZE1][SIZE2]...[SIZEN] variable_type
  下面的例子定义了三维整型数组:5,10,4 -
 
  var threedim [5][10][4]int
  二维数组
 
  二维数组是最简单的多维数组。一个二维数组本质上是一个一维数组的列表。为了定义大小为[x, y]的二维整型数组,语句如下 -
 
  var arrayName [ x ][ y ] variable_type
  variable_type可以是任何Go的数据类型PHP多维数组,arrayName可以是任何有效的Go标识符。可以将二维数组想象成一张表:拥有x个行,y个列。一个拥有3行4列的二维数组,如下所示 -
 
  每一数组中的元素通过a[i][j]的方式访问,其中a是数组的名字,i和j是标识数组a中各个元素的下标。
 
  初始化二维数组
 
  多维数组初始化可以通过把每行的值放置在括号中进行。下面例子中的数组拥有3行数据,每行拥有4列数据。
 
  a = [3][4]int{  
     {0, 1, 2, 3} ,   /*  initializers for row indexed by 0 */
     {4, 5, 6, 7} ,   /*  initializers for row indexed by 1 */
     {8, 9, 10, 11}   /*  initializers for row indexed by 2 */
  }
  访问二维数组元素
 
  二维数组中的每一元素访问通过索引来继续,比如行和列的数组索引。示例如下 -
 
  int val = a[2][3]
  上面的语句访问了数组的第三行数据中的,第四个元素。可以验证上述的例子。考虑下面的例子:内嵌循环处理二维数组 -
 
  package main
  import "fmt"
  func main() {
     /* an array with 5 rows and 2 columns*/
     var a = [5][2]int{ {0,0}, {1,2}, {2,4}, {3,6},{4,8}}
     var i, j int
     /* output each array element's value */
     for  i = 0; i < 5; i++ {
        for j = 0; j < 2; j++ {
           fmt.Printf("a[%d][%d] = %d\n", i,j, a[i][j] )
        }
     }
  }
  编译并运行,结果如下 ?
 
  a[0][0]: 0
  a[0][1]: 0
  a[1][0]: 1
  a[1][1]: 2
  a[2][0]: 2
  a[2][1]: 4
  a[3][0]: 3
  a[3][1]: 6
  a[4][0]: 4
  a[4][1]: 8
  如上述的例子所演示的,你可以创建任意维度的数组,尽管我们一般创建的数组大多是一维或二维的数组。
 
  原文 Go - Multidimensional Arrays in Go
 
  Go programming language allows multidimensional arrays. Here is the general form of a multidimensional array declaration ?
 
  var variable_name [SIZE1][SIZE2]...[SIZEN] variable_type
  For example, the following declaration creates a three dimensional 5 . 10 . 4 integer array ?
 
  var threedim [5][10][4]int
  Two-Dimensional Arrays
 
  A two-dimensional array is the simplest form of a multidimensional array. A two-dimensional array is, in essence, a list of one-dimensional arrays. To declare a two-dimensional integer array of size [x, y], you would write something as follows ?
 
  var arrayName [ x ][ y ] variable_type
  Where variable_type can be any valid Go data type and arrayName will be a valid Go identifier. A two-dimensional array can be think as a table which will have x number of rows and y number of columns. A 2-dimensional array a, which contains three rows and four columns can be shown as below ?
 
  Thus, every element in the array a is identified by an element name of the form a[ i ][ j ], where a is the name of the array, and i and j are the subscripts that uniquely identify each element in a.
 
  Initializing Two-Dimensional Arrays
 
  Multidimensional arrays may be initialized by specifying bracketed values for each row. Following is an array with 3 rows and each row has 4 columns.
 
  a = [3][4]int{  
     {0, 1, 2, 3} ,   /*  initializers for row indexed by 0 */
     {4, 5, 6, 7} ,   /*  initializers for row indexed by 1 */
     {8, 9, 10, 11}   /*  initializers for row indexed by 2 */
  }
  Accessing Two-Dimensional Array Elements
 
  An element in two dimensional array is accessed by using the subscripts, i.e., row index and column index of the array. For example ?
 
  int val = a[2][3]
  The above statement will take the 4th element from the 3rd row of the array. You can verify it in the above diagram. Let us check below program where we have used nested loop to handle a two dimensional array ?
 
  package main
  import "fmt"
  func main() {
     /* an array with 5 rows and 2 columns*/
     var a = [5][2]int{ {0,0}, {1,2}, {2,4}, {3,6},{4,8}}
     var i, j int
     /* output each array element's value */
     for  i = 0; i < 5; i++ {
        for j = 0; j < 2; j++ {
           fmt.Printf("a[%d][%d] = %d\n", i,j, a[i][j] )
        }
     }
  }
  When the above code is compiled and executed, it produces the following result ?
 
  a[0][0]: 0
  a[0][1]: 0
  a[1][0]: 1
  a[1][1]: 2
  a[2][0]: 2
  a[2][1]: 4
  a[3][0]: 3
  a[3][1]: 6
  a[4][0]: 4
  a[4][1]: 8
  As explained above, you can have arrays with any number of dimensions, although it is likely that most of the arrays you create will be of one or two dimensions.
 

(编辑:PHP编程网 - 钦州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!