fork download
  1. class Ideone {
  2. public static void main (String[] args) {
  3. Vector<Integer> a = new Vector<Integer>();
  4. a.adicionar(1);
  5. a.adicionar(2);
  6. a.adicionar(3);
  7. a.adicionar(4);
  8. a.adicionar(5);
  9. System.out.println(a.pegar(3));
  10. System.out.println(a.pegarPrimeiro());
  11. System.out.println(a.pegarUltimo());
  12. System.out.println(a.tamanho());
  13. System.out.println(a.vazio());
  14. System.out.println(a.procurar(3));
  15. System.out.println(a.removerElemento(3));
  16. System.out.println(a.remover(2));
  17. System.out.println(a.removerInicio());
  18. System.out.println(a.removerFim());
  19. System.out.println(a.pegar(0));
  20. System.out.println(a.tamanho());
  21. System.out.println(a.vazio());
  22. System.out.println(a.remover(2));
  23. System.out.println(a.removerInicio());
  24. System.out.println(a.removerFim());
  25. a.limpar();
  26. }
  27. }
  28.  
  29. class Vector<T> {
  30. T[] vetor;
  31. int qntElementos = 0;
  32. public Vector(int tam) {
  33. vetor = (T[]) new Comparable[tam];
  34. }
  35. public Vector() {
  36. this(4);
  37. }
  38. public void adicionar(T elemento) {
  39. if (tamanho() == vetor.length) redimensionar();
  40. vetor[tamanho()] = elemento;
  41. qntElementos++;
  42. }
  43. public T pegar(int posicao) {
  44. if (vazio() || posicao < 0 || posicao >= tamanho()) throw new ArrayIndexOutOfBoundsException("Posição fora da faixa permitida");
  45. return vetor[posicao];
  46. }
  47. public T pegarPrimeiro() {
  48. if (vazio()) throw new ArrayIndexOutOfBoundsException("Posição fora da faixa permitida");
  49. return vetor[0];
  50. }
  51. public T pegarUltimo() {
  52. if (vazio()) throw new ArrayIndexOutOfBoundsException("Posição fora da faiza permitida");
  53. return vetor[qntElementos - 1];
  54. }
  55. public int procurar(T elemento) {
  56. for (int i = 0; i < qntElementos; i++) {
  57. if (vetor[i].equals(elemento)) return i;
  58. }
  59. return -1;
  60. }
  61. public boolean removerElemento(T elemento) {
  62. return remover(procurar(elemento));
  63. }
  64. public boolean remover(int posicao) {
  65. if (!vazio() && posicao >= 0 && posicao < tamanho()) {
  66. for (int i = posicao; i < qntElementos; i++) vetor[i] = vetor[i + 1];
  67. vetor[tamanho()] = null;
  68. qntElementos--;
  69. return true;
  70. } else {
  71. return false;
  72. }
  73. }
  74. public boolean removerInicio() {
  75. if (vazio()) return false;
  76. for (int i = 0; i < qntElementos; i++) vetor[i] = vetor[i + 1];
  77. qntElementos--;
  78. return true;
  79. }
  80. public boolean removerFim() {
  81. if (vazio()) return false;
  82. vetor[tamanho()] = null;
  83. qntElementos--;
  84. return true;
  85. }
  86. public int tamanho() {
  87. return qntElementos;
  88. }
  89. public boolean vazio() {
  90. return tamanho() == 0;
  91. }
  92. public void limpar() {
  93. vetor = (T[]) new Comparable[4];
  94. }
  95. private void redimensionar() {
  96. T[] novoVetor = (T[]) new Comparable[vetor.length * 2];
  97. if (vetor.length == tamanho()) {
  98. for (int i = 0; i < vetor.length; i++) novoVetor[i] = vetor[i];
  99. vetor = novoVetor;
  100. }
  101. }
  102. }
  103.  
  104. //https://pt.stackoverflow.com/q/126968/101
Success #stdin #stdout 0.09s 32628KB
stdin
Standard input is empty
stdout
4
1
5
5
false
2
true
true
true
true
2
1
false
false
true
false