Go by Example: Arrays

Go语言的数组是一个定长的序列,数组包含的元素的类型相同。

package main

import "fmt"

func main() {

    // 这里我们创建了一个具有5个元素的整型(int)数组
    // 元素的数据类型和数组长度都是数组类型的一部分
    // 默认情况下,数组元素都是零值。
    // 对于整数,零值就是0
    var a [5]int
    fmt.Println("emp:", a)

    // 我们可以使用索引值(index)来设置数组元素的值,就像这样"array[index] = value"
    // 或者使用索引来获取元素值, 如 "array[index]"
    a[4] = 100
    fmt.Println("set:", a)
    fmt.Println("get:", a[4])

    // 内置的len函数返回数组的长度
    fmt.Println("len:", len(a))

    // 使用下列方法可以同时定义和初始化一个数组
    b := [5]int{1, 2, 3, 4, 5}
    fmt.Println("dcl:", b)

    // 数组都是一维的,但是你可以把数组的元素定义为一个数组
    // 来获取多维数组结构
    var twoD [2][3]int
    for i := 0; i < 2; i++ {
        for j := 0; j < 3; j++ {
            twoD[i][j] = i + j
        }
    }
    fmt.Println("2d: ", twoD)
}

输出

$ go run arrays.go
emp: [0 0 0 0 0]
set: [0 0 0 0 100]
get: 100
len: 5
dcl: [1 2 3 4 5]
2d:  [[0 1 2] [1 2 3]]

当你使用fmt.Println输出数组时候,你会发现数组会以[v1,v2,v3...]形式被打印。

在经典的Go语言中,相比与数组(Array)你会更多的遇到切片(slice)。

要了解更多关于数组,请查看学习Golang语言(5): 类型--数组

下一个章节将会讲解切片。

下一个例子:  Go by Example:  Slices.

英文原文

时间: 2024-08-04 09:00:01

Go by Example: Arrays的相关文章

[LeetCode] 349 Intersection of Two Arrays &amp; 350 Intersection of Two Arrays II

这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/intersection-of-two-arrays/description/ 350 Intersection of Two Arrays II:https://leetcode.com/problems/intersection-of-two-arrays-ii/description/ 题目&解法

Arrays工具类

Arraysd的静态方法能够方便的对数组进行操作,每个方法也加了注释 : 程序: import java.util.*;public class Array{        public static void main(String[] args){                int[]  arr={1,3,4,2};                System.out.println("排序前:");                printArray(arr);//打印原数组

350.求两个数组的交集 Intersection of Two Arrays II

Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2]. Note: Each element in the result should appear as many times as it shows in both arrays. The result can be in any ord

ArrayList&amp;LinkedList&amp;Map&amp;Arrays

Java集合框架 1:集合接口 1.1:Collection接口 Collection接口是构造集合框架的基础.它声明所有类集合都将拥有的核心方法 Boolean add(Object obj) 将obj加入到调用类集合中,加入返回true 否则 返回 false Boolean addAll(Collection c) 将c中所有元素都加入到类集合中,都加入返回true否则 false Void clean() 从调用类集合中删除所有元素 Boolean contains(Object obj

LeetCode OJ 4. Median of Two Sorted Arrays

There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Example 1: nums1 = [1, 3] nums2 = [2] The median is 2.0 Example 2: nums1 = [1,

leetcode----------------Median of Two Sorted Arrays

There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 分析 这是一道非常经典的题.这题更通用的形式是,给定两个已经排序好的数组,找到两者所有元素中第 k 大的元素.O(m + n) 的解法比较直观,直接merge两个数组,然后

4. Median of Two Sorted Arrays

There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Example 1: nums1 = [1, 3] nums2 = [2] The median is 2.0 Example 2: nums1 = [1,

Arrays类

import java.util.Arrays; public class Person {    public static void main(String[] args) {        int[] intArray = new int[9];        for (int i=0; i<intArray.length; i++) {            intArray[i] = i;        }        intArray[5] = 100;        print(

[leetcode-349-Intersection of Two Arrays]

Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2]. Note: Each element in the result must be unique. The result can be in any order. 参考思路: 找两个数组相同的部分,难度不算大,我们可以用个set把nums1都

【集合框架】JDK1.8源码分析之Collections &amp;&amp; Arrays(十)

一.前言 整个集合框架的常用类我们已经分析完成了,但是还有两个工具类我们还没有进行分析.可以说,这两个工具类对于我们操作集合时相当有用,下面进行分析. 二.Collections源码分析 2.1 类的属性   2.2 构造函数 private Collections() { } 说明:私有构造函数,在类外无法调用. 2.3 方法分析 下面是Collections的所有方法. 可以看到,Collections的方法包含了各种各样的操作.下面分析最常用的方法. 1. sort函数 该函数有两个重载函