Nao executa a tabela hash

0 respostas
L
package AVL;
import java.util.ArrayList;
import java.util.*;

import Tabela.Cliente;


public class AVL {
	private List<AVL> tabela = new ArrayList<AVL>();
	private int tamanho = 0;

	private Cliente[] Lista = null;

	private int n = 0;


	public Cliente Busca(int cod) {
		if (Lista.length > 0) {
			int inicio = 0;
			int meio = 0;
			int fim = Lista.length - 1;
			if (cod >= Lista[0].getCodigo()&& cod <= Lista[Lista.length - 1].getCodigo()) {
				while ((Lista[inicio].getCodigo() <= Lista[fim].getCodigo())) {
					meio = (inicio + fim) / 2;
					if (cod == Lista[meio].getCodigo()) {
						return Lista[meio];
					} else {
						if (cod < Lista[meio].getCodigo()) {
							fim = meio - 1;
						} else {
							inicio = meio + 1;
						}
					}
				}
			}
		}
		return null;
	}

	public Cliente[] getLista() {
		return Lista;
	}

	/**
	 * Insere aluno na lista
	 */
	public void inserir(Cliente c) {
		if (Lista == null) {
			Cliente[] aux = new Cliente[1];
			aux[n] = c;
			Lista = aux;
			n++;
			ordenação();
		} else {
			if (n == Lista.length) {
				Cliente[] aux = new Cliente[n + 1];
				for (int i = 0; i < n; i++) {
					aux[i] = Lista[i];
				}
				aux[n] = c;
				Lista = aux;
				n++;
				ordenação();
			} else {
				Lista[n] = c;
				n++;
				ordenação();
			}
		}
	}

	/**
	 * Retorna se a lista esta vasia
	 */
	public boolean isEmpty() {
		if (Lista == null) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * Remove um elemento da lista
	 */
	public void remove(Cliente c) {
		int aux = 0;
		boolean op = false;
		for (int i = 0; i < n; i++) {
			if (Lista[i].equals(c)) {
				aux = i;
				op = true;
			}
		}
		if (aux == n - 1) {
			n--;
			Cliente[] aux2 = new Cliente[n];
			for (int i = 0; i < n; i++) {
				aux2[i] = Lista[i];
			}
			Lista = aux2;
		} else {
			for (int i = aux; i < n - 1; i++) {
				Lista[i] = Lista[i + 1];
			}
			n--;
			Cliente[] aux2 = new Cliente[n];
			for (int i = 0; i < n; i++) {
				aux2[i] = Lista[i];
			}
			Lista = aux2;
			if (op) {
			} else {
				System.out.print("não encontrado");
			}
		}
	}

	/**
	 * Retorna o tamanho da lista
	 */
	public int size() {
		return n;
	}

	public void mostrar() {
		System.out.print("[");
		for (int i = 0; i < n; i++) {
			// System.out.println(i + " - " + Lista[i].toString());
			System.out.print(Lista[i].getCodigo() + "-" + Lista[i].getNome() + ((i + 1 != Lista.length) ? "|" : ""));
		}
		System.out.println("]");
	}

	/**
	 * Ordena a lista pelo codigo do cliente
	 */
	public void ordenação() {
		for (int i = 0; i < Lista.length; i++) {
			for (int j = 0; j < Lista.length; j++) {
				if (Lista[j].getCodigo() > Lista[i].getCodigo()) {
					Cliente aux = Lista[i];
					Lista[i] = Lista[j];
					Lista[j] = aux;
				}
			}
		}
	}

	public void addAll(){

	}

	public void imprimeTabela() {
		for (int i = 0; i < tabela.size(); i++) {
			tabela.get(i).mostrar();
		}

	}

	public void addAll(AVL avl) {
		// TODO Auto-generated method stub

	}

	public boolean contem(Cliente cliente) {
		int indice=this.calculaIndice(cliente);
		AVL lista=(AVL) this.tabela.get(indice);//pega tudo q tem dentro da posição do vetor
		Cliente clienteBusca; //cria cliente para comparar
		for(int i=0;i<lista.size();i++){ 
			clienteBusca=  lista.Busca(i);//compara se o passado por parametro tem algum elemento igual na lista da posição
			if(clienteBusca.getCodigo()== cliente.getCodigo()){
				return true;
			}
		}
		return false;

	}

	private int calculaIndice(Cliente cliente) {
		return Math.abs(cliente.hashCode())% tabela.size();//math: garante q os valores sejam todos positivos 
	}

}


package Tabela;

public class Cliente {
	private int codigo;
	private String nome;


	public Cliente(int cod, String n) {
		setCodigo(cod);
		setNome(n);

	} 
	public int hashCode(){
		int codigo = 1;
		String aux = String.valueOf(this.codigo);
		for (int i = 0; i < aux.length(); i++) {
			codigo = 31 * codigo + aux.charAt(0) ;
		}
		return codigo;
	}


	public int getCodigo() {
		return codigo;
	}
	public void setCodigo(int rm) {
		this.codigo = rm;
	}

	public String getNome() {
		return nome;
	}

	protected void setNome(String n) {
		nome = n;
	}


}

package Tabela;

public class Principal { 

        public static void main(String[] args) {
                TabelaHash hash = new TabelaHash();
                Cliente c1 = new Cliente(60394, "Alexandre");
                Cliente c2 = new Cliente(60395, "Rogério");
                Cliente c3 = new Cliente(60395, "Jose");
                Cliente c4 = new Cliente(60396, "Maria");
                Cliente c5 = new Cliente(60397, "Ana");
                Cliente c6 = new Cliente(60398, "Marcelo");

                hash.adiciona(c1);
                hash.adiciona(c2);
                hash.adiciona(c3);
                hash.adiciona(c4);
                hash.adiciona(c5);
                hash.adiciona(c6);

                System.out.println("Tamanho da tabela: " + hash.tamanho());
                hash.imprimeTabela();
                System.out.println("-----------------------------------------");

                Cliente cliente = new Cliente(60397, "");
                hash.remove(cliente);
                System.out.println("Aluno 60397 removido.");
                System.out.println("Tamanho da tabela: " + hash.tamanho());
                hash.imprimeTabela();
                System.out.println("-----------------------------------------");
}
}

package Tabela;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.*;
import AVL.AVL;


public class TabelaHash {

	private List<AVL> tabela = new ArrayList<AVL>();
	private int tamanho = 0;
	private Object codigo;

	
	public TabelaHash() {
		for (int i = 0; i < 10; i++) {
			AVL lista = new AVL();
			tabela.add(lista);
		}
	}

	public AVL pegaTodos() {
		AVL clientes = new AVL();
		for (int i = 0; i < this.tabela.size(); i++) {
			clientes.addAll(this.tabela.get(i));
		}
		return clientes;
	}

	private void redimensionaTabela(int novaCapacidade) {
		AVL clientes = this.pegaTodos();
		this.tabela.clear();
		for (int i = 0; i < novaCapacidade; i++) {
			this.tabela.add(new AVL());
		}

	}

	private void verificaCarga() {
		int capacidade = this.tabela.size();
		double carga = (double) this.tamanho / capacidade;
		if (carga > 0.75) {
			this.redimensionaTabela(capacidade * 2);
		} else if (carga < 0.25) {
			this.redimensionaTabela(Math.max(capacidade / 2, 10));
		}
	}

	public void adiciona(Cliente cliente) {
		if (!this.contem(cliente)) {
			this.verificaCarga();
		}
		int indice = this.calculaIndice(cliente);
		AVL lista = (AVL) this.tabela.get(indice);
		lista.inserir(cliente);
		this.tamanho++;
	}

	public int calculaIndice(Cliente cliente){
		return Math.abs(cliente.hashCode())% tabela.size();//math: garante q os valores sejam todos positivos 
	}

	public void remove(Cliente cliente) {
		if (this.contem(cliente)) {
			int indice = this.calculaIndice(cliente);
			AVL lista = this.tabela.get(indice);
			lista.remove(cliente);
			this.tamanho--;
			this.verificaCarga();
		}
	}

	private boolean contem(Cliente cliente) {
		int indice = this.calculaIndice(cliente);
		AVL lista = (AVL) this.tabela.get(indice);
		return lista.contem(cliente);
	}

	public Cliente pegaItem(Cliente cliente) {
		int indice = this.calculaIndice(cliente);
		return tabela.get(indice).Busca(cliente.getCodigo());
	}

	public void Listatabela() {
		for (int i = 0; i < tabela.size(); i++) {
			tabela.get(i).imprimeTabela();
		}
	}

	public int tamanho() {
		return this.tamanho;
	}


	public void imprimeTabela() {
		for (int i = 0; i < tabela.size(); i++) {
			tabela.get(i).imprimeTabela();
		}
	}
		public int hashCode(){
			int codigo = 1;
			String aux = String.valueOf(this.codigo);
			for (int i = 0; i < aux.length(); i++) {
				codigo = 31 * codigo + aux.charAt(i);
			}
			return codigo;
		} 
		
		
}

O codigo nao aparece nenhum erro, porem nao consegue executar
alguem pode me auxiliar
vlws
abracos

Criado 22 de junho de 2008
Respostas 0
Participantes 1