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

答:contains方法执行后调用indexOf方法,先判断是否为空,然后遍历集合里的元素,如有等于空,则返回该元素下标。如果不为空,也是遍历集合里的元素,如有相等的就返回其下标,没有返回-1。最后contains方法判断返回值的大小,大于0说明存在,小于0表示不存在。

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

答:在ArrayList的移除第index个对象元素,之后后面的会编号依次变小。

1.1与1.2,回答ArrayList存储数据时需要考虑元素的类型吗?
答:不需要,因为ArrayList存储的是对象。

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

public boolean add(E e) {
    ensureCapacityInternal(size + 1);  // ensureCapacityInternal用来调整容量
    elementData[size++] = e;
    return true;
}  
private void ensureCapacityInternal(int minCapacity) {
    if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
        minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
    }

    ensureExplicitCapacity(minCapacity);
}
    modCount++;
    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); 
}  

答:当执行Add方法时,会检查数组的容量够不够,如果不够,就会以当前数组容量的两倍来重新构建一个数组,然后将旧元素拷贝到新数组中,然后丢弃旧数组。

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

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

答:是为了不让外部使用这个方法,重新构造一个新的数组性能消耗很大,所以不宜外部调用。

2.HashSet原理

2.1 将元素加入HashSet(散列集)中,其存储位置如何确定?需要调用那些方法?
答:HashSet的实现是封装了一个HashMap对象来存储所有的集合元素,所有放入 HashSet 中的集合元素实际上由 HashMap 的 key 来保存,可以通过HashSet的value来查询。需要两个方法,public int hashCode(),boolean equals(Object o)。

3.ArrayListIntegerStack

题集jmu-Java-05-集合之5-1 ArrayListIntegerStack
3.1 比较自己写的ArrayListIntegerStack与自己在题集jmu-Java-04-面向对象2-进阶-多态、接口与内部类中的题目5-3自定义接口ArrayIntegerStack,有什么不同?(不要出现大段代码)
答:ArrayListIntegerStack使用ArrayList储存,不会出现栈满的情况;ArrayIntegerStack则是用Integer数组储存,会出现栈满的情况,而且需要一个top确定栈顶元素。

3.2 简单描述接口的好处.
答:接口的好处在于它可以操作不同的类,使用相同的方法,且接口可以实现很多扩张功能如继承,提高了程序的灵活性和安全性。

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

题集jmu-Java-05-集合之5-2 统计文字中的单词数量并按单词的字母顺序排序后输出 (不要出现大段代码)
4.1 编写函数判断一个给定字符串是否是回文,一定要使用栈,但不能使用java的Stack类(具体原因自己搜索)。请粘贴你的代码,类名为Main你的学号。

public class Main201521123031 {
public static void main(String[] args) {
    System.out.println("Is abcba a Palindrome?"+isPalindrome("abcdcba"));
}
public static boolean isPalindrome(String s){
    if(s.length()<=1){
        return true;
    }else if(s.charAt(0) != s.charAt(s.length()-1)){
        return false;
    }
    return isPalindrome(s.substring(1,s.length()-1));
}
} 

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

while(!qa.isEmpty()){
            if(!qa.isEmpty()){
                qc.add(qa.poll());
                qc.add(qa.poll());
            }
            if(!qb.isEmpty())
                qc.add(qb.poll());
        }
        while(!qc.isEmpty()){
            int j=0;
            if(j<qc.size()-1){
                System.out.print(qc.poll()+' ');
                ++j;
            }
            else
                System.out.print(qc.poll());
        }

为了方便输出,把2个队列合并为一个再输出。

5.1 实验总结
答:使用TreeSet就可以将成员自然排序,输出时注意格式。

7.面向对象设计大作业-改进

7.1 完善图形界面(说明与上次作业相比增加与修改了些什么)

7.2 使用集合类改进大作业
参考资料:
JTable参考项目

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

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

3.1. 码云代码提交记录

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

3.2. PTA实验

编程(5-1, 5-2, 5-3(选做), 5-6)
实验总结已经在作业中体现,不用写。

原文地址:https://www.cnblogs.com/ltykm/p/6682654.html