Practical example List and ArrayList

0

I'm creating a list of Students, but I find myself in the following situation. At point 2 give me error because it says that I have to get the Student inside the List but when I do point 1 is already correct. Can anyone tell me a practical example because?

The goal is supposedly to have main as point 2. And inside the ArrayList has nothing. What do they advise me? To do as point 1?

public static void main(String[] args) {
    (1) ArrayList<Estudante> lista = new ArrayList<Estudante>(); //assim dá mas com List e arraylist nao da porque?
    (2) List <Estudante> lista1 = new ArrayList<>();

}

Even if you do this:

    List<Estudante> lista1 = new ArrayList<Estudante>();

The List is giving error: The type List is not generic; it can not be parameterized with arguments Student

My student class has only the number and name:

public class Estudante {
private int numero;
private String nome;

public Estudante(int numero, String nome){
    this.numero = numero;
    this.nome = nome;
}
    
asked by anonymous 19.10.2016 / 16:32

2 answers

4

Just put the object that will be used in the construction of the object. You can only omit if the variable type declaration is equal to the object's construct:

import java.util.*;

class Ideone {
    public static void main(String[] args) {
        ArrayList<Estudante> lista = new ArrayList<Estudante>();
        List<Estudante> lista1 = new ArrayList<Estudante>();
        ArrayList<Estudante> lista2 = new ArrayList<>();
    }
}

class Estudante {}

See working on ideone and on CodingGround .

Do not forget to import .

    
19.10.2016 / 16:42
4

Probably the problem is import incorrect. You must be importing the List class from the GUI package. Just swap:

import java.awt.List;

By:

import java.util.List;

Your code will function normally.

IDEONE

    
19.10.2016 / 18:58