ArrayList y Hashmap
El almacenamiento de ArrayList es una matriz.
El almacenamiento de HashMap es una matriz más una lista vinculada.
Los siguientes MyArrayList y Myhashmap no se usan en el trabajo real. Lo más probable de usar es entrevistar y encontrar un trabajo y engañar a los demás. Aunque no es útil en el trabajo, no significa que sea inútil. Puede ayudarnos a comprender sus principios de implementación. Una vez completada la implementación, verificaremos cuidadosamente el código fuente en el JDK y descubriremos dónde pueden aprender otras personas en su implementación.
MyArrayList
clase pública myArrayList <E> {private int capacidad = 10; Tamaño privado int = 0; privado e [] valores = nulo; @Suppleswarnings ("sin verificar") public myArrayList () {valores = (e []) nuevo objeto [capacidad]; } @Suppleswarnings ("sin control") public myArrayList (int capacidad) {this.capacity = capacidad; valores = (e []) Nuevo objeto [this.capacity]; } public void put (e e) {if (e == null) {throw new runtimeException ("El valor no debe ser nulo"); } if (size> = capacidad) {agrargeCapacity (); } valores [tamaño] = e; tamaño ++; } public e get (int index) {if (index> = size) {Throw New RuntimeException ("El índice:" + index + "está fuera de la banda."); } valores de retorno [índice]; } public void remove (int index) {if (index> = size) {Throw New RuntimeException ("El índice:" + index + "está fuera de banda"); } for (int i = index; i <size - 1; i ++) {valores [i] = valores [i+1]; } valores [tamaño - 1] = nulo; tamaño--; } @Suppleswarnings ("sin verificar") privado void ampliargecapacity () {capacidad = capacidad * 2; E [] tmpValues = (e []) nuevo objeto [capacidad]; System.ArrayCopy (valores, 0, tmpvalues, 0, tamaño); valores = tmpValues; } public String toString () {StringBuilder sb = new StringBuilder (); sb.append ("["); for (int i = 0; i <size; i ++) {sb.append (valores [i]). append (","); } if (size> 0) {sb.deletecharat (sb.length () - 1); } sb.append ("]"); return sb.ToString (); } / ** * @param args * / public static void main (string [] args) {myArrayList <String> myList = new MyArrayList <String> (); myList.put ("1"); mylist.put ("2"); mylist.put ("3"); mylist.put ("4"); myList.put ("5"); mylist.put ("6"); mylist.put ("7"); mylist.put ("8"); mylist.put ("9"); mylist.remove (7); System.out.println (myList.ToString ()); }} Myhashmap
clase pública myhashmap <k, v> {// Capacidad de inicialización Private Int Capacidad = 10; // Entidades totales privadas int tamaño = 0; entidad privada <k, v> [] entidades = nulo; @SupessWarnings ("sin control") public myHashMap () {entidades = nueva entidad [capacidad]; } public void put (k key, v valor) {if (key == null) {throw new runtimeException ("La clave es nula"); } rehash (); Entidad <k, v> newEntity = New Entity <k, v> (clave, valor); poner (nuevo entidad, this.Entities, this.capacity); } private void put (entidad <k, v> newEntity, entidad <k, v> [] entidades, int capacidad) {int index = newEntity.getKey (). hashcode () % capacidad; Entidad <k, v> entidad = entidades [índice]; Entidad <k, v> firstEntity = entidades [índice]; if (entity == null) {entidades [index] = newEntity; tamaño ++; } else {if (newEntity.getKey (). Equals (entity.getKey ())) {// Encuentre la misma clave para la primera entidad, si se encuentra, reemplace el valor anterior a New Value NewEntity.SetNext (entity.getNext ()); newEntity.setPre (entity.getPre ()); if (entity.getNext ()! = null) {entity.getNext (). setPre (newEntity); } entidades [index] = newEntity; } else if (entity.getNext ()! = null) {while (entity.getNext ()! = NULL) {// Encuentre la misma clave para toda la siguiente entidad, si busca luego reemplaza el valor anterior a nuevo valor entity = entity.getNext (); if (newEntity.getKey (). Equals (entity.getKey ()))) {newEntity.setPre (entity.getPre ()); newEntity.setNext (entity.getNext ()); if (entity.getNext ()! = null) {entity.getNext (). setPre (newEntity); } entidades [index] = newEntity; devolver; }} // No se puede encontrar la misma clave, luego inserte la nueva entidad en el encabezado NewEntity.setNext (PrimeraTidad); newEntity.setPre (FirstEntity.getPre ()); FirstEntity.SetPre (NewEntentity); entidades [índice] = neweNity; tamaño ++; } else {// No puede encontrar la misma clave, luego coloque la nueva entidad en Head NewEntity.SetNext (PrimeraTidad); FirstEntity.SetPre (NewEntentity); entidades [índice] = neweNity; tamaño ++; }}} public v get (k key) {if (key == null) {throw new runtimeException ("La clave es nula"); } int index = key.hashcode () % capacidad; Entidad <k, v> entidad = entidades [índice]; if (entity! = null) {if (entity.getKey (). Equals (key)) {return entity.getValue (); } else {entity = entity.getNext (); while (entity! = null) {if (entity.getKey (). Equals (key)) {return entity.getValue (); } entity = entity.getNext (); }}} return null; } public void remove (k key) {if (key == NULL) {Throw New RuntimeException ("La clave es nula"); } int index = key.hashcode () % capacidad; Entidad <k, v> entidad = entidades [índice]; if (entity! = null) {if (entity.getKey (). Equals (key)) {if (entity.getNext ()! = null) {// elimina la primera entidad entity.getNext (). setPre (entity.getPre ()); entidades [index] = entity.getNext (); entidad = nulo; } else {// vacía estas entidades de índice [index] = null; } tamaño--; } else {entity = entity.getNext (); while (entity! = null) {if (entity.getKey (). Equals (key)) {if (entity.getNext ()! = null) {entity.getPre (). setNext (entity.getNext ()); entity.getNext (). SetPre (entity.getPre ()); entidad = nulo; } else {// libera la entidad encontrada entidad.getPre (). setNext (null); entidad = nulo; } tamaño--; devolver; } entity = entity.getNext (); }}}} public string toString () {StringBuilder sb = new StringBuilder (); for (int i = 0; i <capacidad; i ++) {sb.append ("index ="). append (i) .append ("["); Boolean HasEntity = false; Entidad <k, v> entidad = entidades [i]; if (entity! = null) {hasEntity = true; } while (entity! = null) {sb.append ("["). append (entity.getKey ()). append ("="). append (entity.getValue ()). append ("]"). append (","); entity = entity.getNext (); } if (HasEntity) {sb.deletecharat (sb.length () - 1); } sb.append ("]/n"); } return sb.ToString (); } / ** * Estrategia de rehacer simple, si el tamaño es más grande que la capacidad, luego vuelva a hacer acción * / private void rehash () {if (size> = capacidad) {int newCapacity = capacidad * 2; @SupessWarnings ("sin control") entidad <k, v> [] newEntities = New Entity [NewCapacity]; para (int i = 0; i <capacidad; i ++) {entidad <k, v> entidad = entidades [i]; while (entity! = null) {put (entidad, newEntities, newcapacity); entity = entity.getNext (); }} this.capacity = newCapacity; this.Entities = NewEntities; }} public static void main (string [] args) {myhashmap <string, string> map = new MyHashMap <String, String> (); map.put ("one", "1"); map.put ("dos", "2"); map.put ("tres", "3"); map.put ("cuatro", "4"); map.put ("cinco", "5"); map.put ("Six", "6"); map.put ("siete", "7"); map.put ("ocho", "8"); map.put ("nueve", "9"); map.put ("diez", "10"); System.out.println (map.get ("one")); System.out.println (map.get ("dos")); System.out.println (map.get ("tres")); System.out.println (map.get ("cuatro")); System.out.println (map.get ("cinco")); System.out.println (map.get ("seis")); System.out.println (map.get ("siete")); System.out.println (map.get ("ocho")); System.out.println (map.get ("nueve")); System.out.println (map.get ("diez")); System.out.println (map.ToString ()); map.remove ("nueve"); map.remove ("tres"); System.out.println (map.get ("one")); System.out.println (map.get ("dos")); System.out.println (map.get ("tres")); System.out.println (map.get ("cuatro")); System.out.println (map.get ("cinco")); System.out.println (map.get ("seis")); System.out.println (map.get ("siete")); System.out.println (map.get ("ocho")); System.out.println (map.get ("nueve")); System.out.println (map.get ("diez")); System.out.println (map.ToString ()); }} Entidad de clase <k, v> {Key private k; valor v privado; entidad privada <k, v> pre; entidad privada <k, v> siguiente; Entidad pública (K Key, V valor) {this.key = key; this.Value = value; } public k getkey () {return key; } public void setKey (k key) {this.key = key; } public v getValue () {Valor de retorno; } public void setValue (V valor) {this.value = valor; } Entidad pública <k, v> getPre () {return pre; } public void setPre (entidad <k, v> pre) {this.pre = pre; } Entidad pública <k, v> getNext () {return Next; } public void setNext (entidad <k, v> next) {this.next = next; }}Gracias por leer, espero que pueda ayudarte. ¡Gracias por su apoyo para este sitio!