배열리스트 및 해시 맵
ArrayList의 저장은 배열입니다.
해시 맵의 저장소는 배열과 링크 된 목록입니다.
다음 MyArrayList 및 MyHashMap은 실제 작업에 사용되지 않습니다. 가장 많이 사용하는 것은 인터뷰를하고 직업을 찾고 다른 사람들을 바보하는 것입니다. 직장에서 유용하지는 않지만 쓸모가 없다는 것을 의미하지는 않습니다. 구현 원칙을 이해하는 데 도움이 될 수 있습니다. 구현이 완료되면 JDK의 소스 코드를 신중하게 확인하고 다른 사람들이 구현에서 배울 수있는 위치를 알게 될 것입니다.
MyArraylist
공개 클래스 myArrayList <e> {개인 int 용량 = 10; 개인 int 크기 = 0; private e [] value = null; @SuppressWarnings ( "확인되지 않은") public myArrayList () {value = (e []) 새 개체 [용량]; } @SuppressWarnings ( "확인되지 않은") public myArrayList (int capacity) {this.capacity = 용량; value = (e []) 새 개체 [this.capacity]; } public void put (e e) {if (e == null) {throw new runtimeexception ( "값은 null이되어서는 안됩니다."); } if (size> = faction) {wepargecapacity (); } 값 [size] = e; 크기 ++; } public e get (int index) {if (index> = size) {wrach new runtimeexception ( "index :" + index + "는 대역에서 벗어납니다."); } return 값 [index]; } public void remove (int index) {if (index> = size) {새로운 runtimeexception ( "index :" + index + "가 대역 밖으로 나옵니다."); } for (int i = index; i <size -1; i ++) {value [i] = value [i+1]; } 값 [size -1] = null; 크기--; } @SuppressWarnings ( "확인되지 않은") Private Void inlargecapacity () {용량 = 용량 * 2; e [] tmpvalues = (e []) 새 개체 [용량]; System.ArrayCopy (값, 0, tmpValues, 0, size); 값 = tmpvalues; } public String toString () {StringBuilder sb = new StringBuilder (); sb.append ( "["); for (int i = 0; i <size; i ++) {sb.append (values [i]). Append ( ","); } if (size> 0) {sb.deletecharat (sb.length () -1); } sb.append ( "]"); 반환 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
공개 클래스 MyHashMap <k, v> {// 초기화 용량 개인 int 용량 = 10; // 전체 엔티티 개인 int 크기 = 0; 개인 엔티티 <k, v> [] entities = null; @suppresswarnings ( "확인되지 않은") public myhashmap () {entities = 새 엔티티 [용량]; } public void put (k key, v value) {if (key == null) {throw new runtimeexception ( "키는 null"); } rashash (); 엔티티 <k, v> newentity = 새로운 엔티티 <k, v> (키, 값); put (newentity, this.entities, this.capacity); } private void put (엔티티 <k, v> newentity, 엔티티 <k, v> [] 엔티티, int 용량) {int index = newentity.getKey (). hashcode () % 용량; 엔티티 <k, v> entity = 엔티티 [index]; 엔티티 <k, v> firstentity = 엔티티 [index]; if (entity == null) {entities [index] = newentity; 크기 ++; } else {if (newentity.getKey (). equals (entity.getKey ())) {// 첫 번째 엔티티에 대해 동일한 키를 찾으십시오. 찾면 이전 값을 새 값 newentity.setNext (entity.getNext ())로 바꾸십시오. newentity.setpre (entity.getpre ()); if (entity.getNext ()! = null) {entity.getNext (). setPre (newentity); } 엔티티 [index] = newentity; } else if (entity.getNext ()! = null) {while (entity.getNext ()! = null) {// 모든 다음 엔티티에 대해 동일한 키를 찾으면 이전 값을 새 값 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); } 엔티티 [index] = newentity; 반품; }} // 동일한 키를 찾을 수없고 Header Newentity.setNext (Firstentity)에 새 엔티티를 삽입합니다. newentity.setpre (firstentity.getpre ()); Firstentity.setpre (Newentity); 엔티티 [index] = Newentity; 크기 ++; } else {// 동일한 키를 찾을 수없고 새 엔티티를 Head Newentity.setNext (Firstentity)에 넣습니다. Firstentity.setpre (Newentity); 엔티티 [index] = Newentity; 크기 ++; }}} public v get (k key) {if (key == null) {throw new runtimeexception ( "키는 null"); } int index = key.hashCode () % 용량; 엔티티 <k, v> entity = 엔티티 [index]; 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 ( "키는 null"); } int index = key.hashCode () % 용량; 엔티티 <k, v> entity = 엔티티 [index]; if (entity! = null) {if (entity.getKey (). equals (key)) {if (entity.getNext ()! = null) {// 첫 번째 엔티티 엔티티.getNext (). setPre (entity.getPre ()); 엔티티 [index] = entity.getNext (); 엔티티 = null; } else {//이 인덱스 엔티티를 비우십시오 [index] = null; } 크기-; } 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 ()); 엔티티 = null; } else {// 찾은 엔티티 엔티티를 릴리스합니다. getPre (). setNext (null); 엔티티 = null; } 크기-; 반품; } entity = entity.getNext (); }}}} public String toString () {StringBuilder sb = new StringBuilder (); for (int i = 0; i <용량; i ++) {sb.append ( "index ="). Append (i) .append ( "["); 부울 hasentity = 거짓; 엔티티 <k, v> entity = 엔티티 [i]; if (entity! = null) {hasentity = true; } while (entity! = null) {SB.Append ( "["). Append (entity.getKey ()). Append ( "="). Append (entity.getValue ()). Append ( "]"). 부록 ( ","); entity = entity.getNext (); } if (hasentity) {sb.deletecharat (sb.length () -1); } sb.append ( "]/n"); } return sb.toString (); } / ** * 간단한 다시 해시 전략, 크기가 용량보다 큰 경우 액션을 다시 해시하십시오 * / private void reshash () {if (size> = caper) {int newCapacity = 용량 * 2; @SuppressWarnings ( "확인되지 않은") 엔티티 <k, v> [] newentities = 새로운 엔티티 [NewCapacity]; for (int i = 0; i <용량; i ++) {entity <k, v> entity = 엔티티 [i]; while (entity! = null) {put (Entity, 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 ( "2", "2"); map.put ( "3", "3"); map.put ( "4", "4"); map.put ( "5", "5"); map.put ( "6", "6"); map.put ( "7", "7"); map.put ( "8", "8"); map.put ( "Nine", "9"); map.put ( "10", "10"); System.out.println (map.get ( "One")); System.out.println (map.get ( "2")); System.out.println (map.get ( "Three")); System.out.println (map.get ( "4")); System.out.println (map.get ( "5")); System.out.println (map.get ( "Six")); System.out.println (map.get ( "7")); System.out.println (map.get ( "8")); System.out.println (map.get ( "Nine")); System.out.println (map.get ( "10")); System.out.println (map.toString ()); map.remove ( "Nine"); map.remove ( "Three"); System.out.println (map.get ( "One")); System.out.println (map.get ( "2")); System.out.println (map.get ( "Three")); System.out.println (map.get ( "4")); System.out.println (map.get ( "5")); System.out.println (map.get ( "Six")); System.out.println (map.get ( "7")); System.out.println (map.get ( "8")); System.out.println (map.get ( "Nine")); System.out.println (map.get ( "10")); System.out.println (map.toString ()); }} 클래스 엔티티 <k, v> {private k 키; 개인 V 값; 민간 기업 <k, v> pre; 민간 기업 <k, v> 다음; 공공 기관 (k key, v value) {this.key = 키; this.value = value; } public k getkey () {리턴 키; } public void setkey (k key) {this.key = 키; } public v getValue () {return 값; } public void setValue (v value) {this.value = value; } 공공 기관 <k, v> getpre () {return pre; } public void setpre (엔티티 <k, v> pre) {this.pre = pre; } 공공 기관 <k, v> getNext () {return next; } public void setNext (엔티티 <k, v> next) {this.next = next; }}읽어 주셔서 감사합니다. 도움이되기를 바랍니다. 이 사이트를 지원 해주셔서 감사합니다!