Error when cleaning and constructing: "uses unchecked or unsafe operations."

2

Next, I have a "Tray" class in my project which at the time of Clean and Build is displaying the following error: "Trap.java uses unchecked or unsafe operations."

I gave one a search and at first it would be a problem to use Generic ArrayList, with no type definition. But the ArrayList of the class are all with type definition: |

Follow the class:

private void setItemCelula(){
    ArrayList<Pedra>[] listPedra = new ArrayList[linhas*colunas];
    ArrayList<Animal>[] listAnimal = new ArrayList[linhas*colunas];
    ArrayList<Arma>[] listArma = new ArrayList[linhas*colunas];
    ArrayList<Pessoa>[] listPessoa = new ArrayList[linhas*colunas];

    for (int i = 0; i < linhas*colunas; i++) {
        listPedra[i] = new ArrayList<>();
        listAnimal[i] = new ArrayList<>();
        listArma[i] = new ArrayList<>();
        listPessoa[i] = new ArrayList<>();       
    }

    for (Pedra pedra : this.itensTabuleiro.getPedras()) {
        Random rand = new Random();
        int pos = rand.nextInt(linhas*colunas);
        listPedra[pos].add(pedra);
    }
    for (Animal animal : this.itensTabuleiro.getAnimais()){
        Random rand = new Random();
        int pos = rand.nextInt(linhas*colunas);
        if(listAnimal[pos].isEmpty())
            listAnimal[pos].add(animal);          
    }
    for (Arma arma : this.itensTabuleiro.getArmas()) {
        Random rand = new Random();
        int pos = rand.nextInt(linhas*colunas);
        listArma[pos].add(arma);
    }
    for (Pessoa pessoa : this.itensTabuleiro.getPessoas()) {
        Random rand = new Random();
        int pos = rand.nextInt(linhas*colunas);
        listPessoa[pos].add(pessoa);
    }
    int i = 0;
    for (Celula[] linha : campos) {
        for (Celula coluna : linha) {
            Itens it = coluna.getItens();
            it.setPedras(listPedra[i]);
            it.setAnimais(listAnimal[i]);
            it.setArmas(listArma[i]);
            it.setPessoas(listPessoa[i]);
            i++;
        }
    }        
}
    
asked by anonymous 02.05.2015 / 01:30

1 answer

2

To start, this is not an error, it's just a warning. The root cause for this warning is due to types erasure , since ArrayList is an raw type .

In your code, in fact, none of these statements are of type definition:

ArrayList<Pedra>[] listPedra = new ArrayList[linhas * colunas];
ArrayList<Animal>[] listAnimal = new ArrayList[linhas * colunas];
ArrayList<Arma>[] listArma = new ArrayList[linhas * colunas];
ArrayList<Pessoa>[] listPessoa = new ArrayList[linhas * colunas];

To avoid this warning you have some alternatives:

  • "delete" the warning, for example by adding this to your method:

    @SuppressWarnings("unchecked")
    private void setItemCelula() {}
    
  • change your solution from a vector ArrayList to list of lists, because in Java can not create array of parameterized types , something like this:

    final List<List<Pedra>> listPedra = new ArrayList<>(linhas * colunas);
    final List<List<Animal>> listAnimal = new ArrayList<>(linhas * colunas);
    final List<List<Arma>> listArma = new ArrayList<>(linhas * colunas);
    final List<List<Pessoa>> listPessoa = new ArrayList<>(linhas * colunas);
    

In this second solution you will need to change the way your code is built, taking into account that you now have no more vectors, but list lists. For example, your first for would look like this:

for (int i = 0; i < linhas * colunas; i++) {
    listPedra.add(i, new ArrayList<>());
    listAnimal.add(i, new ArrayList<>());
    listArma.add(i, new ArrayList<>());
    listPessoa.add(i, new ArrayList<>());
}

And unlike doing this:

listPedra[pos].add(pedra);

You should now use this way:

listPedra.get(pos).add(pedra);

PS: Give preference to referencing an interface, such as List used. You can find out the why of this good practice easily on the internet.

    
02.05.2015 / 03:26