实现ArrayList和HashMap类

本文介绍了如何从零开始实现自定义的ArrayList类MyArrayList及HashMap类MyHashMap,详细讲解了这两种数据结构的基本操作如添加、获取、删除等,并讨论了内部实现机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现ArrayList类,定义为MyArrayList.

package practice.collections.myArrayList;
/**
 * 实现ArrayList类
 *
 * @param <T>
 */
public class MyArrayList<T> {

	private int capacity = 10;
	
	private int size = 0;
	
	private T[] elementData = null;
	
	@SuppressWarnings("unchecked")
	public MyArrayList(){
		elementData = (T[])new Object[capacity];
	}
	
	//add()
	public void add(T t){
		if(null == t){
			throw new RuntimeException("要添加的值为空!");
		}
		if(size >= capacity){
			ensureCapacity();
		}
		elementData[size++] = t;
	}

	//remove()
	public void remove(int index){
		if(index < 0 || index >= size){
			throw new RuntimeException("索引超出范围!");
		}
		int length = size - index - 1;
                System.arraycopy(elementData, index+1, elementData, index, length);
		size--;
		elementData[size] = null;
	}
	
	//get()
	public T get(int index){
		if(index < 0 || index >= size){
			throw new RuntimeException("索引超出范围!");
		}
		return elementData[index];
	}
	
	public int size(){
		return this.size;
	}
	
	//扩容
	@SuppressWarnings("unchecked")
	private void ensureCapacity() {
		capacity = 2*capacity;
		T[] newElementData = (T[])new Object[capacity];
		System.arraycopy(elementData, 0, newElementData, 0, size);
		elementData = newElementData;
	}
	
}


实现HashMap类,类名为MyHashMap

先定义桶数组元素类型

package practice.collections.myHashMap;

public class Entry<K, V> {

	protected K key;
	
	protected V value;
	
	protected Entry<K, V> next;
	
	public Entry(K key,V value,Entry<K,V> next){
		this.key = key;
		this.value = value;
		this.next = next;
	}
	
}

再定义MyHashMap

package practice.collections.myHashMap;
/**
 * 实现HashMap类
 *
 */
public class MyHashMap<K,V> {

	protected int capacity = 16;
	protected int size = 0 ;
	Entry<K,V>[] table = null; 
	
	@SuppressWarnings("unchecked")
	public MyHashMap(){
		table = new Entry[capacity];
	}
	
	public void put(K key,V value){
		if(null == key){
			throw new RuntimeException("the key should not be null.");
		}
		int i = key.hashCode()%capacity;
		for(Entry<K, V> e = table[i];e != null;e = e.next){
			if(e.key.equals(key)){
				e.value = value;
				return;
			}
		}
		//若在链表中未找到key,则新建该key的节点,并将其作为新的table[i]
		table[i] = new Entry<K, V>(key, value, table[i]);
		size++;
	}
	
	public V get(K key){
		if(null == key){
			throw new RuntimeException("the key should not be null.");
		}
		int i = key.hashCode()%capacity;
		for(Entry<K, V> e = table[i];e != null;e = e.next){
			if(e.key.equals(key)){
				return e.value;
			}
		}
		return null;
	}
	
	public V remove(K key){
		if(null == key){
			throw new RuntimeException("the key should not be null.");
		}
		int i = key.hashCode()%capacity;
		Entry<K, V> pre = table[i];
		for(Entry<K, V> e = table[i];e != null;e = e.next){
			if(e.key.equals(key)){
				if(pre == e)
					table[i] = e.next;
				else
					pre.next = e.next;
				return e.value;
			}
			pre = e;  //注重保持一个前缀的方法
		}
		return null;
	}
	
}

以上MyHashMap未考虑扩容,感觉比较复杂



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值