What is the "easiest" way for you to create a singleton (with exactly one element) Objects array in Java ?
在Java中创建单元素(只有一个元素)对象数组的“最简单”方法是什么?
6 个解决方案
#1
29
Object [] singleton = { new SomeObject() };
#2
11
The standard way is this:
标准的方法是:
String[] arr = new String[]{"I am the one and only"};
I'm afraid it doesn't get much simpler than this.
恐怕没有比这更简单的了。
Edit: it does:
编辑:它:
String[] arr = {"I am the one and only"};
Thanks aioobe, I keep forgetting this.
谢谢大家,我总是忘记这一点。
Of course if you often create array you can create a helper method that makes things a bit simpler:
当然,如果你经常创建数组,你可以创建一个帮助方法,使事情更简单:
public static <T> T[] asArray(T... items){
return items;
}
String[] arr = asArray("I am the one and only");
(But you can't enforce at compile time that it will be an array with only one element)
(但是不能在编译时强制要求它是一个只有一个元素的数组)
Next I was going to write a singleton array method, but Stephen beat me to that.
接下来我要写一个单例数组方法,但是Stephen比我强。
#3
5
enum solution(anti reflect attack):
enum解决方案(抗反射攻击):
enum MySingleton{
INSTANCE(new String[]{"a","b"});
final String[] values;
private MySingleton(String[] values) {
this.values = values;
}
}
reference it as:
参考它:
MySingleton.INSTANCE;
#4
4
This should do the job
这应该是可行的
public SomeType[] makeSingletonArray(SomeType elem) {
return new SomeType[]{elem};
}
A generic version of this method would be somewhat awkward to use, since you would need to pass it a Class
object as an additional parameter.
该方法的通用版本使用起来有些笨拙,因为您需要将类对象作为附加参数传递给它。
Inlining the SomeType[]{elem}
expression is simpler, and that's how I'd normally do this.
在SomeType[]{elem}表达式中嵌入更简单,这是我通常这样做的方法。
#5
2
You could do this:
你可以这样做:
String[] a = Collections.
singletonList
("SingleElement").toArray();
String[]= Collections.singletonList(“圈”).toArray();
Edit: Whoops! The above example doesn't compile. As stated in the comment, this can be done either as:
编辑:哎呀!上面的例子没有编译。如评论所述,可以这样做:
Object[] a = Collections.singletonList("SingleElement").toArray();
OrString[] a = Collections.singletonList("SingleElement").toArray(new String[1]);
对象[]= Collections.singletonList(“圈”).toArray();或String[] a = collection . singletonlist(“singletonlist”)toArray(新的字符串[1]);
#6
0
If your project is already using Apache Commons lib, you can stick to ArrayUtils.toArray()
method.
如果您的项目已经在使用Apache Commons lib,您可以坚持使用ArrayUtils.toArray()方法。
String[] arr = ArrayUtils.toArray("The string");
// or if using static import
String[] arr = toArray("The string");
Even if using static import it is still more verbose than the accepted answer:
即使使用静态导入,它仍然比公认的答案更加冗长:
String[] arr = {"The string"};
But it comes very handy when compact array initializer syntax is not allowed.
但是当不允许使用紧凑数组初始化器语法时,它就变得非常方便。
Some examples:
一些例子:
someMethod(toArray("The string"), /* other params */);
return toArray("The string");
@DataProvider
public Object[][] someDataProvider() {
return rangeClosed(-12, +12)
.map(HOURS::toMillis).boxed()
.map(ArrayUtils::toArray)
.toArray(Object[][]::new);
}
You can imagine any other examples yourself.
你可以自己想象其他的例子。
Also note, that the ArrayUtils.toArray()
can wrap an arbitrary number of objects into array, not only a single one.
还要注意,ArrayUtils.toArray()可以将任意数量的对象包装到数组中,而不仅仅是单个对象。
#1
29
Object [] singleton = { new SomeObject() };
#2
11
The standard way is this:
标准的方法是:
String[] arr = new String[]{"I am the one and only"};
I'm afraid it doesn't get much simpler than this.
恐怕没有比这更简单的了。
Edit: it does:
编辑:它:
String[] arr = {"I am the one and only"};
Thanks aioobe, I keep forgetting this.
谢谢大家,我总是忘记这一点。
Of course if you often create array you can create a helper method that makes things a bit simpler:
当然,如果你经常创建数组,你可以创建一个帮助方法,使事情更简单:
public static <T> T[] asArray(T... items){
return items;
}
String[] arr = asArray("I am the one and only");
(But you can't enforce at compile time that it will be an array with only one element)
(但是不能在编译时强制要求它是一个只有一个元素的数组)
Next I was going to write a singleton array method, but Stephen beat me to that.
接下来我要写一个单例数组方法,但是Stephen比我强。
#3
5
enum solution(anti reflect attack):
enum解决方案(抗反射攻击):
enum MySingleton{
INSTANCE(new String[]{"a","b"});
final String[] values;
private MySingleton(String[] values) {
this.values = values;
}
}
reference it as:
参考它:
MySingleton.INSTANCE;
#4
4
This should do the job
这应该是可行的
public SomeType[] makeSingletonArray(SomeType elem) {
return new SomeType[]{elem};
}
A generic version of this method would be somewhat awkward to use, since you would need to pass it a Class
object as an additional parameter.
该方法的通用版本使用起来有些笨拙,因为您需要将类对象作为附加参数传递给它。
Inlining the SomeType[]{elem}
expression is simpler, and that's how I'd normally do this.
在SomeType[]{elem}表达式中嵌入更简单,这是我通常这样做的方法。
#5
2
You could do this:
你可以这样做:
String[] a = Collections.
singletonList
("SingleElement").toArray();
String[]= Collections.singletonList(“圈”).toArray();
Edit: Whoops! The above example doesn't compile. As stated in the comment, this can be done either as:
编辑:哎呀!上面的例子没有编译。如评论所述,可以这样做:
Object[] a = Collections.singletonList("SingleElement").toArray();
OrString[] a = Collections.singletonList("SingleElement").toArray(new String[1]);
对象[]= Collections.singletonList(“圈”).toArray();或String[] a = collection . singletonlist(“singletonlist”)toArray(新的字符串[1]);
#6
0
If your project is already using Apache Commons lib, you can stick to ArrayUtils.toArray()
method.
如果您的项目已经在使用Apache Commons lib,您可以坚持使用ArrayUtils.toArray()方法。
String[] arr = ArrayUtils.toArray("The string");
// or if using static import
String[] arr = toArray("The string");
Even if using static import it is still more verbose than the accepted answer:
即使使用静态导入,它仍然比公认的答案更加冗长:
String[] arr = {"The string"};
But it comes very handy when compact array initializer syntax is not allowed.
但是当不允许使用紧凑数组初始化器语法时,它就变得非常方便。
Some examples:
一些例子:
someMethod(toArray("The string"), /* other params */);
return toArray("The string");
@DataProvider
public Object[][] someDataProvider() {
return rangeClosed(-12, +12)
.map(HOURS::toMillis).boxed()
.map(ArrayUtils::toArray)
.toArray(Object[][]::new);
}
You can imagine any other examples yourself.
你可以自己想象其他的例子。
Also note, that the ArrayUtils.toArray()
can wrap an arbitrary number of objects into array, not only a single one.
还要注意,ArrayUtils.toArray()可以将任意数量的对象包装到数组中,而不仅仅是单个对象。