Java 中的 new Object[]{} 是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20054481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does new Object[]{} in Java means?
提问by eversor
In Java you are able to do the following:
在 Java 中,您可以执行以下操作:
new Object[] { /* parameters separated by comma */};
Indeed, this is used in the prepared statements of the Spring framework. Eg:
事实上,这在 Spring 框架的准备好的语句中使用。例如:
getJdbcTemplate().queryForList(
"DELETE FROM foo WHERE id = ?", //the "?" mark will be substituted by "3"
new Object[] { 3 }, //What kind of magic is this?
String.class //Irrelevant in this example
);
- How is this called?
- What is going on there?
- How could you access that parameters?
- 这个怎么称呼?
- 那里发生了什么?
- 你怎么能访问这些参数?
采纳答案by Deniz
Object[] objs = new Object[]{3,4};
is the same as:
是相同的:
Object[] objs = new Object[2];
objs[0] = 3;
objs[1] = 4;
So you access it as objs[0];
所以你访问它作为 objs[0];
回答by Nishant
new Object[] { /* parameters separated by comma */};
new Object[] { /* parameters separated by comma */};
Creates anonymous array of Object
创建匿名数组 Object
The parameters separated by comma are elements of the array.
逗号分隔的参数是数组的元素。
Number of elements is the length
of array
元素数是length
数组的
回答by Benoit Wickramarachi
It's used to initialize an array of Object
with a value of 3
at index 0
.
它用于初始化Object
一个值为3
at index的数组0
。
回答by Felipe Volpatto
Creates anonymous array of Object.
创建对象的匿名数组。
new Object[] { /* parameters separated by comma */};
initialize an array with a value 3 at index 0
在索引 0 处初始化一个值为 3 的数组
new Object[] { 3 }
The last example is the same as:
最后一个示例与以下示例相同:
Object[] obj = new Object[1];
objs[0] = 3;
回答by ksv
The code new Object[] { /* parameters separated by comma */};
for example
new String[] {"a","b", "c"}
creates 3 objects of type String.
代码 new Object[] { /* 参数以逗号分隔 */}; 例如
new String[] {"a","b", "c"}
创建 3 个字符串类型的对象。
回答by Stephen C
You asked about this:
你问过这个:
new Object[] { 3 }
As the other answers have said, it is creating and initializing an array of objects that is going to be passed as a parameter to the queryForList
method.
正如其他答案所说,它正在创建和初始化将作为参数传递给queryForList
方法的对象数组。
The array's actual type will be Object[]
, its length will be 1
, and its first element will be an Integer
object ... produced by autoboxing the int
value 3
.
数组的实际类型将为Object[]
,其长度为1
,其第一个元素将是一个Integer
对象...通过自动装箱int
值生成3
。
The rest of your questions don't make much sense to me:
你的其余问题对我来说没有多大意义:
- How is this called?
- 这个怎么称呼?
It is called "creating and initializing an array"
它被称为“创建和初始化数组”
- What is going on there?
- 那里发生了什么?
It is creating and initializing an array
它正在创建和初始化一个数组
- How could you access that parameters?
- 你怎么能访问这些参数?
Umm ... as shown in the example? By value? By indexing the array? It is not clear what you are asking.
嗯...如示例所示?按价值?通过索引数组?不清楚你在问什么。
回答by EProgrammerNotFound
This code can possibly be translate as the following
这段代码可能翻译如下
private void queryforlist(string SQL, object[] parameters, Class c)
{
//parameters is an array of objects used to replace the ? caracteres in the sql
//text.
if (parameters != null)
{
//we can access parameters by index inside the method
for(int i=0; parameters.length; i++)
{
if (parameters[i] instanceof String )
{
//adding quotes to string for example
String param = "\""+parameters[i]+"\"";
} else if (parameters[i] instanceof Integer )
{
//Note is Integer not int because of AutoBoxing:
}
}
}
}
This is useful to accept dynamic datatypes, like Integers, Strings, etc. And deal it them individually e.g: Adding to_date() in a date in case of Oracle database/provider.
这对于接受动态数据类型(如整数、字符串等)很有用。并单独处理它们,例如:在 Oracle 数据库/提供程序的情况下在日期中添加 to_date()。
It's defined as an array of objects to accept multiple parameters, so, the query can be build the way you want.
它被定义为一个接受多个参数的对象数组,因此,可以按照您想要的方式构建查询。
This specific line:
此特定行:
new Object[] { 3 }
can be translated as:
可以翻译为:
Object[] AnonymousArrayObject = new Object[1];
AnonymousArrayObject [0] = 3;
Since queryForList receives an array of object as parameter, there several ways of calling this method:
由于 queryForList 接收一个对象数组作为参数,因此有几种调用此方法的方法:
Example 1
示例 1
getJdbcTemplate().queryForList(
"DELETE FROM foo WHERE id = ?",
new Object[] { 3 },
String.class
);
Example 2
示例 2
Object[] Parameters = new Object[1];
parameters[0] = 3;
getJdbcTemplate().queryForList(
"DELETE FROM foo WHERE id = ?",
Parameters,
String.class
);
Example 3
示例 3
Object[] Parameters = new Object[] { 3 };
getJdbcTemplate().queryForList(
"DELETE FROM foo WHERE id = ?",
Parameters,
String.class
);
Example 1,2 and 3 are doing the same thing
示例 1,2 和 3 正在做同样的事情
Example 4
示例 4
getJdbcTemplate().queryForList(
"DELETE FROM foo WHERE id = ?",
null,
String.class
);
Example 5
例 5
Object[] Parameters = null;
getJdbcTemplate().queryForList(
"DELETE FROM foo WHERE id = ?",
Parameters,
String.class
);
Example 4 and 5 are doing the same thing
示例 4 和示例 5 正在做同样的事情
How is this called?
这个怎么称呼?
This is called Short-hand for creation and initialization of an array. Since you don't need to declare a variable to access the array of object in that specific moment.
这称为创建和初始化数组的简写。由于您不需要声明一个变量来在那个特定时刻访问对象数组。
What is going on there?
那里发生了什么?
Explained in the answer!
答案中有说明!
How could you access that parameters?
你怎么能访问这些参数?
Explained in the answer!
答案中有说明!
回答by Kashif Nazar
new Object[]
means that it's an array of Object
type. new Object[]{3}
is a short-hand to assign this array with an Integer 3.
new Object[]
意味着它是一个Object
类型的数组。new Object[]{3}
是为该数组分配整数 3 的简写。