简体   繁体   English

为什么方法'Arrays.asList()'不接受类型为{“”,“”}的参数,而是接受(“”,“”)?

[英]Why does method 'Arrays.asList()' not accept arguments of type {“”,“”}, but accepts (“”,“”)?

I am not new to Java Collections , but I have a confusion about the following scenario. 我对Java Collections并不Collections ,但是对以下情况感到困惑。

In my project I've implemented a code like this: 在我的项目中,我实现了如下代码:

List<String> obj_lStr= new ArrayList<String>();
String[] obj_arrStr = {"someString", "noString", "abcString"};
obj_lStr.addAll(Arrays.asList(obj_arrStr));

but during code review my project lead gave me the instruction to change this code and implement it without using String[] obj_arrStr . 但是在代码审查期间,我的项目负责人向我提供了更改代码和不使用String[] obj_arrStr来实现它的指令。

Then I changed my code to this: 然后我将代码更改为此:

obj_lStr.addAll(Arrays.asList( { "someString", "noString", "abcString" }));

but I got compilation errors: 但是我得到了编译错误:

Main.java:13: error: illegal start of expression
    x.addAll(Arrays.asList({"someString", "noString", "abcString"}));
                           ^
Main.java:13: error: ')' expected
    x.addAll(Arrays.asList({"someString", "noString", "abcString"}));
                            ^
Main.java:13: error: ';' expected
    x.addAll(Arrays.asList({"someString", "noString", "abcString"}));

and I change my line of code to this: 我将代码行更改为此:

obj_lStr.addAll(Arrays.asList("someString", "noString", "abcString"));

then the compilation error is gone. 那么编译错误就消失了。

Question: Why is it so? 问题:为什么会这样? Why asList() method raises a compilation error with {"","",""} , but not for ("","","") ? 为什么asList()方法使用{"","",""}引发编译错误,而不是("","","")

Arrays.asList takes either an array, or all elements of the array as argument. Arrays.asList采用一个数组或该数组的所有元素作为参数。

So you need either 所以你需要

Arrays.asList(new String[]{"someString", "noString", "abcString"})

or 要么

Arrays.asList("someString", "noString", "abcString")

the signature of asList() has the answer : public static <T> List<T> asList(T... a) asList()的签名具有答案: public static <T> List<T> asList(T... a)

it takes a varargs , so obj_lStr.addAll(Arrays.asList("someString", "noString", "abcString")); 它需要一个varargs,因此obj_lStr.addAll(Arrays.asList("someString", "noString", "abcString")); compiles bcoz your arguments will be turned into an array. 编译bcoz,您的参数将被转换为数组。

but here obj_lStr.addAll(Arrays.asList( { "someString", "noString", "abcString" })); 但是这里obj_lStr.addAll(Arrays.asList( { "someString", "noString", "abcString" })); , you need to specify the type of the array. ,您需要指定数组的类型。 so you can do this : obj_lStr.addAll(Arrays.asList(new String[] { "someString", "noString", "abcString" })); 因此,您可以执行以下操作: obj_lStr.addAll(Arrays.asList(new String[] { "someString", "noString", "abcString" })); .

the {} initialization syntax for arrays is available only during initilization , for ex: 数组的{}初始化语法仅在初始化期间可用,例如:

int[] a ={1,2,4}; // works

but

int[] a new int[3];
a = {1,2,4}; // doesn't work

It's just to do with where in Java code you're allowed to omit the type and allow the array to be inferred -- specifically, you can only use array = {foo, bar...} when you're assigning directly into an array of the appropriate type, not where you're passing it around elsewhere. 这与允许在Java代码中的什么地方省略类型并允许推断数组有关-特别是,当直接分配给一个array = {foo, bar...} ,只能使用array = {foo, bar...}适当类型的数组,而不是将其传递到其他地方的位置。

In any event, what you should be writing is 无论如何,你应该写的是

Arrays.asList("someString", "noString", "abcString")

with no {} braces at all. 根本没有{}大括号。

you can either feed an String Array to Arrays.asList : 您可以将字符串数组提供给Arrays.asList

Arrays.asList(new String[] {"a","b","c"});

or with multiple Strings (varags) 或具有多个字符串(变量)

Arrays.asList("a","b","c");

But just 只是

Arrays.asList({"a","b","c"});

isnt working, as {"a","b","c"} does not represent an object an Java 不起作用,因为{"a","b","c"}并不代表Java对象

If you try looking at the javadocs , you'll learn that asList has the signature 如果您尝试查看javadocs ,则会发现asList具有签名

public static <T> List<T> asList(T... a)

Now, the argument declaration T... a , uses a feature of java called variable arguments , which basically just means that the argument a , can either be an array of type T, or, it can be a sequence of objects all of type T. 现在,参数声明T... a使用了Java的称为可变参数的功能 ,这基本上意味着参数a可以是类型T的数组,也可以是所有类型的对象序列T.

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM