字符串[] []的说明 [英] String [][] clarification
问题描述
我只是在问这条指令:
String[][] s = new String[2][2];
如果创建此变量 s,我将创建一个包含2行2列的字符串表?
还是2个包含2个元素的表?
感谢您的澄清。
If i create this variable "s", will i create a string table with 2 lines and 2 columns ? Or 2 tables with 2 elements? Thank you for this clarification.
推荐答案
Java没有内置的 table 数据类型,唯一的本机基本基本类型(元类型?)是
Java has no build-in table datatype, the only native basic types of types (metatypes?) are
- 原始值(数字,字符和布尔值)
- 数组(相同类型的变量的线性可索引集合)。
- 类的对象(基本上是指定类型的命名变量的集合,以及一些方法,由类)。 (字符串就是这样的例子。)
其他所有内容都必须由这些类型组成。 (为此,数组类型都被视为 java.lang.Object
的子类型,这是所有其他类都继承的类。)
Everything else must be composed of those types. (To complicate this, the array types are all considered subtypes of java.lang.Object
, which is the class from which all other classes inherit.)
因此,您可以拥有特定类的数组,具有数组类型作为类变量(字段)的类,以及数组的数组,如下所示。
So you can have arrays of a specific class, classes which have array-types as class variables (fields), and also arrays of arrays, as here.
您的 String [] []
类型由String数组和 new String [2] [2]组成
数组创建表达式创建一个长度为2的数组,每个元素本身就是一个长度为2的数组。这些数组的每个元素都可以是字符串,但在创建时首先是 null
。
Your String[][]
type consists of arrays of arrays of String, and the new String[2][2]
array creation expression creates an array of length two, each element being itself an array of length two. Each element of these arrays can be a string, but on creation it is first null
.
s --> [ 0 , 1 ]
| '----> [ null, null ]
|
'---------> [ null, null ]
您现在可以将这些 null放置在
引用实际的 String
对象(如果需要),方法是使用 s [0] [1] = Hello ;
和类似的语句。
You now can put in the places of these null
s references to actual String
objects, if you want, by using s[0][1] = "Hello";
and similar statements.
正如其他答复者已经说过的那样,这样的2D数组可以看作是具有行和列的表,如果需要的话,但是从语言的角度来看并没什么特别的。
As said already by the other answerers, such a 2D-array can be viewed as a table with rows and columns, if you want, but it is nothing special from the language viewpoint.
这篇关于字符串[] []的说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!