简体   繁体   English

Java-需要使用菱形运算符; android编译器合规性级别与支持菱形运算符的级别之间的冲突

[英]Java - need to use diamond operator; conflict between android compiler compliance level and level supporting diamond operator

I am very new to java but have been reading up a bit and was trying to make a fairly simple android app in Eclipse. 我对Java很陌生,但是已经读了一点,并试图在Eclipse中制作一个相当简单的android应用。 I have the following line of code: 我有以下代码行:

    ArrayList<String> userNumbers = new ArrayList<>(Arrays.asList(userNumbersArray));

Of course, I get the error: 当然,我得到了错误:

'<>' is not allowed for source level below 1.7 低于1.7的源级别不允许使用“ <>”

So I change the source level to 1.7 in Eclipse, and then I get the error: 所以我在Eclipse中将源级别更改为1.7,然后出现错误:

Android requires compiler compliance level 5.0 or 6.0. Android要求编译器符合级别5.0或6.0。 Found '1.7' instead. 发现“ 1.7”。 Please use Android Tools > Fix Project Properties. 请使用Android工具>修复项目属性。

So I do this; 所以我这样做 the compiler level then goes back to 1.6, and now I get the first error. 然后编译器级别回到1.6,现在我得到了第一个错误。

My question is if there is either a way for it to be compatible with android and with the diamond operator or if there is another way to write that line of code (still using an arraylist; it must be specified that it is a String). 我的问题是,是否有办法使其与android和diamond运算符兼容,或者是否有另一种方式来编写该行代码(仍然使用arraylist;必须将其指定为String)。

Thank you in advance! 先感谢您!

Just change your line of code to 只需将您的代码行更改为

ArrayList<String> userNumbers = 
   new ArrayList<String>(Arrays.asList(userNumbersArray));

That notation will be supported above and below version 1.7. 1.7版和更高版本将支持该表示法。

When you don't add the String in the diamond in the right hand side of the equation you are using the diamond operator. 当您不在等式右侧的菱形中添加字符串时,您将使用菱形运算符。 The diamond operator was added to reduce the verbosity of the Java code, but you don't actually have to use it. 添加了菱形运算符可减少Java代码的冗长性,但实际上您不必使用它。 You can still specify the type in the diamond on the right hand side of the equation if you wish. 如果需要,您仍然可以在等式右侧的菱形中指定类型。

Note: If you are still learning Java and/or learning about the diamond operator See the answer in this quesiton: What is the point of the diamond operator in Java 7? 注意:如果您仍在学习Java和/或关于Diamond运算符,请参阅此问题的答案: Java 7中 Diamond 运算符的意义是什么?

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

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