热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

为什么我们不能只使用[][]而不是int[][]?-Whycannotwejustuse[][]insteadofint[][]?

Case1:int[]data1newint[]{1,2,3};canbesimplifiedas可以简化为int[]data2new[]{

Case 1:

int[] data1 = new int [] { 1, 2, 3 };

can be simplified as

可以简化为

int[] data2 = new [] { 1, 2, 3 };

Case 2:

int[][] table1 = new int[][] { new int[] { },  new int[] { 1, 2 } };

cannot be simplified as

不能简化为

int[][] table2 = new [][] { new int[] { }, new int[] { 1, 2 } };

Question:

For the second case, why cannot we just use [][] instead of int[][]?

对于第二种情况,为什么我们不能只使用[] []而不是int [] []?

1 个解决方案

#1


10  

An int[][] is an array of arrays. If you are explicitly typing it as int[][] then it works (as you can see) but if you are using implicit typing then the code works it out differently.

int [] []是一个数组数组。如果您明确地将其键入为int [] [],那么它可以正常工作(如您所见),但如果您使用隐式类型,则代码将以不同方式运行。

the syntax new [] says "I'm creating an array but I want you to work out the type. Consider the following working example:

语法new []说“我正在创建一个数组,但我想让你计算出类型。考虑下面的工作示例:

new [] { new int [] { }, new [] { 1, 42 } }

Here we say we want a new implicitly typed array. The compiler then looks at our initialiser to work out the type of that array. In this case it sees two items: the first is an explicitly typed empty array of type int[], the second is an implicitly typed array. When the compiler examines the second item's contents it discovers it is an int[] as well so determines that the overall expression defines an int[][].

这里我们说我们想要一个新的隐式类型数组。然后编译器查看我们的初始化程序以计算出该数组的类型。在这种情况下,它会看到两个项目:第一个是int []类型的显式类型空数组,第二个是隐式类型数组。当编译器检查第二项的内容时,它发现它是一个int [],因此确定整个表达式定义了一个int [] []。

In the above example we need to explicitly type the first empty array because otherwise the compiler would have no idea of its type since it has no items to use to determine the type.

在上面的示例中,我们需要显式地键入第一个空数组,否则编译器将不知道它的类型,因为它没有用于确定类型的项。

Thanks to William Andrew Montgomery for providing me the first hint to get to this answer.

感谢William Andrew Montgomery为我提供了第一个提示,以获得这个答案。


推荐阅读
  • This article discusses the efficiency of using char str[] and char *str and whether there is any reason to prefer one over the other. It explains the difference between the two and provides an example to illustrate their usage. ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 本文介绍了在Go语言中可见性与scope的规则,包括在函数内外声明的可见性、命名规范和命名风格,以及变量声明和短变量声明的语法。同时,还介绍了变量的生命周期,包括包级别变量和局部变量的生命周期,以及变量在堆和栈上分配的规则和逃逸分析的概念。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 如何用UE4制作2D游戏文档——计算篇
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了如何用UE4制作2D游戏文档——计算篇相关的知识,希望对你有一定的参考价值。 ... [详细]
  • switch语句的一些用法及注意事项
    本文介绍了使用switch语句时的一些用法和注意事项,包括如何实现"fall through"、default语句的作用、在case语句中定义变量时可能出现的问题以及解决方法。同时也提到了C#严格控制switch分支不允许贯穿的规定。通过本文的介绍,读者可以更好地理解和使用switch语句。 ... [详细]
  • IjustinheritedsomewebpageswhichusesMooTools.IneverusedMooTools.NowIneedtoaddsomef ... [详细]
  • 先看官方文档TheJavaTutorialshavebeenwrittenforJDK8.Examplesandpracticesdescribedinthispagedontta ... [详细]
  • iOS Swift中如何实现自动登录?
    本文介绍了在iOS Swift中如何实现自动登录的方法,包括使用故事板、SWRevealViewController等技术,以及解决用户注销后重新登录自动跳转到主页的问题。 ... [详细]
  • Whatsthedifferencebetweento_aandto_ary?to_a和to_ary有什么区别? ... [详细]
  • 十大经典排序算法动图演示+Python实现
    本文介绍了十大经典排序算法的原理、演示和Python实现。排序算法分为内部排序和外部排序,常见的内部排序算法有插入排序、希尔排序、选择排序、冒泡排序、归并排序、快速排序、堆排序、基数排序等。文章还解释了时间复杂度和稳定性的概念,并提供了相关的名词解释。 ... [详细]
  • Ihaveaworkfolderdirectory.我有一个工作文件夹目录。holderDir.glob(*)>holder[ProjectOne, ... [详细]
  • 本文详细介绍了在Linux虚拟化部署中进行VLAN配置的方法。首先要确认Linux系统内核是否已经支持VLAN功能,然后配置物理网卡、子网卡和虚拟VLAN网卡的关系。接着介绍了在Linux配置VLAN Trunk的步骤,包括将物理网卡添加到VLAN、检查添加的VLAN虚拟网卡信息以及重启网络服务等。最后,通过验证连通性来确认配置是否成功。 ... [详细]
  • 本文介绍了Oracle存储过程的基本语法和写法示例,同时还介绍了已命名的系统异常的产生原因。 ... [详细]
author-avatar
Rianbow_小渊渊设
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有