ArrayList和LinkedList的简单实现

 一  Collection是顶级接口 ,List(有序可重复)和Set(无序不可重复)也是接口    剩下的是实现类


还是别人总结的比较好https://blog.csdn.net/a23006239/article/details/47442867

  

二  简单实现ArrayList和LinkedList

public class MyArrayList {
	private Object[] eleData;
	private int size;
	public MyArrayList(){
		this(10);//调用构造方法this(参数1,参数2)
	}
	public MyArrayList(int initialCapacity) {
		if(initialCapacity < 0){
			try {
				throw new Exception();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		eleData =new Object[initialCapacity];
	}
	/**
	 * 
	 * @param args
	 */
	
	public void add(Object o){
		//数组扩容
		if(size==eleData.length){
			Object[] newArr=new Object[size*2+1];
			System.arraycopy(eleData, 0, newArr, 0, eleData.length);
			eleData = newArr;		
		}
		eleData[size++] = o;
	}
	
	public int size(){
		return size;
	}
	public boolean isEmpty(){
		return size == 0;
	}
	public void remove(int index){
		rangeCheck(index);
		int numMoved=size-index-1;
		if(numMoved > 0){
			System.arraycopy(eleData, index+1,eleData , index, numMoved);
		}
		eleData[--size]=null;
		
	}
	
	public void remove(Object o){
		for(int i = 0; i < size; i++){
			if(get(i).equals(o)){
				remove(i);
			}
		}
	}
	
	
	public  Object get(int i) {
		rangeCheck(i);
		return eleData[i];
	}
	public void rangeCheck(int index) {
		if(index < 0 || index >= size){
			try {
				throw new Exception();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	public static void main(String[] args) {
		MyArrayList list=new MyArrayList();
		list.add("111");
		list.add("112");
		list.add("113");
		list.add("114");
		list.add("115");
		list.add("116");
		list.remove("111");
		for(int i=0;i<list.size();i++){
			System.out.println(list.get(i));
		}
	}
	
}

(¥)LinkedList实现是一个双向链表

public class Node {
	
	Node pre;
	Node next;
	Object obj;
	public Node(){
		
	}
	public Node(Node pre, Object obj, Node next){
		super();//super调用父类的方法 父类是Object
		this.pre=pre;
		this.obj=obj;
		this.next=next;
	}
	
	public Node getPre() {
		return pre;
	}
	public void setPre(Node pre) {
		this.pre = pre;
	}
	public Node getNext() {
		return next;
	}
	public void setNext(Node next) {
		this.next = next;
	}
	public Object getObj() {
		return obj;
	}
	public void setObj(Object obj) {
		this.obj = obj;
	}
}
public class MyLinkedList {
	private Node first;
	private Node last;
	private int size;
	public void add(Object obj){
		Node n=new Node();
		if(first == null){
			n.setNext(null);
			n.setObj(obj);
			n.setPre(null);
			first=n;    //添加的第一个是空的,将 first last=n
			last=n;  
			
		}else{//直接往last节点后面增加新的节点
			n.setPre(last);
			n.setNext(null);
			n.setObj(obj);
			
			last.setNext(n);
			last=n;
		}
		size++;
	}
	
	public int size(){
		return size;
	}
	
	private void rangeCheck(int index){
		if(index<0 || index >=size){
			try {
				throw new Exception();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	public Object get(int index){
		rangeCheck(index);
		Node temp=node(index);
		if(temp != null){
			return temp.obj;
		}
		return null;
	}

	public Node node(int index) {
		Node temp = null;
		if(first !=null){
			if(index < (size>>1)){
				temp=first;
				for(int i=0; i<index;i++){
					temp=temp.next;
				}
			}else{
				temp=last;
				for(int i=size-1; i > index; i--){
					temp=temp.pre;
				}
			}
		}
		return temp;
	}
	
	public void remove(int index){
		Node temp=node(index);
		if(temp!=null){
			temp.pre.next =temp.next;
			temp.next.pre=temp.pre;
			size--;
		}
	}
	
	
	public static void main(String[] args) {
		MyLinkedList list=new MyLinkedList();
		list.add("aaa");
		list.add("aab");
		list.add("aac");
		list.remove(1);
		for(int i=0;i<list.size();i++){
			System.out.println(list.get(i));
		}
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值