2014-10-12 每日一记

今天

(1)看了clone方法

首先要使用object的clone方法首先要实现Cloneable接口才行,也就是说得覆盖clone方法,单纯地仅仅调用super.clone(如下)只能实现浅复制,也就是说只做基本数据类型的拷贝,而对象还是共享的。

1   public Object clone(){
2         Object obj = null;
3         try{
4             obj = super.clone();
5         }catch(CloneNotSupportedException ex){
6             ex.printStackTrace();
7         }
8         return obj;
9     }

要实现深复制,需要在clone函数中处理对象变量

//如 DeepCopyClass类中的对象为Date对象
 public Object clone(){
        DeepCopyClass obj = null;
        try{
            obj = (DeepCopyClass)super.clone();
        }catch(CloneNotSupportedException e){
            e.printStackTrace();
        }
        obj.setObject((Date)this.getObject().clone());
        return obj;
    }

(2)动手写了写linkedlist类

  1 package mylinkedlist;
  2 
  3 import java.util.ConcurrentModificationException;
  4 import java.util.Iterator;
  5 import java.util.NoSuchElementException;
  6 
  7 public class MyLinkedList<E> implements Iterable<E>{
  8     private int size;
  9     private int modCount;
 10     private Node<E> header;
 11     private Node<E> tail;
 12     
 13     private static class Node<E>{
 14         public E data;
 15         public Node<E> previous;
 16         public Node<E> next;
 17         
 18         public Node(E d, Node<E> p,Node<E> n){
 19             this.next = n;
 20             this.previous=p;
 21             this.data=d;    
 22         }
 23     }
 24     public MyLinkedList(){
 25         size=0;
 26         header = new Node<E>(null,null,null);
 27         tail =new Node<E>(null,header,null);
 28         header.next=tail;
 29         modCount++;//这里也要使modCount+1
 30     }
 31     public int size(){
 32         return size;
 33     }
 34     public boolean isEmpty(){
 35         return size==0;
 36     }
 37     public E get(int index){
 38         return getNode(index).data;
 39     }
 40     public E set(int index, E ele){
 41         Node<E> temp = getNode(index);
 42         E old = temp.data;
 43         temp.data = ele;
 44         return old;
 45     }
 46  
 47     public void add(E element){
 48         add(size(),element);
 49     }
 50     public void add(int index, E element){
 51         Node<E> temp=getNode(index-1);
 52         Node<E> newEle= new Node<E>(element,temp,temp.next);
 53         temp.next=newEle;
 54         temp.next.previous=newEle;
 55         size++;
 56         modCount++;
 57     }
 58     //remove的方法这样写完全是为了让迭代器也能使用remove的方法,因为迭代器获取的不是要删除元素的具体位置而是要删除元素本身
 59     private E remove(Node<E> temp){
 60         temp.previous.next = temp.next;
 61         temp.next.previous=temp.previous;
 62         size--;
 63         modCount++;
 64         return  temp.data;
 65     }
 66     public E remove(int index){
 67         return remove(getNode(index));
 68     }
 69     
 70     
 71     private Node<E> getNode(int index){
 72         if(index<0|index>=size())
 73             throw new IndexOutOfBoundsException();
 74         Node<E> temp;
 75         if(size()/2>index){
 76             temp=header.next;
 77             for(int i=0;i<index;i++){
 78                 temp=temp.next;
 79             }
 80         }else{
 81             temp=tail.previous;
 82             for(int i=size();i>index;i--){
 83                 temp=temp.previous;
 84             }
 85         }
 86         return temp;
 87     }
 88     @Override
 89     public java.util.Iterator<E> iterator() {//你这个实现了Iterable的接口就要实现这个方法
 90         return new LinkedListIterator();
 91     }
 92     
 93     private class LinkedListIterator implements Iterator<E>{
 94         private int expectedModCount=modCount;
 95         private Node<E> current=header.next;
 96         private boolean okToMove = false;
 97         @Override
 98         public boolean hasNext() {
 99             // TODO Auto-generated method stub
100             return current!=tail;
101         }
102 
103         @Override
104         public E next() {
105             // TODO Auto-generated method stub
106             if(!hasNext())
107                 throw new NoSuchElementException();
108             if(expectedModCount!=modCount)
109                 throw new  ConcurrentModificationException();
110             E temp = current.data;
111             current=current.next;
112             okToMove=true;
113             return temp;
114         }
115 
116         @Override
117         public void remove() {
118             // TODO Auto-generated method stub
119             if(expectedModCount!=modCount)
120                 throw new  ConcurrentModificationException();
121             if(!okToMove)
122                 throw new IllegalStateException();
123             MyLinkedList.this.remove(current.previous);
124             okToMove=false;
125             expectedModCount++;//调用了外部类的remove会在那里面将modCount++;
126             
127         }
128         
129     }
130 
131 }
原文地址:https://www.cnblogs.com/ivywenyuan/p/4020895.html