c++和Java数组声明/定义:差异

时间:2022-06-04 16:59:43

my question is really simple (which doesn't imply that the answer will be as simple.. :D )

我的问题很简单(这并不意味着答案就这么简单)。:D)

why do arrays in C++ include the size as part of the type and Java's do not?

为什么在c++中数组中包含的大小是类型的一部分,而Java不是?

I know that Java array reference variables are just pointers to arrays on the heap,but so are C++ pointers to arrays,but I need to provide a size even then. Let's analyze C++ first:

我知道Java数组引用变量只是堆上数组的指针,但是c++数组的指针也是,但是我仍然需要提供一个大小。我们先分析c++:

// in C++ :

// an array on the stack:
int array[*constexpr*]; 

// a bidimensional array on the stack:                            
int m_array[*constexpr1*][*constexpr2*]; 

// a multidimensional array on the stack:
int mm_array[*constexpr1*][*constexpr2*][*constexpr3*];

// a dynamic "array" on the heap:
int *array = new int[n];

// a dynamic bidimensional "array" on the heap:               
int (*m_array)[*constexpr*] = new int[n][*constexpr*];  

// a dynamic multidimensional "array" on the heap:
int (*mm_array)[*constexpr*][*constexpr*] = new int [n][*constexpr1*][*constexpr2*];

n doesn't have to be a compile time constant expression,all the elements are default initialized. Dynamically allocated "arrays" are not of type array,but the new expression yields a pointer to the first element.

n不需要是编译时常量表达式,所有元素都是默认初始化的。动态分配的“数组”不是数组类型,但是新的表达式会产生指向第一个元素的指针。

So when I create a dynamic array,all dimensions apart the first one,must be constant expressions (otherwise I couldn't declare the pointer to hold their elements). Is it right??

所以当我创建一个动态数组时,除了第一个维度之外的所有维度都必须是常量表达式(否则我不能声明指针来保存它们的元素)。对吧? ?

Now to Java.I can only allocate array on the heap,since this is how Java works:

现在Java。我只能在堆上分配数组,因为Java就是这样工作的:

// a dynamic array on the heap:
 int[] array = new int[n];

// a dynamic bidimensional array on the heap:               
 int[][] m_array = new int[n][];  

// a dynamic multidimensional array on the heap:
 int[][][] mm_array = new int [n][][];

In Java, it doesn't seem to care about array size when defining an array reference variable (it's an error in Java to explicitly provide a size),and so I just need to provide the size for the first dimension when creating the array. This allows me to create jagged array,which I'm not sure I can create in C++ (not arrays of pointers).

在Java中,当定义数组引用变量时,它似乎不关心数组大小(在Java中显式地提供大小是错误的),因此我只需要在创建数组时提供第一个维度的大小。这允许我创建交错数组,我不确定是否可以在c++中创建(不是指针数组)。

can someone explain me how's that? maybe what's happening behind the curtains should make it clear. Thanks.

有人能给我解释一下吗?也许窗帘后面发生的事情应该能说明问题。谢谢。

7 个解决方案

#1


8  

That's because in Java, all arrays are single-dimensional. A two-dimensional array in Java is merely an array of references to one-dimensional arrays. A three-dimensional array in Java is merely a one-dimensional array of references to arrays of references to arrays of whatever base type you wanted.

这是因为在Java中,所有的数组都是单维的。在Java中,二维数组只是对一维数组的引用数组。在Java中,一个三维数组仅仅是一个一维数组,它包含了对任意基类型数组的引用。

Or in C++ speak, an array in Java, if it's not an array of primitive, it's an "array of pointers".

或者用c++来说,Java中的数组,如果它不是一个基元数组,它就是一个“指针数组”。

So, for example, this code:

例如,这个代码

    int[][][] arr3D = new int [5][][];

    System.out.println(Arrays.deepToString(arr3D));

Would yield the output:

将产生的输出:

[null, null, null, null, null]

You can decide to initialize one of its elements:

您可以决定初始化它的一个元素:

    arr3D[2] = new int[3][];

And the output from the same println would now be:

来自同一个println的输出现在是:

[null, null, [null, null, null], null, null]

Still no ints here... Now we can add:

仍然没有整数…现在我们可以添加:

    arr3D[2][2] = new int[7];

And now the result will be:

现在的结果是:

[null, null, [null, null, [0, 0, 0, 0, 0, 0, 0]], null, null]

So, you can see that this is an "array of pointers".

你可以看到这是一个“指针数组”。

In C++, when you allocate a multi-dimensional array the way you described, you are allocating a contiguous array which actually holds all the dimensions of the array and is initialized all the way through to the ints. To be able to know whether it's a 10x10x10 array or a 100x10 array, you have to mention the sizes.

在c++中,当您按照您描述的方式分配多维数组时,您正在分配一个连续数组,该数组实际上包含数组的所有维度,并一直初始化到int。要知道它是一个10x10x10的数组还是一个100x10的数组,您必须提到它的大小。

Further explanation

进一步的解释

In C++, the declaration

在c++中,声明

int (*mm_array)[5][3];

means "mm_array is a pointer to a 5x3 array of integers". When you assign something to it, you expect that thing to be a pointer to a contiguous block of memory, which is at least big enough to contain 15 integers, or maybe an array of several such 5x3 arrays.

表示“mm_array是指向5x3整数数组的指针”。当你给它赋值时,你希望它是一个指向一个连续内存块的指针,它至少大到可以包含15个整数,或者可能是几个这样的5x3数组的数组。

Suppose you didn't mention that "5" and "3".

假设你没有提到“5”和“3”。

int (*mm_array)[][]; // This is not a legal declaration in C++

Now, suppose you are handed a pointer to a newly allocated array, and we have statements like:

现在,假设有一个指针指向一个新分配的数组,我们有如下语句:

mm_array[1][1][1] = 2;

Or

mm_array++;

In order to know where to put the number, it needs to know where index 1 of the array is. Element 0 is easy - it's right at the pointer. But where is element 1? It's supposed to be 15 ints after that. But at compile time, you won't know that, because you didn't give the sizes. The same goes for the ++. If it doesn't know that each element of the array is 15 ints, how will it skip that many bytes?

为了知道把数字放在哪里,它需要知道数组的索引1在哪里。元素0很简单——它在指针上。但是元素1在哪里呢?在那之后应该是15英寸。但是在编译时,你不会知道,因为你没有给出大小。++也是一样。如果它不知道数组的每个元素都是15英寸,它怎么能跳过那么多字节呢?

Furthermore, when is it a 3x5 or a 5x3 array? If it needs to go to element mm_array[0][2][1], does it need to skip two rows of five elements, or two rows of three elements?

此外,它是3x5还是5x3的数组?如果需要访问元素mm_array[0][2][1],它是否需要跳过两行5个元素,或者两行3个元素?

This is why it needs to know, at compile time, the size of its base array. Since the pointer has no information about sizes in it, and merely points to a contiguous block of integer, that information will need to be known in advance.

这就是为什么它需要在编译时知道基数组的大小。由于指针没有关于它的大小的信息,并且仅仅指向一个连续的整数块,因此需要提前知道这些信息。

In Java, the situation is different. The array itself and its sub-arrays, are all Java objects. Each array is one-dimensional. When you have an expression like

在Java中,情况是不同的。数组本身及其子数组都是Java对象。每个数组是一维的。当你有一个像。

arr3D[0][1][2]

arr3D is known to be a reference to an array. That array has length and type information, and one dimension of references. It can check whether 0 is a valid index, and dereference the 0th element, which is itself a reference to an array.

arr3D被认为是对数组的引用。该数组具有长度和类型信息,以及一个维度的引用。它可以检查0是否是一个有效的索引,并取消引用第0个元素(它本身就是对数组的引用)。

Which means that now it has type and length information again, and then a single dimension of references. It can check whether 1 is a valid index in that array. If it is, it can go to that element, and dereference it, and get the innermost array.

这意味着现在它又有了类型和长度信息,然后是一个单一维度的引用。它可以检查1是否为该数组中的有效索引。如果是,它可以转到该元素并取消引用,并获得最内部的数组。

Since the arrays are not a contiguous block, but rather references to objects, you don't need to know sizes at compile time. Everything is allocated dynamically, and only the third level (in this case) has actual contiguous integers in it - only a single dimension, which does not require advance calculation.

由于数组不是连续的块,而是对对象的引用,所以在编译时不需要知道大小。所有内容都是动态分配的,只有第三层(在本例中)具有实际的连续整数——只有一个维度,不需要预先计算。

#2


1  

I guess your real question is, why a stack array must have a fixed size at compile time.

我猜您真正的问题是,为什么堆栈数组在编译时必须具有固定的大小。

Well, for one, that makes it easier to calculate the addresses of following local variables.

首先,这使得计算下列局部变量的地址变得更容易。

Dynamic size for stack array isn't impossible, it's just more complicated, as you would imagine.

栈数组的动态大小不是不可能的,它只是更复杂,正如你想象的那样。

C99 does support variable length arrays on stack. Some C++ compilers also support this feature. See also Array size at run time without dynamic allocation is allowed?

C99支持堆栈上的可变长度数组。一些c++编译器也支持这个特性。在运行时还可以看到不允许动态分配的数组大小吗?

#3


0  

I believe this has to do with what code the compiler issues to address the array. For dynamic arrays you have an array of arrays and cells are addressed by redirecting a redirection.

我认为这与编译器处理数组的代码有关。对于动态数组,您有一个数组数组,单元格可以通过重定向来寻址。

But multidimensional arrays are stored in contiguous memory and the compiler indexes them using a mathematical formula to calculate the cell position based upon each of the array's dimensions.

但是多维数组存储在连续内存中,编译器使用数学公式对它们进行索引,根据数组的每个维度计算单元位置。

Therefore the dimensions need to be known (declared) to the compiler (all except the last one).

因此,需要将维度(声明)给编译器(除了最后一个)。

#4


0  

Correction:

更正:

C sometimes has dimension

C有时维度

Java

Java

 Sometype some[];

declaration is itself an (declaration of) reference to Object and can be changed (to new instance or array). This may be one reason so in java dimension cannot be given "on the left side". Its near to

声明本身是对对象的(声明)引用,可以更改(到新的实例或数组)。这可能是一个原因,因此在java维度中不能给出“在左边”。它的附近

Sometype * some 

in C (forgive me, array in Java is much more intelligent and safe) if we think about pass array to C function, formal situation is similar like in Java. Not only we don't have dimension(s), but cannot have.

在C中(请原谅,Java中的数组更智能、更安全),如果我们考虑将数组传递给C函数,其形式与Java相似。我们不仅没有维度,而且不能有维度。

void func(Sometype arg[])
{
 // in C totally unknown (without library / framework / convention  etc)
 // in Java formally not declared, can be get at runtime
}

#5


0  

In Java, it doesn't seem to care about array size when defining an array reference variable (it's an error in Java to explicitly provide a size),

在Java中,当定义数组引用变量时,它似乎不关心数组大小(在Java中显式地提供大小是错误的),

It is not Java doesn't care about the initial array size when you define an array. The concept of an array in Java is almost totally different from C/C++.

在定义数组时,Java并不关心初始数组的大小。Java中数组的概念几乎与C/ c++完全不同。

First of all the syntax for creating an array in Java is already different. The reason why you are still seeing C/C++ look-alike square brackets in Java when declaring arrays is because when Java was implemented, they tried to follow the syntax of C/C++ as much as possible.

首先,在Java中创建数组的所有语法都是不同的。在声明数组时,在Java中仍然可以看到C/ c++类似的方括号,因为在实现Java时,他们尽可能地遵循C/ c++的语法。

From Java docs:

从Java文档:

Like declarations for variables of other types, an array declaration has two components: the array's type and the array's name. An array's type is written as type[], where type is the data type of the contained elements; the brackets are special symbols indicating that this variable holds an array. The size of the array is not part of its type (which is why the brackets are empty)

与其他类型的变量声明一样,数组声明有两个组件:数组的类型和数组的名称。数组的类型被写入type[],其中type是所包含元素的数据类型;括号是表示该变量持有数组的特殊符号。数组的大小不是其类型的一部分(这就是方括号为空的原因)

When you declare an array in Java, for e.g.:

当你用Java声明一个数组时,例如:

int[] array;

You are merely creating an object which Java called it an array (which acts like an array).

您只是在创建一个对象,Java将其称为数组(其行为类似于数组)。

The brackets [ ] are merely symbol to indicate this is an Array object. How could you insert numbers into a specific symbol which Java uses it to create an Array Object!!

方括号[]只是表示这是一个数组对象的符号。如何将数字插入Java用来创建数组对象的特定符号中!

The brackets looks like what we used in C/C++ array declaration. But Java gives a different meaning to it to the syntax looks like C/C++.

括号类似于我们在C/ c++数组声明中使用的。但是Java给它的语法赋予了不同的含义,看起来像C/ c++。

Another description from Java docs:

另一个来自Java文档的描述:

Brackets are allowed in declarators as a nod to the tradition of C and C++.

作为对C和c++传统的认可,声明符中允许使用括号。


Part of your question:

你的问题的一部分:

This allows me to create jagged array,which I'm not sure I can create in C++ (not arrays of pointers).

这允许我创建交错数组,我不确定是否可以在c++中创建(不是指针数组)。

From Java Docs:

从Java文档:

In the Java programming language, a multidimensional array is an array whose components are themselves arrays. This is unlike arrays in C or Fortran. A consequence of this is that the rows are allowed to vary in length

在Java编程语言中,多维数组是一个数组,其组件本身就是数组。这与C或Fortran中的数组不同。这样做的结果是允许行的长度变化

If you are interested to find out more on Java Arrays, visit:

如果您有兴趣了解更多关于Java数组的信息,请访问:

#6


0  

The difference between in arrays in C++ and Java is that Java arrays are references, like all non-primitive Java objects, while C++ arrays are not, like all C++ objects (yes, you hear a lot that C++ arrays are like pointers, but see below).

c++和Java中数组的不同之处在于,Java数组是引用,就像所有非原始的Java对象一样,而c++数组不像所有c++对象那样(是的,您经常听到c++数组像指针,但请参见下面)。

Declaring an array in C++ allocates memory for the array.

在c++中声明一个数组,为该数组分配内存。

int a[2];
a[0] = 42;
a[1] = 64;

is perfectly legal. However, to allocate memory for the array you must know its size.

是完全合法的。但是,要为数组分配内存,必须知道它的大小。

Declaring an array in Java does not allocate memory for the array, only for the reference, so if you do:

在Java中声明数组并不为数组分配内存,只为引用分配内存,因此,如果您这样做:

int[] a;
a[0] = 42;

you'll get a NullPointerException. You first have to construct the array (and also in Java, to construct the array you need to know its size):

你会得到一个NullPointerException。首先需要构造数组(在Java中也需要构造数组,需要知道数组的大小):

int[] a = new int[2];
a[0] = 42;
a[1] = 64;

So what about C++ array being pointers? Well, they are pointers (because you can do pointer arithmetic with them) but they are constant pointers whose value is not actually stored in the program but known at compile time. For this reason the following C++ code will not compile:

那么c++数组是指针呢?它们是指针(因为你可以用它们来做指针运算),但它们是常量指针,它们的值实际上并不是存储在程序中,而是在编译时知道。因此,以下c++代码将不会编译:

int a[2];
int b[2];
a = b;

#7


0  

You're confusing the meaning of some of your C++ arrays: e.g., your 'm_array' is a pointer to an array of values - see the following compilable C++ example:

您混淆了一些c++数组的含义:例如,您的“m_array”是指向一个值数组的指针——请参见以下可编译的c++示例:

int array_of_values[3] = { 1, 2, 3 };
int (*m_array)[3] = &array_of_values;

the equivalent Java is:

相当于Java是:

int[] array_of_values = {1, 2, 3};
int[] m_array = array_of_values;

similarly, your 'mm_array' is a pointer to an array of arrays:

同样,你的“mm_array”是指向数组的指针:

int array_of_array_of_values[3][2] = { 1, 2, 3, 4, 5, 6 };
int (*mm_array)[3][2] = &array_of_array_of_values;

the equivalent Java is:

相当于Java是:

int[][] array_of_array_of_values = { {1, 2}, {3, 4}, {5, 6} };
int[][] mm_array = array_of_array_of_values;

#1


8  

That's because in Java, all arrays are single-dimensional. A two-dimensional array in Java is merely an array of references to one-dimensional arrays. A three-dimensional array in Java is merely a one-dimensional array of references to arrays of references to arrays of whatever base type you wanted.

这是因为在Java中,所有的数组都是单维的。在Java中,二维数组只是对一维数组的引用数组。在Java中,一个三维数组仅仅是一个一维数组,它包含了对任意基类型数组的引用。

Or in C++ speak, an array in Java, if it's not an array of primitive, it's an "array of pointers".

或者用c++来说,Java中的数组,如果它不是一个基元数组,它就是一个“指针数组”。

So, for example, this code:

例如,这个代码

    int[][][] arr3D = new int [5][][];

    System.out.println(Arrays.deepToString(arr3D));

Would yield the output:

将产生的输出:

[null, null, null, null, null]

You can decide to initialize one of its elements:

您可以决定初始化它的一个元素:

    arr3D[2] = new int[3][];

And the output from the same println would now be:

来自同一个println的输出现在是:

[null, null, [null, null, null], null, null]

Still no ints here... Now we can add:

仍然没有整数…现在我们可以添加:

    arr3D[2][2] = new int[7];

And now the result will be:

现在的结果是:

[null, null, [null, null, [0, 0, 0, 0, 0, 0, 0]], null, null]

So, you can see that this is an "array of pointers".

你可以看到这是一个“指针数组”。

In C++, when you allocate a multi-dimensional array the way you described, you are allocating a contiguous array which actually holds all the dimensions of the array and is initialized all the way through to the ints. To be able to know whether it's a 10x10x10 array or a 100x10 array, you have to mention the sizes.

在c++中,当您按照您描述的方式分配多维数组时,您正在分配一个连续数组,该数组实际上包含数组的所有维度,并一直初始化到int。要知道它是一个10x10x10的数组还是一个100x10的数组,您必须提到它的大小。

Further explanation

进一步的解释

In C++, the declaration

在c++中,声明

int (*mm_array)[5][3];

means "mm_array is a pointer to a 5x3 array of integers". When you assign something to it, you expect that thing to be a pointer to a contiguous block of memory, which is at least big enough to contain 15 integers, or maybe an array of several such 5x3 arrays.

表示“mm_array是指向5x3整数数组的指针”。当你给它赋值时,你希望它是一个指向一个连续内存块的指针,它至少大到可以包含15个整数,或者可能是几个这样的5x3数组的数组。

Suppose you didn't mention that "5" and "3".

假设你没有提到“5”和“3”。

int (*mm_array)[][]; // This is not a legal declaration in C++

Now, suppose you are handed a pointer to a newly allocated array, and we have statements like:

现在,假设有一个指针指向一个新分配的数组,我们有如下语句:

mm_array[1][1][1] = 2;

Or

mm_array++;

In order to know where to put the number, it needs to know where index 1 of the array is. Element 0 is easy - it's right at the pointer. But where is element 1? It's supposed to be 15 ints after that. But at compile time, you won't know that, because you didn't give the sizes. The same goes for the ++. If it doesn't know that each element of the array is 15 ints, how will it skip that many bytes?

为了知道把数字放在哪里,它需要知道数组的索引1在哪里。元素0很简单——它在指针上。但是元素1在哪里呢?在那之后应该是15英寸。但是在编译时,你不会知道,因为你没有给出大小。++也是一样。如果它不知道数组的每个元素都是15英寸,它怎么能跳过那么多字节呢?

Furthermore, when is it a 3x5 or a 5x3 array? If it needs to go to element mm_array[0][2][1], does it need to skip two rows of five elements, or two rows of three elements?

此外,它是3x5还是5x3的数组?如果需要访问元素mm_array[0][2][1],它是否需要跳过两行5个元素,或者两行3个元素?

This is why it needs to know, at compile time, the size of its base array. Since the pointer has no information about sizes in it, and merely points to a contiguous block of integer, that information will need to be known in advance.

这就是为什么它需要在编译时知道基数组的大小。由于指针没有关于它的大小的信息,并且仅仅指向一个连续的整数块,因此需要提前知道这些信息。

In Java, the situation is different. The array itself and its sub-arrays, are all Java objects. Each array is one-dimensional. When you have an expression like

在Java中,情况是不同的。数组本身及其子数组都是Java对象。每个数组是一维的。当你有一个像。

arr3D[0][1][2]

arr3D is known to be a reference to an array. That array has length and type information, and one dimension of references. It can check whether 0 is a valid index, and dereference the 0th element, which is itself a reference to an array.

arr3D被认为是对数组的引用。该数组具有长度和类型信息,以及一个维度的引用。它可以检查0是否是一个有效的索引,并取消引用第0个元素(它本身就是对数组的引用)。

Which means that now it has type and length information again, and then a single dimension of references. It can check whether 1 is a valid index in that array. If it is, it can go to that element, and dereference it, and get the innermost array.

这意味着现在它又有了类型和长度信息,然后是一个单一维度的引用。它可以检查1是否为该数组中的有效索引。如果是,它可以转到该元素并取消引用,并获得最内部的数组。

Since the arrays are not a contiguous block, but rather references to objects, you don't need to know sizes at compile time. Everything is allocated dynamically, and only the third level (in this case) has actual contiguous integers in it - only a single dimension, which does not require advance calculation.

由于数组不是连续的块,而是对对象的引用,所以在编译时不需要知道大小。所有内容都是动态分配的,只有第三层(在本例中)具有实际的连续整数——只有一个维度,不需要预先计算。

#2


1  

I guess your real question is, why a stack array must have a fixed size at compile time.

我猜您真正的问题是,为什么堆栈数组在编译时必须具有固定的大小。

Well, for one, that makes it easier to calculate the addresses of following local variables.

首先,这使得计算下列局部变量的地址变得更容易。

Dynamic size for stack array isn't impossible, it's just more complicated, as you would imagine.

栈数组的动态大小不是不可能的,它只是更复杂,正如你想象的那样。

C99 does support variable length arrays on stack. Some C++ compilers also support this feature. See also Array size at run time without dynamic allocation is allowed?

C99支持堆栈上的可变长度数组。一些c++编译器也支持这个特性。在运行时还可以看到不允许动态分配的数组大小吗?

#3


0  

I believe this has to do with what code the compiler issues to address the array. For dynamic arrays you have an array of arrays and cells are addressed by redirecting a redirection.

我认为这与编译器处理数组的代码有关。对于动态数组,您有一个数组数组,单元格可以通过重定向来寻址。

But multidimensional arrays are stored in contiguous memory and the compiler indexes them using a mathematical formula to calculate the cell position based upon each of the array's dimensions.

但是多维数组存储在连续内存中,编译器使用数学公式对它们进行索引,根据数组的每个维度计算单元位置。

Therefore the dimensions need to be known (declared) to the compiler (all except the last one).

因此,需要将维度(声明)给编译器(除了最后一个)。

#4


0  

Correction:

更正:

C sometimes has dimension

C有时维度

Java

Java

 Sometype some[];

declaration is itself an (declaration of) reference to Object and can be changed (to new instance or array). This may be one reason so in java dimension cannot be given "on the left side". Its near to

声明本身是对对象的(声明)引用,可以更改(到新的实例或数组)。这可能是一个原因,因此在java维度中不能给出“在左边”。它的附近

Sometype * some 

in C (forgive me, array in Java is much more intelligent and safe) if we think about pass array to C function, formal situation is similar like in Java. Not only we don't have dimension(s), but cannot have.

在C中(请原谅,Java中的数组更智能、更安全),如果我们考虑将数组传递给C函数,其形式与Java相似。我们不仅没有维度,而且不能有维度。

void func(Sometype arg[])
{
 // in C totally unknown (without library / framework / convention  etc)
 // in Java formally not declared, can be get at runtime
}

#5


0  

In Java, it doesn't seem to care about array size when defining an array reference variable (it's an error in Java to explicitly provide a size),

在Java中,当定义数组引用变量时,它似乎不关心数组大小(在Java中显式地提供大小是错误的),

It is not Java doesn't care about the initial array size when you define an array. The concept of an array in Java is almost totally different from C/C++.

在定义数组时,Java并不关心初始数组的大小。Java中数组的概念几乎与C/ c++完全不同。

First of all the syntax for creating an array in Java is already different. The reason why you are still seeing C/C++ look-alike square brackets in Java when declaring arrays is because when Java was implemented, they tried to follow the syntax of C/C++ as much as possible.

首先,在Java中创建数组的所有语法都是不同的。在声明数组时,在Java中仍然可以看到C/ c++类似的方括号,因为在实现Java时,他们尽可能地遵循C/ c++的语法。

From Java docs:

从Java文档:

Like declarations for variables of other types, an array declaration has two components: the array's type and the array's name. An array's type is written as type[], where type is the data type of the contained elements; the brackets are special symbols indicating that this variable holds an array. The size of the array is not part of its type (which is why the brackets are empty)

与其他类型的变量声明一样,数组声明有两个组件:数组的类型和数组的名称。数组的类型被写入type[],其中type是所包含元素的数据类型;括号是表示该变量持有数组的特殊符号。数组的大小不是其类型的一部分(这就是方括号为空的原因)

When you declare an array in Java, for e.g.:

当你用Java声明一个数组时,例如:

int[] array;

You are merely creating an object which Java called it an array (which acts like an array).

您只是在创建一个对象,Java将其称为数组(其行为类似于数组)。

The brackets [ ] are merely symbol to indicate this is an Array object. How could you insert numbers into a specific symbol which Java uses it to create an Array Object!!

方括号[]只是表示这是一个数组对象的符号。如何将数字插入Java用来创建数组对象的特定符号中!

The brackets looks like what we used in C/C++ array declaration. But Java gives a different meaning to it to the syntax looks like C/C++.

括号类似于我们在C/ c++数组声明中使用的。但是Java给它的语法赋予了不同的含义,看起来像C/ c++。

Another description from Java docs:

另一个来自Java文档的描述:

Brackets are allowed in declarators as a nod to the tradition of C and C++.

作为对C和c++传统的认可,声明符中允许使用括号。


Part of your question:

你的问题的一部分:

This allows me to create jagged array,which I'm not sure I can create in C++ (not arrays of pointers).

这允许我创建交错数组,我不确定是否可以在c++中创建(不是指针数组)。

From Java Docs:

从Java文档:

In the Java programming language, a multidimensional array is an array whose components are themselves arrays. This is unlike arrays in C or Fortran. A consequence of this is that the rows are allowed to vary in length

在Java编程语言中,多维数组是一个数组,其组件本身就是数组。这与C或Fortran中的数组不同。这样做的结果是允许行的长度变化

If you are interested to find out more on Java Arrays, visit:

如果您有兴趣了解更多关于Java数组的信息,请访问:

#6


0  

The difference between in arrays in C++ and Java is that Java arrays are references, like all non-primitive Java objects, while C++ arrays are not, like all C++ objects (yes, you hear a lot that C++ arrays are like pointers, but see below).

c++和Java中数组的不同之处在于,Java数组是引用,就像所有非原始的Java对象一样,而c++数组不像所有c++对象那样(是的,您经常听到c++数组像指针,但请参见下面)。

Declaring an array in C++ allocates memory for the array.

在c++中声明一个数组,为该数组分配内存。

int a[2];
a[0] = 42;
a[1] = 64;

is perfectly legal. However, to allocate memory for the array you must know its size.

是完全合法的。但是,要为数组分配内存,必须知道它的大小。

Declaring an array in Java does not allocate memory for the array, only for the reference, so if you do:

在Java中声明数组并不为数组分配内存,只为引用分配内存,因此,如果您这样做:

int[] a;
a[0] = 42;

you'll get a NullPointerException. You first have to construct the array (and also in Java, to construct the array you need to know its size):

你会得到一个NullPointerException。首先需要构造数组(在Java中也需要构造数组,需要知道数组的大小):

int[] a = new int[2];
a[0] = 42;
a[1] = 64;

So what about C++ array being pointers? Well, they are pointers (because you can do pointer arithmetic with them) but they are constant pointers whose value is not actually stored in the program but known at compile time. For this reason the following C++ code will not compile:

那么c++数组是指针呢?它们是指针(因为你可以用它们来做指针运算),但它们是常量指针,它们的值实际上并不是存储在程序中,而是在编译时知道。因此,以下c++代码将不会编译:

int a[2];
int b[2];
a = b;

#7


0  

You're confusing the meaning of some of your C++ arrays: e.g., your 'm_array' is a pointer to an array of values - see the following compilable C++ example:

您混淆了一些c++数组的含义:例如,您的“m_array”是指向一个值数组的指针——请参见以下可编译的c++示例:

int array_of_values[3] = { 1, 2, 3 };
int (*m_array)[3] = &array_of_values;

the equivalent Java is:

相当于Java是:

int[] array_of_values = {1, 2, 3};
int[] m_array = array_of_values;

similarly, your 'mm_array' is a pointer to an array of arrays:

同样,你的“mm_array”是指向数组的指针:

int array_of_array_of_values[3][2] = { 1, 2, 3, 4, 5, 6 };
int (*mm_array)[3][2] = &array_of_array_of_values;

the equivalent Java is:

相当于Java是:

int[][] array_of_array_of_values = { {1, 2}, {3, 4}, {5, 6} };
int[][] mm_array = array_of_array_of_values;