Problem with ArrayList in NetBeans7.2

I'm new to Java, but I like it!

I am using NetBeans7.2, and when I try to create an ArrayList as follows:

ArrayList<String> list = new ArrayList<>(); 

NetBeans says that "the ArrayList type does not accept parameters" (which does not make sense, since my code is simple and correct for Java7).

Also, when I try to import:

 import java.util.ArrayList; 

NetBeans says: "ArrayList is already defined in this compilation unit."

You no longer need to import ArrayList?

Thank you very much! Please forgive my bad English;)

EDIT: Here is my complete code (this is just an exercise)

 import java.util.ArrayList; public class ArrayList { public static void main(String[] args) { ArrayList<String> cores = new ArrayList<>(); cores.add("Branco"); cores.add(0, "Vermelho"); cores.add("Amarelo"); cores.add("Azul"); System.out.println(cores.toString()); System.out.println("Tamanho= " + cores.size()); System.out.println("Elemento2= " + cores.get(2)); System.out.println("Indice Branco= " + cores.indexOf("Branco")); cores.remove("Branco"); System.out.println("Tem Amarelo?" + cores.contains("Amarelo")); } } 
+4
source share
3 answers

If you change the class name to something other than ArrayList, your code will be absolutely correct in Java7, where it is legal to use the diamond operator ( <> ), like you:

 ArrayList<String> list = new ArrayList<>(); 

The basic idea is that code that creates common classes may become less verbose. The Java7 compiler implies what is required automatically.

Java6 will complain and demand that you spell it as John suggested.

+5
source

You need to do:

 ArrayList<String> myArray = new ArrayList<String>(); 

And it should work fine!

+3
source

Change the class name if you named it " ArrayList ".

+1
source

Source: https://habr.com/ru/post/1437305/


All Articles