Ver Mensaje Individual
  #10 (permalink)  
Antiguo 23/04/2017, 11:59
Avatar de detective_jd
detective_jd
 
Fecha de Ingreso: abril-2011
Ubicación: Salto
Mensajes: 437
Antigüedad: 13 años, 1 mes
Puntos: 6
Respuesta: Implementar TablaHash

Hola CalgaryCorpus justamente eso es lo que no quiero, lo había hecho para continuar con el desarrollo del HashMap, pero ahora me veo en que necesito que me obtenga bien el índice sin la resta pero no me sale bien, no tengo un plan cómo para hacerlo andar.
Por otro lado también tengo problemas en caso de que por ejemplo: tengo dicho map de tamaño 4 por defecto pero me da error cuando quiero agregar en la posición 3 y no me deja.

Los que andan son put, getIndex, setEntry (provisorio de momento, xq cuando haga andar bien los otros 2, trataré de deshacerme de él).

Código Java:
Ver original
  1. @Override
  2.     public V put(K key, V value) {
  3.         if(key == null){
  4.             return null;
  5.         } else {
  6.             int index = getIndex(key);
  7.             System.out.println("indice -> "+ index);
  8.             Entry<K, V> pair = table[index];
  9.             if (pair != null && pair.getKey().equals(key)) {
  10.                 V oldValue = pair.getValue();
  11.                 pair.setValue(value);
  12.                 return oldValue;
  13.             }
  14.             setEntry(key, value, index);
  15.             return value;
  16.         }
  17.     }
  18.     private int getIndex(Object key) {
  19.        return (key.hashCode() & 0x7FFFFFFF) % table.length;
  20.     }
  21.  
  22.     private void setEntry(K key, V value, int index) {
  23.         Entry<K, V> e = table[index];
  24.         while (e != null) {
  25.             index++;
  26.             if (index >= capacity) {
  27.                 table = Arrays.copyOf(table, table.length +1);
  28.                 put(key,value);
  29.                 return;
  30.             }
  31.             e = table[index];
  32.         }
  33.         table[index] = new Entry(key, value);
  34.         size++;
  35.     }

Espero sus respuestas y Saludos.