-2

I have imported some project into my workspace. it has some hashmaps, arraylist etc. it has syntax error in this <> part of code.

for example definition of array list is ArrayList<String> bases = null; but when a make a new instance like bases = Arraylist<>(); it gets a syntax error.

I have jre1.7 and JDK 7 installed on my system

Tim S.
  • 55,448
  • 7
  • 96
  • 122
E mad
  • 293
  • 3
  • 6
  • 18
  • If you are using generics, shouldn't you specify the type argument... `= Arraylist();` – Fenton Nov 21 '13 at 21:00
  • Do you have the word "new" after the "=" sign or not? It's in your question title but not your question body. – Dawood ibn Kareem Nov 21 '13 at 21:05
  • @DavidWallace it has new after = i just forgot to write that in question. i guess its something with my eclipse preferences ! because this project works with another systems or another eclipse! – E mad Nov 22 '13 at 05:47
  • @DavidWallace this is also one of the exceptions : Exception in thread "main" java.lang.Error: Unresolved compilation problems: Type mismatch: cannot convert from DefaultComboBoxModel> to DefaultComboBoxModel Cannot instantiate the type DefaultComboBoxModel> Syntax error on token "<", ? expected after this token – E mad Nov 22 '13 at 05:54

5 Answers5

2

bases = new ArrayList<>();

Anirban Nag 'tintinmj'
  • 5,572
  • 6
  • 39
  • 59
0

Looks like a declaration for a generic type, but it's missing the letter. Try filling in the brackets with a letter and declaring the letter as a generic type in the class header.

0

This (<>) is called diamond operator (type inference), it is added to Java in 1.7. Having Java 7 installed on your system is not enough, your project should also be using Java, so make it use Java 7 from your ide's project settings.

Utku Özdemir
  • 7,390
  • 2
  • 52
  • 49
  • you mean in java build path? i've change it to java 7 from that project properties but the problem is still on ! – E mad Nov 22 '13 at 05:49
0

diamond is only support by jdk7 (and later version). You have jdk7 installed on your system. You should make sure your IDE (eclipse?) uses the jdk/jre7 for your project.

also it maybe a typo in your question. you need the new keyword and ArrayList (big L).

xxx = new ArrayList<>();
Kent
  • 189,393
  • 32
  • 233
  • 301
0

If you are going to use raw types, then declare and assign like following:

ArrayList bases = null;
bases = new ArrayList();

If you are using generics and JDK7, then declare the TYPE of the object to be stored, and use the diamond operator:

ArrayList<TYPE> bases = null;
bases = new ArrayList<>();

More info: What is a raw type and why shouldn't we use it?

Community
  • 1
  • 1
yamilmedina
  • 3,315
  • 2
  • 20
  • 28