A apresentação está carregando. Por favor, espere

A apresentação está carregando. Por favor, espere

Programação II Prof. Mateus Raeder Universidade do Vale do Rio dos Sinos - São Leopoldo -

Apresentações semelhantes


Apresentação em tema: "Programação II Prof. Mateus Raeder Universidade do Vale do Rio dos Sinos - São Leopoldo -"— Transcrição da apresentação:

1 Programação II Prof. Mateus Raeder Universidade do Vale do Rio dos Sinos - São Leopoldo -

2 Programação II – Prof. Mateus Raeder Classes de Exceção public class UnderflowException extends Exception { public String toString() { return "UNDERFLOW!"; } public class OverflowException extends Exception { public String toString (){ return "OVERFLOW!"; }

3 Programação II – Prof. Mateus Raeder Listas com Exceções public class SequentialListComExcecao { private int list[]; private int last = -1; public SequentialListComExcecao(int size) { list = new int[size]; } public boolean isEmpty() { if (last == -1) return true; else return false; } public boolean isFull() { if (last == list.length - 1) return true; else return false; } public int getSize() { return last + 1; }

4 Programação II – Prof. Mateus Raeder Listas com Exceções public void insert (int element) throws OverflowException { if (isFull()) throw new OverflowException(); else list[++last] = element; } public void insert(int element, int index) throws OverflowException, ArrayIndexOutOfBoundsException { if (isFull()) throw new OverflowException(); else if (index last + 1) throw new ArrayIndexOutOfBoundsException(); else{ for (int i = last + 1; i > index; i--) { list[i] = list[i - 1]; } last++; list[index] = element; }

5 Programação II – Prof. Mateus Raeder Listas com Exceções public int remove(int index) throws UnderflowException, ArrayIndexOutOfBoundsException { if (isEmpty()) throw new UnderflowException(); else if (index last) throw new ArrayIndexOutOfBoundsException(); else{ int el = list[index]; int numberofElements = last - index; if (numberofElements > 0) { System.arraycopy(list, index + 1, list, index, numberofElements); } last--; return el; }

6 Programação II – Prof. Mateus Raeder Listas com Exceções public int get (int index) throws UnderflowException, ArrayIndexOutOfBoundsException { if (isEmpty()) throw new UnderflowException(); else if (index last) throw new ArrayIndexOutOfBoundsException(); else return list[index]; } public void print() { for (int i = 0; i <= last; i++) System.out.println(list[i]); }

7 Programação II – Prof. Mateus Raeder Bibliotecas Java Classes da biblioteca Java que implementam listas em Java: – java.util.Vector – java.util.ArrayList (desde versão 1.2) Qual a diferença? – Vector é sincronizado, assim todos os métodos da classe Vector são thread-safe (garante integridade dos dados quando mais de uma thread acessa) – Sincronização tem um custo no desempenho, assim quando não é necessário sincronizar, melhor utilizar ArrayList


Carregar ppt "Programação II Prof. Mateus Raeder Universidade do Vale do Rio dos Sinos - São Leopoldo -"

Apresentações semelhantes


Anúncios Google