201521123097 《JAVA程序设计》第七周学习总结

1. 本周学习总结

总结

2. 书面作业

1.ArrayList代码分析

1.1 解释ArrayList的contains源代码

源代码:

public boolean contains(Object o) {
    return indexOf(o) >= 0;
}


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;
}

当o为null时,找到后就返回;当o不为null时,若equals则返回下标;找不到时返回-1。
contains主要是通过indexof的返回值来确认有没有包含某个对象。

1.2 解释E remove(int index)源代码
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;
}


private void rangeCheck(int index) {
    if (index >= size)
        throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}

rangeCheck方法中首先判断index是否超出size,若超出则抛出IndexOutOfBoundsException。

1.3 结合1.1与1.2,回答ArrayList存储数据时需要考虑元素的类型吗?

不需要,因为Object类是所有类的父类,所以所有对象都为Object。

1.4 分析add源代码,回答当内部数组容量不够时,怎么办?

public boolean add(E e) {
    ensureCapacityInternal(size + 1);
    elementData[size++] = e;
    return true;
}

private void ensureCapacityInternal(int minCapacity) {
    if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
        minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
    }

    ensureExplicitCapacity(minCapacity);
}

 
private void ensureExplicitCapacity(int minCapacity) {
modCount++;

    // overflow-conscious code
    if (minCapacity - elementData.length > 0)
        grow(minCapacity);
}

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);
}

private static int hugeCapacity(int minCapacity) {
    if (minCapacity < 0) // overflow
        throw new OutOfMemoryError();
    return (minCapacity > MAX_ARRAY_SIZE) ?
        Integer.MAX_VALUE :
        MAX_ARRAY_SIZE;
}

“int newCapacity = oldCapacity + (oldCapacity >> 1)”新容量扩大到原容量的1.5倍,右移一位相当于原数值除以2。

1.5 分析private void rangeCheck(int index)源代码,为什么该方法应该声明为private而不声明为public?

rangeCheck已经告诉我们了结果的正确与错误,所以外部根本不需要知道内部到底如何操作,所以该方法应该声明为private而不声明为public。

2.HashSet原理

2.1 将元素加入HashSet(散列集)中,其存储位置如何确定?需要调用那些方法?

HashSet存储的对象都是不重复的,所以当HashSet储存元素时,调用了hashCode方法来确定储存位置。

3.ArrayListIntegerStack

3.1 比较自己写的ArrayListIntegerStack与自己在题集jmu-Java-04-面向对象2-进阶-多态、接口与内部类中的题目5-3自定义接口ArrayIntegerStack,有什么不同?(不要出现大段代码)

ArrayListIntegerStack不需要指针,但ArrayIntegerStack则需要指针。

3.2 简单描述接口的好处.

4.Stack and Queue

4.1 编写函数判断一个给定字符串是否是回文,一定要使用栈,但不能使用java的Stack类(具体原因自己搜索)。请粘贴你的代码,类名为Main你的学号。

4.2 题集jmu-Java-05-集合之5-6 银行业务队列简单模拟。(不要出现大段代码)

5.统计文字中的单词数量并按单词的字母顺序排序后输出

5.1 实验总结

**7.1 完善图形界面(说明与上次作业相比增加与修改了些什么)
7.2 使用集合类改进大作业

3. 码云上代码提交记录及PTA实验总结

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

3.1. 码云代码提交记录

3.2. PTA实验

原文地址:https://www.cnblogs.com/set-L-Ann/p/6682621.html