Make a generic vector in Java

4

How to create a dynamic vector in Java with generic programming? This vector must have an initial size of size 4 and be enlarged as new elements need to be inserted.

The class must have:

  • Standard Builder.
  • Constructor with initial vector size parameter
  • bool remove (element)
  • bool add (Element)
  • First () element
  • Last () element
  • bool search (element)
  • int size ()
  • string print ().

What I've done so far:

package atividade;

public class Vetor<T> {

    T[] vetor;
    int qntElementos = 0;
    public Vetor(int tam) {
        vetor = (T[]) new Object[tam];
    }
    public boolean vazio() {
        return Tamanho() == 0;
    }
    public void Adicionar(T elemento) {
        if (Tamanho() == vetor.length) {
            redimensionar();
        }
        for (int i = 0; i < vetor.length; i++) {
            if (vetor[i] == null) {
                vetor[i] = (T) elemento;
                qntElementos++;
                break;
            }
        }
    }

    public void Remover(int posicao) throws ArrayIndexOutOfBoundsException {
        if (vazio()) {
            throw new ArrayIndexOutOfBoundsException("Vazio");
        }
        if (posicao < vetor.length) {
            vetor[posicao] = null;
            qntElementos--;
        } else {
            System.out.println("Posição inexistente!!!");
        }
    }

    public void RemoverInicio() {
        if (vazio()) {
            System.out.println("Vetor vazio!");
        }
        for (int i = 0; i < vetor.length; i++) {
            if (vetor[i] != null) {
                vetor[i] = null;
                break;
            }
            qntElementos--;
        }
    }

    public void RemoverFim() {
        if (vazio()) {
            System.out.println("Vetor vazio!");
        }
        for (int i = 0; i < vetor.length; i++) {
            if (vetor[i] == null) {
                vetor[i - 1] = null;
            }
            qntElementos--;
            break;
        }
        vetor[Tamanho()] = null;
    }

    public int Tamanho() {
        return qntElementos;
    }

    public void Limpar() {
        for (int i = 0; i < vetor.length; i++) {
            if (vetor[i] != null) {
                vetor[i] = null;
                qntElementos--;
            }
        }
    }

    public void redimensionar(){
        T[] novoVetor = (T[]) new Object[vetor.length * 2];
        if(vetor.length == Tamanho()){
            for(int i =0; i < vetor.length; i++){
                novoVetor[i] = vetor[i];
            }
            vetor = novoVetor;
        }
    }
}
    
asked by anonymous 05.05.2016 / 16:23

1 answer

4

I gave an improvement and I solve some problems, many syntax even. I did some extra methods, which were in the statement and others that might be useful. I adopted the typical Java style but I have not changed the names of methods and functions that I do not think are the best. And I did some things that I find most appropriate in each situation. Normally I would improve other things, but I've already moved too much. I can not guarantee that everything is ok, but I did a basic test. Obviously the implementation is a bit naïve.

I would like Java to have a better way of dealing with genericity. It even has a little improvement with reflection that I preferred to avoid. At least I used Comparable instead of Object to limit a little more.

class Vector<T> {
    T[] vetor;
    int qntElementos = 0;
    public Vector(int tam) {
        vetor = (T[]) new Comparable[tam];
    }
    public Vector() {
        this(4);
    }
    public void adicionar(T elemento) {
        if (tamanho() == vetor.length) {
            redimensionar();
        }
        vetor[tamanho()] = elemento;
        qntElementos++;
    }
    public T pegar(int posicao) {
        if (vazio() || posicao < 0 || posicao >= tamanho()) {
            throw new ArrayIndexOutOfBoundsException("Posição fora da faixa permitida");
        }
        return vetor[posicao];
    }
    public T pegarPrimeiro() {
        if (vazio()) {
            throw new ArrayIndexOutOfBoundsException("Posição fora da faixa permitida");
        }
        return vetor[0];
    }
    public T pegarUltimo() {
        if (vazio()) {
            throw new ArrayIndexOutOfBoundsException("Posição fora da faiza permitida");
        }
        return vetor[qntElementos - 1];
    }
    public int procurar(T elemento) {
        for (int i = 0; i < qntElementos; i++) {
            if (vetor[i].equals(elemento)) {
                return i;
            }
        }
        return -1;
    }
    public boolean removerElemento(T elemento) {
        return remover(procurar(elemento));
    }
    public boolean remover(int posicao) {
        if (!vazio() && posicao >= 0 && posicao < tamanho()) {
            for (int i = posicao; i < qntElementos; i++) {
                vetor[i] = vetor[i + 1];
            }
            vetor[tamanho()] = null;
            qntElementos--;
            return true;
        } else {
            return false;
        }
    }
    public boolean removerInicio() {
        if (vazio()) {
            return false;
        }
        for (int i = 0; i < qntElementos; i++) {
            vetor[i] = vetor[i + 1];
        }
        qntElementos--;
        return true;
    }
    public boolean removerFim() {
        if (vazio()) {
            return false;
        }
        vetor[tamanho()] = null;
        qntElementos--;
        return true;
    }
    public int tamanho() {
        return qntElementos;
    }
    public boolean vazio() {
        return tamanho() == 0;
    }
    public void limpar() {
        vetor = (T[]) new Comparable[4];
    }
    private void redimensionar() {
        T[] novoVetor = (T[]) new Comparable[vetor.length * 2];
        if (vetor.length == tamanho()){
            for (int i = 0; i < vetor.length; i++){
                novoVetor[i] = vetor[i];
            }
            vetor = novoVetor;
        }
    }
}

See running on ideone .

    
05.05.2016 / 18:17