3.1 集合
(1) 集合是一種彙集,組織了其餘對象的對象。它定義一張破那個特定的方式,能夠訪問、管理所包含的對象。
(2)集合的分類:1)按其元素是否按直線方式組織進行劃分,分爲線性集合(linear collection)和非線性集合(nonlinear collection);2)按保存類型劃分,分爲同構集合和異構集合。
(3)抽象數據類型:一個抽象數據類型(ADT)是由數據和在該數據上所實施的操做構成的集合,一個ADT有名稱、值域和一組容許執行的操做。
(4)Java集合類API:Java標準類庫中定義了幾種不一樣類型集合的類,稱爲Java集合類API。前端
3.2 棧集合
(1)棧(stack)是一種線性集合,其元素添加和刪除都在同一端進行。
(2)棧的元素是按照後進先出(LIFO)(即 Last in ,first out)的方法進行處理的,最後進入棧中的元素最早被移出。
(3) 棧的一些操做:java
操做 | 描述 |
---|---|
push | 把項壓入堆棧頂部 |
pop | 移除堆棧頂部的對象,並做爲此函數的值返回該對象 |
peek | 查看堆棧頂部的對象,但不從堆棧中移除它 |
isempty | 測試堆棧是否爲空 |
size | 肯定棧的元素數目 |
search | 返回對象在堆棧中的位置,以 1 爲基數 |
3.3 主要的面向對象的概念
(1)繼承:繼承的過程是在兩個類之間創建一種「是」的關係,即子類是一種更具體的父類版本。
(2)由繼承實現多態性:當用類名聲明一個引用變量時,這個變量能夠指向該類的任何一個對象,同時,它也能引用經過繼承與它所聲明的類型有關的任何類的對象。
(3)泛型(generic type):一個集合所管理的對象的類型要在實例化該集合對象時才肯定。泛型保證了集合中對象類型的兼容性。android
3.4 異常
(1)異常(exception)就是一種對象,它定義了一種非正常或錯誤的狀況。異常有程序或運行時環境拋出,能夠按預期的被捕獲或被正確處理。
(2)錯誤(error)與異常類似,不過異常表示一種沒法恢復的狀況,且沒必要去捕獲它。
(3)Java有一個預約義的異常和錯誤集,當程序運行時他們可能會發生。git
public interface Stack<T> { // Adds the specified element to the top of the stack. public void push (T element); // Removes and returns the top element from the stack. public T pop(); // Returns a reference to the top element of this stack without removing it. public T peek(); // Returns true if this stack contains no elements and false otherwise. public boolean isEmpty(); // Returns the number of elements in the stack. public int size(); // Returns a string representation of the stack. public String toString(); }
3.6 ArrayStack類數組
package jsjf; import jsjf.exceptions.*; import java.util.Arrays; public class ArrayStack<T> implements StackADT<T> { private final static int DEFAULT_CAPACITY = 100; private int top; private T[] stack;
public ArrayStack() { this(DEFAULT_CAPACITY); } public ArrayStack(int initialCapacity) { top = 0; stack = (T[])(new Object[initialCapacity]); }
public void push(T element) { if (size() == stack.length) expandCapacity(); stack[top] = element; top++; } private void expandCapacity() { stack = Arrays.copyOf(stack, stack.length * 2); } @Override public T pop() { if (isEmpty()) throw new EmptyCollectionException("stack"); top--; T result = stack[top]; stack[top] = null; return result; } @Override public T peek() { if (isEmpty()) throw new EmptyCollectionException("stack"); return stack[top-1]; } @Override public boolean isEmpty() { if (top==0) return true; else return false; } @Override public int size() { return top; }
1.鏈式結構是一種數據結構,它使用對象引用變量來建立對象之間的連接。安全
2.使用下面的這個類邊能夠建立一個鏈式結構,一個Person對象含有指向兩個Person對象的連接,如此類推,這種類型的對象由稱爲自引用的。數據結構
public class Person { private String name; private String address; private Person next; }
3.全部動態建立的對象都來自於一個名爲系統堆(system heap)或自由存儲(free store)的內存區。併發
4.管理鏈表包括如下幾個方面:
(1)訪問元素
要訪問其元素,咱們必須先訪問第一個元素,而後順着下一個指針從一個元素到另外一個元素。同時注意必需要維護指向鏈表的第一個指針。
(2)插入節點
首先,新添加節點的next引用被設置爲指向鏈表的當前首結點,接着,指向鏈表前端的引用從新設置爲指向這個新添加的節點。
(3)刪除節點
要刪除鏈表的首結點,須要重置指向鏈表前端的引用,使其指向鏈表當前的次結點。
app
5.無連接的元素
(1)定義一個單獨的結點類,將全部元素連接在一塊兒
(2)雙向鏈表
6.LinkedList類
//-------------------------------------------------------------------- // Creates an empty stack using the default capacity. //-------------------------------------------------------------------- public LinkedStack() { count = 0; top = null; }
//刪除並返回棧頂元素 public void push(T element) { LinearNode<T> eNode = new LinearNode<>(element); //新元素入棧對應新的對象 eNode.setNext(top); //新結點next引用指向棧頂 top = eNode; count++; } //返回當前棧頂所保存元素的引用 public T peek() throws EmptyStackException { return top.getElement(); } public boolean isEmpty() { return count == 0; } public int size() { return count; }
問題1:關於泛型的理解,泛型上學期涉及但未使用,此次看書上泛型代碼就看不明白,「T」究竟是在指什麼?
問題1解決方案:因而我把書上講解泛型的部分從新看了一遍,又結合某公衆號講解總結了如下內容。
泛型的本質是爲了參數化類型(在不建立新的類型的狀況下,經過泛型指定的不一樣類型來控制形參具體限制的類型)。也就是說在泛型使用過程當中,操做的數據類型被指定爲一個參數,這種參數類型能夠用在類、接口和方法中,分別被稱爲泛型類、泛型接口、泛型方法。
public class Box { private String object; public void set(String object) { this.object = object; } public String get() { return object; } }
這是最多見的作法,這樣作的一個壞處是Box裏面如今只能裝入String類型的元素,若是咱們須要裝入Int等其餘類型的元素,還必需要另外重寫一個Box,代碼得不到複用,使用泛型能夠很好的解決這個問題。
public class Box<T> { // T stands for "Type" private T t; public void set(T t) { this.t = t; } public T get() { return t; } }
這樣咱們的Box類即可以獲得複用,咱們能夠將T替換成任何咱們想要的類型:
Box<Integer> integerBox = new Box<Integer>(); Box<Double> doubleBox = new Box<Double>(); Box<String> stringBox = new Box<String>();這樣一來,T的含義就很明晰了,T它是能夠代指咱們所須要的任何類型,用起來也會很方便。
問題2:java.util.Stack的實現有哪些優勢,又存在哪些潛在的問題?
public class Stack<E> extends Vector<E> { /** * Creates an empty Stack. */ public Stack() { } /** * Pushes an item onto the top of this stack. This has exactly * the same effect as: * <blockquote><pre> * addElement(item)</pre></blockquote> * * @param item the item to be pushed onto this stack. * @return the <code>item</code> argument. * @see java.util.Vector#addElement */ public E push(E item) { addElement(item); return item; } /** * Removes the object at the top of this stack and returns that * object as the value of this function. * * @return The object at the top of this stack (the last item * of the <tt>Vector</tt> object). * @exception EmptyStackException if this stack is empty. */ public synchronized E pop() { E obj; int len = size(); obj = peek(); removeElementAt(len - 1); return obj; } /** * Looks at the object at the top of this stack without removing it * from the stack. * * @return the object at the top of this stack (the last item * of the <tt>Vector</tt> object). * @exception EmptyStackException if this stack is empty. */ public synchronized E peek() { int len = size(); if (len == 0) throw new EmptyStackException(); return elementAt(len - 1); } /** * Tests if this stack is empty. * * @return <code>true</code> if and only if this stack contains * no items; <code>false</code> otherwise. */ public boolean empty() { return size() == 0; } /** * Returns the 1-based position where an object is on this stack. * If the object <tt>o</tt> occurs as an item in this stack, this * method returns the distance from the top of the stack of the * occurrence nearest the top of the stack; the topmost item on the * stack is considered to be at distance <tt>1</tt>. The <tt>equals</tt> * method is used to compare <tt>o</tt> to the * items in this stack. * * @param o the desired object. * @return the 1-based position from the top of the stack where * the object is located; the return value <code>-1</code> * indicates that the object is not on the stack. */ public synchronized int search(Object o) { int i = lastIndexOf(o); if (i >= 0) { return size() - i; } return -1; } /** use serialVersionUID from JDK 1.0.2 for interoperability */ private static final long serialVersionUID = 1224463164541339165L; }
1.經過源碼發現,Vector類在初始化的時候,會構造一個大小爲10是空間
2.Stack中的pop、peek、search爲線程安全類型
3.時間複雜度:
索引: O(n)
搜索: O(n)
插入: O(1)
移除: O(1)
經過peek()方法註釋The object at the top of this stack (the last item of the Vector object,能夠發現數組(Vector)的最後一位即爲Stack的棧頂
pop、peek以及search方法自己進行了同步
push方法調用了父類的addElement方法
empty方法調用了父類的size方法
由此咱們能夠了解到Stack類,它經過五個操做對類Vector 進行了擴展 ,容許將向量視爲堆棧。它提供了一般的push 和 pop 操做,以及取堆棧頂點的peek 方法、測試堆棧是否爲空的 empty 方法、在堆棧中查找項並肯定到堆棧頂距離的search 方法。因爲Vector是經過數組實現的,這就意味着,Stack也是經過數組實現的。它可使用一個索引來跟蹤棧中元素的位置。同時,因爲它繼承自Vector類,它繼承了不少與棧的基本假設相互衝突的操做。
- 關於synchronized解釋
Java語言的關鍵字,當它用來修飾一個方法或者一個代碼塊的時候,可以保證在同一時刻最多隻有一個線程執行該段代碼。
1、當兩個併發線程訪問同一個對象object中的這個synchronized(this)同步代碼塊時,一個時間內只能有一個線程獲得執行。另外一個線程必須等待當前線程執行完這個代碼塊之後才能執行該代碼塊。
2、然而,當一個線程訪問object的一個synchronized(this)同步代碼塊時,另外一個線程仍然能夠訪問該object中的非synchronized(this)同步代碼塊。
3、尤爲關鍵的是,當一個線程訪問object的一個synchronized(this)同步代碼塊時,其餘線程對object中全部其它synchronized(this)同步代碼塊的訪問將被阻塞。
4、第三個例子一樣適用其它同步代碼塊。也就是說,當一個線程訪問object的一個synchronized(this)同步代碼塊時,它就得到了這個object的對象鎖。結果,其它線程對該object對象全部同步代碼部分的訪問都被暫時阻塞。
5、以上規則對其它對象鎖一樣適用.
問題1:關於Android Studio中文本顯示換行問題,如圖
問題1解決方案:參考上學期所作app,將layout文件夾下xml文件中text中代碼作了這些修改,將android:singleLine="true"
中true改成false,同時添加一行代碼android:inputType="textMultiLine"
,修改完以後程序運行如圖:
問題2:關於書上intValue方法變紅問題:
intValue方法,這是第一次接觸這個方法。
intValue()
如Integer類型,就回有intValue()方法意識是說,把Integer類型轉化爲Int類型。其餘相似,都是一個意思
valueOf()
如String就有valueOf()方法,意識是說,要把參數中給的值,轉化爲String類型,Integer的valueOf()就是把參數給的值,轉化爲Integer類型。其餘相似,都是一個意思。
上學期結束時爲7683行,如今爲8255行,本週共572行
1.Software systems need only to work to support the work of developers, maintainers, and users.【×】
解析:軟件系統不只僅只須要支持開發人員、維護人員和用戶的工做。
2.Which Growth function has the highest order?
A .O(n log n)
B .O(n2)
C .O(2n)
D .O(log n)
解析:這道題目是問哪一個增加函數具備最高的階次,咱們知道一個結論c(常量)<㏒₂n < n < n㏒₂n < n²< n³ < 2ⁿ < 3ⁿ< n!
,因此題目中的四個函數顯然是2ⁿ的階次是最高的,這裏出錯是由於當時把2ⁿ當作了2×n,誤選了B。
博客中值得學習的或問題: 博客中代碼問題解決過程記錄較詳細,可適當添加教材內容總結。
結對學習內容:第三章內容:集合概述——棧,第四章內容:鏈式結構——棧
代碼行數(新增/累積) | 博客量(新增/累積) | 學習時間(新增/累積) | 重要成長 | |
---|---|---|---|---|
目標 | 5000行 | 30篇 | 400小時 | |
第一週 | 0/0 | 1/1 | 15/15 | |
第二週 | 572/572 | 1/2 | 16/31 |