ArrayList類
和使用鏈表實現的LinkedList類
,它們均可以存儲由泛型參數E定義的元素,同時也都實現了List接口。add操做:不管是有序列表的add操做仍是無序列表的addToFtont和addAfter操做,都與remove操做相似,須要進行屢次比較和平移操做,因此其時間複雜度爲O(n)。addToRear操做與棧的push操做相似,時間複雜度爲O(1)。html
在隊列中使用環形數組時,能夠把dequeue的效率從O(n)變爲O(1),由於它不須要平移數組,可是在列表中由於咱們能夠在任何位置隨意添加刪除元素,因此平移數組的操做沒法省略,所以是否使用環形數組就沒有那麼重要了。前端
instanceof
是什麼?怎麼用?public boolean equals(Object other) { boolean result = false; if (other instanceof Course) { Course otherCourse = (Course) other; if (prefix.equals(otherCourse.getPrefix()) && number == otherCourse.getNumber()) { result = true; } } return result; }
boolean b1 = new String() instanceof String; //b1爲true boolean b2 = "Sting" instanceof Object; //b2爲true,由於String是Object的子類 boolean b3 = new Object() instanceof String; //b3爲false,Object是父類 boolean b4 = 'A' instanceof Character; //編譯不經過,‘A’在此處視爲基本數據類型char,instanceof操做符只能用做對象的判斷 boolean b5 = null instanceof String; //b5爲false,這是instanceof特有的規則:若左操做數爲null,結果就直接返回false,再也不運算右操做數是什麼類。 boolean b6 = (String)null instanceof String; //b6爲false,即便類型轉換仍是個 null boolean b7 = new Date() instanceof String; //編譯不經過,instanceof操做符的左右操做數必須有繼承或實現關係,不然編譯出錯
target
進行比較的元素上面,假如我設定的元素是temp
,它指的是用於遍歷鏈表的指針如今所指的節點的下一個節點,當它指向的是所要找的目標元素時,實際要插入的位置(即current指向的節點)是目標元素的前一個,因此最後會致使,addAfter將所插元素插到指定位置以前而不是以後。將與target
進行比較的元素改成current
便可。NullPointerException
Deque類
,當時它的add和remove都是在一個類中寫的,因此tail會發生改變。可是在列表中remove操做與add操做並不在一個類裏,因此LinkedList類
中的tail從開始設定爲null以後,若是直接引用的話理所固然要拋出異常。//原last方法 public T last() throws EmptyCollectionException { if (isEmpty()){ throw new EmptyCollectionException("LinkedList"); } return tail.getElement(); } //修改過的last方法 public T last() throws EmptyCollectionException { if (isEmpty()){ throw new EmptyCollectionException("LinkedList"); } LinearNode<T> temp = head; T result = temp.getElement(); while (temp.getNext() != tail){ temp = temp.getNext(); } tail = temp; return tail.getElement(); }
代碼行數(新增/累積) | 博客量(新增/累積) | 學習時間(新增/累積) | 重要成長 | |
---|---|---|---|---|
目標 | 5000行 | 30篇 | 400小時 | |
第一週 | 10/10 | 1/1 | 10/10 | |
第二週 | 246/366 | 2/3 | 20/30 | |
第三週 | 567/903 | 1/4 | 10/40 | |
第四周 | 2346/3249 | 2/6 | 20/60 |