Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble with ArrayList in NetBeans7.2

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

I'm using NetBeans7.2, and when I try to create an ArrayList like this:

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

NetBeans says "type ArrayList does not take parameters" (which doesn't make sense, as my code is simple and seams to be correct for Java7).

Also, when I try to import:

import java.util.ArrayList;

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

It's not necessary to import ArrayList anymore?

Thank you very much! Please, forgive my bad english ;)

EDIT: Here is my full code (it's just an exercice)

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"));

   }
}
like image 655
Borjovsky Avatar asked Jan 16 '23 10:01

Borjovsky


1 Answers

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

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

The basic idea behind this is so that code which instantiates generic classes can become less verbose. The Java7 compiler implies what is needed automatically.

Java6 will complain and require that you write it the way Jon suggested.

like image 95
Costis Aivalis Avatar answered Jan 25 '23 22:01

Costis Aivalis