1
I’m concluding this generic vector activity, but I’m having trouble returning the last and first element of the list. and to remove the element.
public class Vetor<T>{
    T[] vetor;
    int qntElementos = 0;
    public Vetor(int tam) {
        vetor = (T[]) new Comparable[tam];          //Comparadando os elementos de um vetor generico         
    }
    public boolean vazio() {
        return Tamanho() == 0;
    }
    public void Adicionar(T elemento) {
        if (Tamanho() == vetor.length) {            // Verifica se o tamanho de elementos é igual ao tamanho do vetor
            redimensionar();
        }
        for (int i = 0; i < vetor.length; i++) {
            if (vetor[i] == null) {
                vetor[i] = (T) elemento;
                qntElementos++;
                break;
            }
        }
    }
    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 int First(){
        if (vazio()) {
            throw new ArrayIndexOutOfBoundsException("Posição fora da faixa permitida");
        }
        return (int) vetor[0];
    }
    public int Last(){
        if (vazio()) {
            throw new ArrayIndexOutOfBoundsException("Posição fora da faiza permitida");
        }
        return (int) vetor[qntElementos - 1];
    }
    public T pegar(int posicao) {
        if (vazio() || posicao < 0 || posicao >= Tamanho()) {
            throw new ArrayIndexOutOfBoundsException("Posição fora da faixa permitida");
        }
        System.out.print("O valor esta na posiçao: " + vetor[posicao]);
        return vetor[posicao];
    }
    public int Tamanho() {
        System.out.print("A lista tem: " + qntElementos + " posições.");
        return qntElementos;
    }
    public void redimensionar() {
        T[] novoVetor = (T[]) new Object[vetor.length + 4]; // Aumenta o tamanho do vetor em +4
        if (vetor.length == Tamanho()) {
            for(int i = 0; i < vetor.length; i++) {
                novoVetor[i] = vetor[i];
            }
            vetor = novoVetor;
        }
    }
    public void print() {
        if(vetor[0] == null) {
            System.out.println("A lista esta vazia");
        }else {
            for (T elemento : vetor) {
                System.out.print("[ " + elemento + " ]");
            }
        }System.out.println("\n");
    }
public class Main {
    private static boolean pass;
    public static void printMenu(){
             System.out.println("------------MENU--------------:");
            System.out.println("1 - Retorna o tamanho da lista.");
            System.out.println("2 - Retornar uma referencia para o primeiro elemento da lista.");
            System.out.println("3 - Retornar uma referencia para o primeiro elemento da lista.");
            System.out.println("4 - Inserir um elemento no fim da lista");
            System.out.println("5 - Imprimir a lista.");
            System.out.println("6 - Buscar um numero dentro do Vetor.");
            System.out.println("7 - Remover um numero dentro do Vetor.");
            System.out.println("0 - Sair do programa.");         
    }
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        boolean go = true;
        pass = true;
            do{
                try {
                     System.out.println("Digite o tamanho do array para FILA: ");
                     int tam = in.nextInt(); 
                     Vetor vet = new Vetor(tam);
                     pass = false;
                     int opcao = -1; 
                     do{          
                         printMenu();  
                         opcao = in.nextInt();
                         switch(opcao){                        
                             case 1:
                                 System.out.println("Retornando o tamanho da fila:");
                                 System.out.println (vet.Tamanho());
                                 break;
                             case 2:
                                 System.out.println("Retornando referencia para o primeiro elemento da fila:");
                                 System.out.println (vet.First());
                                 break;
                             case 3:
                                 System.out.println("Retornado referencia para o ultimo elemento da fila:");
                                 System.out.println (vet.Last());
                                 break;
                             case 4:
                                for (int i = 1; i <= tam; i++){
                                    System.out.print("Digite um valor para inserir na fila: \n");
                                    int valor = in.nextInt( );
                                    System.out.printf("Inserindo elemento, %s no fim da fila \n",valor);
                                    vet.Adicionar(valor);
                                }
                                 break;
                             case 5:
                                 System.out.println("Imprimindo a fila:");
                                 vet.print();
                                 break;
                             case 6:
                                 System.out.println("Retornado a busca do elemento: ");
                                 System.out.print("Digite um valor para buscar na lista: \n");
                                 int valor = in.nextInt( );
                                 System.out.print("O valor que está na posição " + valor + " é: ");
                                 System.out.println (vet.pegar(valor));
                                 break;
                             case 7:
                                 System.out.println("Removendo elementos da lista: ");
                                 System.out.println (vet.remover());
                                 break;
                             case 0:
                                 System.out.println("Finalizando o programa!");
                                 System.out.println("Confirma saida? S-sim outro-não");
                                 String confirm = in.next();
                                 if(confirm.equals("s") || confirm.equals("S")){
                                     System.out.println("Programa finalizado!");
                                     go = false;
                                 }
                                 break;
                             default:
                                 System.out.println("Valor invalido!");                        
                         }
                     }while(go);
                 }catch(InputMismatchException erro1){
                     pass = true;
                     System.out.println("O tipo de valor digitado e invalido : ");
                     in.nextLine(); // discards the entry invalidates and frees the Scanner function again
                 }catch(ArrayIndexOutOfBoundsException erro2){
                     pass = true;    
                     int teste = parseInt(erro2.getMessage());
                     if(teste == -1){
                         System.out.println("Underflow");
                     }else if(teste > -1){
                         System.out.println("Overflow"); // provisional treatment should be double the size of the array.
                     }                   
                 }catch(NegativeArraySizeException erro3){
                     System.out.println("Numero negativo invalido para o tamanho do array!");
                 } 
            }while(pass);
      }
Possible duplicate of Make a generic vector in Java
– user28595
What’s the matter?
– Maniero
It is not duplicate, I have been recommending to create a new question. my doubt is that I am not able to show the first and last element of the list, nor being able to remove an element.
– Carlos Diego