参数名称"int g []"后面的Java方括号? [英] Java square brackets behind parameter name "int g[]"?

查看:48
本文介绍了参数名称"int g []"后面的Java方括号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将代码从Java转换为OpenCL

I have to translate code from Java to OpenCL

我有用Java提供的此功能:

I have this function given in Java:

float dot(int g[], double x, double y, double z) {
    return g[0]*x + g[1]*y + g[2]*z;
}

这是可能的呼叫:

dot(g[i], x, y, z);

其中: i = int g =通常的int数组.

这奇怪的 int g [] 参数是什么?我以前从未见过,也没有找到任何有关参数名称后方括号"的信息.

What is this weird int g[] parameter thing? I've never seen this before and didn't find anything about "square brackets after parameter name".

我唯一能想象的是,这是某种偏移量,例如将 g [0] * x 转换为 g [i + 0] * x ?

The only thing I can imagine, that this is some sort of offset thing, like its translates g[0]*x to g[i+0]*x?

推荐答案

这奇怪的int g []参数是什么?

What is this weird int g[] parameter thing?

这是编写 int [] g 的另一种方法,其含义也完全相同:参数键入 int [] ,因此需要引用 int 的数组.来自 JLS§10.2:

It's just another way of writing int[] g and means exactly the same thing: The argument is typed int[], so needs to be a reference to an array of int. From JLS §10.2:

[] 可以作为类型的一部分出现在声明的开头,或者作为特定变量的声明符的一部分出现,或者同时出现在这两者之间.

The [] may appear as part of the type at the beginning of the declaration, or as part of the declarator for a particular variable, or both.

关于您的通话:

这是可能的呼叫:

This is a possible call:

dot(g[i], x, y, z);

其中: i = int ,而 g = int 的常规数组.

Where: i = int, and g = usual array of int.

不,正如您所发现的,那是不可能的电话.:-)您需要传递一个数组引用,而不是一个 int ,所以也许是这样:

No, that isn't a possible call, as you discovered. :-) You need to pass in an array reference, not an int, so perhaps:

dot(g, x, y, z);

这篇关于参数名称"int g []"后面的Java方括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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