check items does not work

0

I want to make a method that will check if an element has already been added to a list, if it is already going to increment 1. This is the class I made, but it is giving error.

public class Rascunho {

    private int [] vetor;
    private int countValor ;
    private int c = 0;

        public Rascunho () {
            vetor = new int [7];
            countValor = 0;
        }

        public void insertValor (int valor) {
            countValor ++;
            int i = 0; 
            vetor [i] = valor;
        }

        public int verificar () {
            for (int i = 0; i<countValor-1; i++)
                for (int j= 0; j < countValor; j++) {       
                    if (vetor[j+1] == vetor[i]) {c++;}
                }
            return c;
        }
    }
    
asked by anonymous 13.11.2018 / 15:22

1 answer

0
___ erkimt ___ check items does not work ______ qstntxt ___

I want to make a method that will check if an element has already been added to a list, if it is already going to increment 1. This is the class I made, but it is giving error.

   public void insertValor (int valor) {
        countValor ++;
        vetor [countValor-1] = valor;
    }

   public int verificar () {
        for (int i = 0; i<countValor-1; i++)
            for (int j = i+1; j < countValor; j++) {       
                if (vetor[j] == vetor[i]){
                    c++;
                }
            }
        return c;
    }
    
______ ___ azszpr343150

The method for entering value is always entering position 0. The method for checking checks more than once for the same value.

   public void insertValor (int valor) {
        countValor ++;
        vetor [countValor-1] = valor;
    }

   public int verificar () {
        for (int i = 0; i<countValor-1; i++)
            for (int j = i+1; j < countValor; j++) {       
                if (vetor[j] == vetor[i]){
                    c++;
                }
            }
        return c;
    }
    
___
13.11.2018 / 15:50