Go、Java和C#中数组的最大长度是多少?

huangapple go评论89阅读模式
英文:

What is the maximum length of an array in Go, Java and C#?

问题

在Go、Java和C#中,你可以声明的数组的最大长度取决于各个语言的规范和实现。这些语言都有一些限制,但并不直接与运行时的最大内存相关。以下是各个语言的一些信息:

  • Go:在Go中,数组的长度是一个非负整数,最大长度取决于你的计算机的可用内存。Go语言没有明确规定数组的最大长度,但是由于内存限制,你可能无法声明非常大的数组。

  • Java:在Java中,数组的最大长度是由int类型的最大值决定的,即2^31-1。这是因为Java使用int类型来表示数组的长度。然而,由于Java的堆内存限制,你可能无法实际声明达到最大长度的数组。

  • C#:在C#中,数组的最大长度也是由int类型的最大值决定的,即2^31-1。与Java类似,C#使用int类型来表示数组的长度。同样地,由于内存限制,你可能无法声明达到最大长度的数组。

需要注意的是,实际可用的内存可能会受到操作系统、硬件和其他因素的限制。因此,即使语言本身没有明确规定数组的最大长度,你仍然可能受到实际可用内存的限制。

英文:

What is the maximum length of an array that I can declare in Go, Java and C#? Does it relate to the maximum memory at runtime? Or do they have a standard?

答案1

得分: 1

《Go编程语言规范》

数组类型

数组是一系列按顺序排列的元素,元素类型相同,称为元素类型。元素的数量称为长度,长度不能为负数。

长度是数组类型的一部分;它必须是一个非负常量,可以用int类型的值表示。

数值类型

数值类型表示整数或浮点数值的集合。

有一组预声明的数值类型,其具体大小取决于实现:

uint     所有无符号整数的集合,32位或64位
int      所有有符号整数的集合,与uint大小相同

Go数组的长度是int类型的值,它是一个32位或64位的有符号整数,取决于编译架构(GOARCH),例如386或amd64。它还受到硬件或操作系统内存大小限制的影响。

package main

import (
	"fmt"
	"runtime"
	"strconv"
)

func main() {
	fmt.Println("int is", strconv.IntSize, "bits on", runtime.GOARCH)
}

输出:

int is 64 bits on amd64
英文:

> The Go Programming Language Specification
>
> Array types
>
> An array is a numbered sequence of elements of a single type, called
> the element type. The number of elements is called the length and is
> never negative.
>
> The length is part of the array's type; it must evaluate to a
> non-negative constant representable by a value of type int.
>
> Numeric types
>
> A numeric type represents sets of integer or floating-point values.
>
> There is a set of predeclared numeric types with
> implementation-specific sizes:
>
> <pre>
> uint the set of all unsigned integers, either 32 or 64 bits
> int the set of all signed integers, same size as uint
> </pre>

Go array length is a value of type int, which is a 32 or 64 bit signed integer, depending on the compilation architecture (GOARCH), for example, 386 or amd64. It's also subject to any hardware or operating system memory size limits.

package main

import (
	&quot;fmt&quot;
	&quot;runtime&quot;
	&quot;strconv&quot;
)

func main() {
	fmt.Println(&quot;int is&quot;, strconv.IntSize, &quot;bits on&quot;, runtime.GOARCH)
}

Output:
<pre>
int is 64 bits on amd64
</pre>

huangapple
  • 本文由 发表于 2014年10月29日 11:40:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/26622422.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定