java學習漫筆- 搗蛋vector

來源:互聯網
上載者:User
java學習隨筆--- 搗蛋vector

最近比較有時間啦,有時間搞下java,個人覺得學這門語言文法太多啦,不一一去學習啦,心血來潮,掛了個struct2的原始碼,一入深似海啊,看得我天花繚亂,從最簡單的開始吧

 1 public static void main(String[] args) { 2          3         Vector v = new Vector(4); 4  5         //向Vector中添加元素 靜態數組+動態擴充 6         //使用add方法直接添加元素  7         v.add("Test0");  8         v.add("Test1");  9         v.add("Test0"); 10         v.add("Test2"); 11         v.add("Test2");12 13         //從Vector中刪除元素 14         v.remove("Test0"); //刪除指定內容的元素 15         v.remove(0); //按照索引號刪除元素16 17         //獲得Vector中已有元素的個數 18         int size = v.size(); 19         System.out.println("size:" + size);20 21         //遍曆Vector中的元素 22         for(int i = 0;i < v.size();i++){ 23         System.out.println(v.get(i)); 24         } 25 }

代碼很簡單啦,學過資料結構的都知道,簡單的新增改查啦,不過我們要深入一下瞭解,這玩意跟數組有什麼區別

建構函式如下,意思是說你可以初始化一個容量的數,多少你自己決定

 1  /** 2      * Constructs an empty vector with the specified initial capacity and 3      * with its capacity increment equal to zero. 4      * 5      * @param   initialCapacity   the initial capacity of the vector 6      * @throws IllegalArgumentException if the specified initial capacity 7      *         is negative 8      */ 9     public Vector(int initialCapacity) {10     this(initialCapacity, 0);11     }

我們接著來看,java的建構函式可真的比php強大,支援不同參數調用,換php的話早就報錯啦

 1     /** 2      * Constructs an empty vector with the specified initial capacity and 3      * capacity increment. 4      * 5      * @param   initialCapacity     the initial capacity of the vector 6      * @param   capacityIncrement   the amount by which the capacity is 7      *                              increased when the vector overflows 8      * @throws IllegalArgumentException if the specified initial capacity 9      *         is negative10      */11     public Vector(int initialCapacity, int capacityIncrement) {12     super();13         if (initialCapacity < 0)14             throw new IllegalArgumentException("Illegal Capacity: "+15                                                initialCapacity);16     this.elementData = new Object[initialCapacity];17     this.capacityIncrement = capacityIncrement;18     }

代碼是不是很簡單,簡單的初始化一個對象數組,連我一個高中生的看出來啦,注意到第二個參數,這個是控制數組填滿了之後要怎麼增加,可以理解為一個策略吧

我們來看看添加元素是怎樣實現的

 1   /** 2      * Appends the specified element to the end of this Vector. 3      * 4      * @param e element to be appended to this Vector 5      * @return {@code true} (as specified by {@link Collection#add}) 6      * @since 1.2 7      */ 8     public synchronized boolean add(E e) { 9     modCount++;10     ensureCapacityHelper(elementCount + 1);11     elementData[elementCount++] = e;12         return true;13     }

synchronized 這玩意就是多安全執行緒的時候用的,防止多個線程同事操作

關鍵是 ensureCapacityHelper 這個函數

 1 /** 2      * This implements the unsynchronized semantics of ensureCapacity. 3      * Synchronized methods in this class can internally call this 4      * method for ensuring capacity without incurring the cost of an 5      * extra synchronization. 6      * 7      * @see #ensureCapacity(int) 8      */ 9     private void ensureCapacityHelper(int minCapacity) {10     int oldCapacity = elementData.length;11     if (minCapacity > oldCapacity) {12         Object[] oldData = elementData;13         int newCapacity = (capacityIncrement > 0) ?14         (oldCapacity + capacityIncrement) : (oldCapacity * 2);15             if (newCapacity < minCapacity) {16         newCapacity = minCapacity;17         }18             elementData = Arrays.copyOf(elementData, newCapacity);19     }20     }


可以這麼理解吧,上面這段代碼就是看看數組滿了沒有,如果滿了就動態增加,還記得我們上面說的那個參數嗎,就是可以理解為擴充因子,如果沒有定義的話就double增加,就是這麼簡單,貌似跟c語言的動態數組好像啊

總結一下

上面我們學到的知識點

1. synchronized  同步用的,相當於一個鎖吧

2. Arrays.copyOf 這函數是從一個數組複製到一個新數組裡面,新數組容量可以自己定義

3. java 的建構函式可以支援多個,前提你每個建構函式的參數都不同

4. vector 這東西跟數組沒什麼區別,只不過它比靜態數組可以自動擴充罷了
今天就到這裡吧




  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.