201521123012 《Java程序设计》第七周学习总结

1. 本周学习总结

以你喜欢的方式(思维导图或其他)归纳总结集合相关内容。

参考资料:
XMind


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

contains的实现就是遍历一次ArrayList,若equals,则返回序号,当然,要判断o是否为null,假如onull就没有equals方法。

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

1.3 结合1.1与1.2,回答ArrayList存储数据时需要考虑元素的类型吗?
不需要。可以随便放,因为实际上是Object数组实现的,而Object类又是所有类的父类。
1.4 分析add源代码,回答当内部数组容量不够时,怎么办?
源码

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

尾部添加.

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);
}   
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方法,使用modCount++记录每次操作,之后当minCapacity - elementData.length > 0成立的时候,即ArrayList的内部容量大于数组的长度时,它就会对ArrayList内部的数组进行扩容了;这时候,我们就要使用grow方法进行扩容,先扩为原来的1.5倍( int newCapacity = oldCapacity + (oldCapacity >> 1); ,如果是大容量,则取大容量的值,再利用Arrays.copyOf()方法,进行扩容。最后,又回到add()方法中,使用elementData[size++] = e;为数组中的元素赋值。所以当内部数组容量不够时,我们不需要担心,源代码本身就能够使得内部数组扩容。

可参考网址:http://www.cnblogs.com/vitasyuan/p/5410435.html

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

private void rangeCheck(int index) {
    if (index >= size)
        throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
} 
用户不需要知道具体发生了什么,只要可以实现就好。private使得外部无法使用这个方法。

2.HashSet原理

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

因为输入Set中的元素一定是唯一的,所以需要用到equal()来保证元素唯一性。
然后,在HashSet当中的元素还要定义hashCode()方法。

2.2 选做:尝试分析HashSet源代码后,重新解释1.1

3.ArrayListIntegerStack

题集jmu-Java-05-集合5-1 ArrayListIntegerStack

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

答:不同:5-1中的ArrayListIntegerStack不需要定义数组的大小,list自身就可以为内部数组进行初始容量赋值,及扩容,而ArrayIntegerStack中则需要自行定义数组的大小,并且在删除,输出等操作中需要使用到top指针来帮助进行。

3.2 简单描述接口的好处.

答:相同方法,不同实现。可以让我们根据需求,通过不同方式去实现同一个目的。

4.Stack and Queue

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

 ArrayList<Integer> arr1=new ArrayList<Integer>();
    ArrayList<Integer> arr2=new ArrayList<Integer>();

    int n=sc.nextInt();
    for(int i=0;i<n;i++)
    {
        int m=sc.nextInt();

        if(m%2==1)arr1.add(m);
        else arr2.add(m);
    }
    Collections.sort(arr1);
    Collections.sort(arr2);

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

题集jmu-Java-05-集合之5-2 统计文字中的单词数量并按单词的字母顺序排序后输出 (不要出现大段代码)

public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        Set<String> arr = new TreeSet<String>();
        String str=null;
        while(true)
        {
            String word=sc.next();
            if(word.equals("!!!!!"))break;
            arr.add(word);

        }

        System.out.println(arr.size());


        int i=0;
        for(String s:arr)
        {
            if(i==10)break;
            else if(i>arr.size())break;
            System.out.println(s);
            i++;
        }
    }
}

5.1 实验总结

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

题目集:jmu-Java-05-集合
3.1. 码云代码提交记录

  • 在码云的项目中,依次选择“统计-Commits历史-设置时间段”, 然后搜索图

3.2. PTA实验

  • 编程(5-1, 5-2, 5-3(选做), 5-6)
  • 实验总结已经在作业中体现,不用写。
posted @ 2017-04-08 20:06  lxr-  Views(181)  Comments(1Edit  收藏  举报