petrus123
petrus123

Reputation: 9

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. 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

So I change the source level to 1.7 in Eclipse, and then I get the error:

Android requires compiler compliance level 5.0 or 6.0. Found '1.7' instead. Please use Android Tools > Fix Project Properties.

So I do this; the compiler level then goes back to 1.6, and now I get the first error.

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).

Thank you in advance!

Upvotes: 0

Views: 272

Answers (1)

mdewitt
mdewitt

Reputation: 2534

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.

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. 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?

Upvotes: 3

Related Questions