Error when cleaning and building: "uses unchecked or unsafe Operations."

Asked

Viewed 87 times

2

Next, I have a "Board" class in my project that at the time of Clean and Build is showing the following error: "Board.java uses unchecked or unsafe Operations."

I did some research and at first it would be a problem to use generic Arraylist, with no type definition. But the class Arraylist are all type-defined :|

Follows 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++;
        }
    }        
}

1 answer

2


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

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

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 of a vector of ArrayList to list lists, as in Java cannot create array of parameterized types, something like that:

    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 now you no longer have vectors, but lists of 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 instead of doing that:

listPedra[pos].add(pedra);

You must now wear it like this:

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

P.S.: give preference to having as reference an interface, as the List used. You can find the why of this good practice easily on the internet.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.