201521123088《Java程式設計》第七周學習總結

來源:互聯網
上載者:User

標籤:ash   integer   queue   lin   keyword   top   equal   指定元素   rar   

1. 本周學習總結

以你喜歡的方式(思維導圖或其他)歸納總結集合相關內容。

2. 書面作業

ArrayList程式碼分析
1.1 解釋ArrayList的contains原始碼
原始碼:

//contains()方法public boolean contains(Object o) {    return indexOf(o) >= 0;}//indexOf()方法public int indexOf(Object o) {    if (o == null) {        for (int i = 0; i < size; i++)            if (elementData[i]==null)                return i;    } else {        for (int i = 0; i < size; i++)            if (o.equals(elementData[i]))                return i;    }    return -1;}
答:在indexOf()方法中對傳進來的值o判斷是否為null,因為值o若為null,則不可以使用equals進行比較。如果找到了值o,則返回對應的數組下標;如果滿意找到,則返回-1,那麼contains()方法就會返回false。

1.2 解釋E remove(int index)原始碼

//remove代碼public E remove(int index) {    rangeCheck(index);    modCount++;    E oldValue = elementData(index);    int numMoved = size - index - 1;    if (numMoved > 0)        System.arraycopy(elementData, index+1, elementData, index,                         numMoved);    elementData[--size] = null; // clear to let GC do its work    return oldValue;}//rangeCheck代碼private void rangeCheck(int index) {    if (index >= size)        throw new IndexOutOfBoundsException(outOfBoundsMsg(index));}
答:首先在remove()方法中調用rangeCheck()方法,是用來判斷傳入的index是否超過size,若超過,則拋出IndexOutOfBoundsException異常。然後這個remove()方法就是將對應下標的元素取出,再將後面的元素全部往前移一位,將最後一位,即size-1的位置置null。

1.3 結合1.1與1.2,回答ArrayList儲存資料時需要考慮元素的類型嗎?

答:不需要。從1.1和1.2的原始碼中可以看到其傳入的對象是object,Object為一個頂級父類,它是任何類父類,所以說ArrayList儲存資料時不用考慮元素類型。

1.4 分析add原始碼,回答當內部數組容量不夠時,怎麼辦?
原始碼:

//add代碼public boolean add(E e) {    ensureCapacityInternal(size + 1);     elementData[size++] = e;    return true;//ensureCapacityInternal代碼private void ensureCapacityInternal(int minCapacity) {    if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {        minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);    }    ensureExplicitCapacity(minCapacity);}    modCount++;    // overflow-conscious code    if (minCapacity - elementData.length > 0)         grow(minCapacity);}   }//grow代碼 private void grow(int minCapacity) {    // overflow-conscious code    int oldCapacity = elementData.length;    int newCapacity = oldCapacity + (oldCapacity >> 1);     if (newCapacity - minCapacity < 0)        newCapacity = minCapacity;    if (newCapacity - MAX_ARRAY_SIZE > 0)        newCapacity = hugeCapacity(minCapacity);    // minCapacity is usually close to size, so this is a win:    elementData = Arrays.copyOf(elementData, newCapacity);}   
答:當內部數組容量不夠時,首先ensureCapacityInternal方法得到最小擴容量,其次ensureExplicitCapacity判斷是否要擴容。

1.5 分析private void rangeCheck(int index)原始碼,為什麼該方法應該聲明為private而不聲明為public?

private void rangeCheck(int index) {    if (index >= size)        throw new IndexOutOfBoundsException(outOfBoundsMsg(index));}  
答:因為rangeCheck()方法只需要判斷數組是否越界,沒有其他的作用,外部不需要對它進行修改或者訪問,所以說聲明應為private。

HashSet原理
2.1 將元素加入HashSet(散列集)中,其儲存位置如何確定?需要調用那些方法?

答:計算出帶儲存的散列碼作為儲存的位置依據,將對象存入Hash類集合

  要調用equals和hashCode方法

 

ArrayListIntegerStack
題集jmu-Java-05-集合之5-1 ArrayListIntegerStack
3.1 比較自己寫的ArrayListIntegerStack與自己在題集jmu-Java-04-物件導向2-進階-多態、介面與內部類中的題目5-3自訂介面ArrayIntegerStack,有什麼不同?(不要出現大段代碼)

 

private ArrayList<Integer> list;    public ArrayListIntegerStack(){        list=new ArrayList<Integer>();//使用了Arraylist    } public Integer push(Integer item) {        // TODO Auto-generated method stub        if(item==null)        return null;        list.add(item);//用到了add方法        return item;    }public Integer pop() {        // TODO Auto-generated method stub        if(list.size()==0)        return null;         return list.remove(list.size()-1);//用到remove方法             }



 
04-5-3中的ArrayListIntegerStack

private
Integer[]arr;//使用數組
private int top=0;//top指標 public ArrayIntegerStack(int capacity) { arr=new Integer[capacity]; } public Integer pop(){ if(top==0) return null; top--;//出棧需要移動指標 return arr[top]; }

 

答:在5-1中編寫的ArrayListIntegerStack使用的是ArrayList數組來存放對象,因此相應的push、pop、peek等方法的編寫用到了list中的add、remove等方法;5-3中則是用數組來存放對象,並且要有top指標。

3.2 簡單描述介面的好處.

答:根本區別就是一個內部使用數組實現,一個內部使用ArrayList實現,數組的話增刪查改都要自己編寫方法,ArrayList可以調用已有方法進行操作。

Stack and Queue
4.1 編寫函數判斷一個給定字串是否是迴文,一定要使用棧,但不能使用java的Stack類(具體原因自己搜尋)。請粘貼你的代碼,類名為Main你的學號。

public class Main201521123088 {    public static void main(String[] args) {        // TODO Auto-generated method stub    Scanner sc=new Scanner(System.in);    String x=sc.next();    ArrayListStringStack list=new ArrayListStringStack();    ArrayListStringStack list1=new ArrayListStringStack();    char [] arr = x.toCharArray();    for(int i=0;i<arr.length;i++){        list.push(String.valueOf(arr[i]));    }    int q=arr.length/2;    int k=arr.length%2;    if(k==0){        for(int y=1;y<=q;y++){            list1.push(list.pop());        }         for(int b=0;!list.empty()&&!list1.empty();b++){                if(!list.pop().equals(list1.pop())){                    System.out.println("該字串不是迴文");                    break;                }                if(list.empty()&&list1.empty()){                    System.out.println("該字串是迴文");                }            }    }    else{                for(int p=1;p<=q;p++){            list1.push(list.pop());                    }          list.pop();         for(int b=0;!list.empty()&&!list1.empty();b++){                                if(!(list.pop().equals(list1.pop()))){                    System.out.println("該字串不是迴文");                    break;                }                if(list.empty()&&list1.empty()){                    System.out.println("該字串是迴文");                    break;                }            }     }       }        }

運行結果:


4.2 題集jmu-Java-05-集合之5-6 銀行業務隊列簡單類比。(不要出現大段代碼)

for (int i = 0; i < n; i++) {            int number = in.nextInt();            if (number % 2 == 0) B.add(number);            else A.add(number);        }while(!A.isEmpty() || !B.isEmpty())        {            Integer a1 = A.poll();            if(a1 != null){                if(B.isEmpty() && A.isEmpty())                     System.out.print(a1);                else                    System.out.print(a1 + " ");             }                        Integer a2 = A.poll();            if(a2 != null){                if(B.isEmpty() && A.isEmpty())                    System.out.print(a2);                else                     System.out.print(a2 + " ");             }            Integer b = B.poll();            if(b != null){                if(B.isEmpty() && A.isEmpty())                    System.out.print(b);                else                     System.out.print(b + " ");            }                    }

題集jmu-Java-05-集合之5-2 統計文字中的單詞數量並按單詞的字母順序排序後輸出 (不要出現大段代碼)
5.1 實驗總結

答:用TreeSet的add方法將指定的元素添加到此 set(如果該元素尚未存在於 set 中)。更確切地講,如果該 set 不包含滿足 (e==null ? e2==null : e.equals(e2)) 的元素 e2,則將指定元素 e 添加到此 set 中。如果此 set 已經包含這樣的元素,則該調用不改變此 set 並返回 false。且就會預設幫我們排好序,最後按要求輸出一下即可。

物件導向設計大作業-改進
7.1 完善圖形介面(說明與上次作業相比增加與修改了些什麼)

7.2 使用集合類改進大作業

3. 碼雲上代碼提交記錄及PTA實驗總結

題目集:jmu-Java-05-集合

3.1. 碼雲代碼提交記錄

在碼雲的項目中,依次選擇“統計-Commits曆史-設定時間段”, 然後搜尋並

201521123088《Java程式設計》第七周學習總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.