在数组声明中 int[] k,i 和 int k[],i; [英] in array declaration int[] k,i and int k[],i;

查看:35
本文介绍了在数组声明中 int[] k,i 和 int k[],i;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在声明数组时,我们可以在标识符的任何一侧使用括号,但在这种情况下:

While declaring array we can use brackets any side of the identifier but in the case:

int[] k,i;

int k[],i;

它将以两种不同的方式进行考虑.那是第一个创建两个数组 k 和 i.第二个创建了一个数组 k 和一个普通变量 i.这是什么行为?

It will be considered in two different ways. That is first one creates two arrays k and i. that second one creates an array k and a normal variable i. What is this behavior?

在java中通常我们更喜欢第一种类型的声明.但在这种情况下,我们无法在单个语句中创建数组和原始变量.

in java usually we prefer first type of declaration. but in this case we cant create an array and a primitive variable in the single statement.

推荐答案

我的猜测如下:

声明int[] k更合乎逻辑,因为它声明k是一个int数组>.因此,它是 Java 中首选的 (?) 样式.

The declaration int[] k is more logical, because it declares k to be an array of int. Therefore, it is the preferred (?) style in Java.

int k[] 另一方面,是声明这个数组的 C 方式(K&R 在声明语法方面有不同的哲学——他们希望声明模仿 访问变量)并简化 C 程序员的转换,也允许使用这种语法 - 无害.

int k[], on the other hand, was the C way of declaring this array (K&R had a different philosophy when it came to declaration syntax – they wanted declarations to mimic access to the variable) and to ease the transition for C programmers, this syntax was also allowed – no harm done.

现在,在上面的语句中,您链接了两个声明.在第一种情况下,两个变量都声明为相同的类型——显然是 int[].然而,在第二个代码中,这种行为是违反直觉的(并且也不同于 C 的行为),因此具有其他语义.

Now, in your above statement you have chained two declarations. In the first case, both variables are declared with the same type – which is clearly int[]. However, in the second code this behaviour would be counter-intuitive (and also different from C’s behaviour), and therefore has other semantics.

请记住,这纯粹是猜测.

Keep in mind that this is purely a guess.

这篇关于在数组声明中 int[] k,i 和 int k[],i;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆