201521123033《Java程序设计》第7周学习总结

1. 本周学习总结

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

XMind
answer:

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

answer:contains()方法:根据输入参数o遍历ArrayList。当o == null时,遍历并判断elementData[i]是否为空。如果elementData[i]==null,返回序号i,否则调用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));
    }

answer:先检查删除的位置是否超出index的大小。如果超出,则显示IndexOutOfBoundsException异常。如果未超出,则删除该位置元素,并将后面的元素往前移,size-1,原来最后一个元素为null.

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

answer:不用考虑。从上述源代码可以看到参数都为Object类型的对象,而Object类又是所有类的父类,所以不需要考虑。

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

    // overflow-conscious code
    if (minCapacity - elementData.length > 0)//如果超出容量,调用grow扩容
        grow(minCapacity);
}
private void grow(int minCapacity) {
    // overflow-conscious code
    int oldCapacity = elementData.length;
    int newCapacity = oldCapacity + (oldCapacity >> 1);//将数组扩大为原数组的1.5倍
    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);//将旧数组copy到新数组里
}

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

源代码:

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

answer:只在内部进行,而不被外部获取到。rangeCheck方法只是在remove等操作中,用来判断是否超出了界限。外界没有获取的必要。

2.HashSet原理

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

answer:HashSet使用是由链表数组实现的,每个列表的位置被称为桶。可以根据hashCode值查找到对应的桶。
调用方法:equals(),hashCode()

3.ArrayListIntegerStack

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

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

answer:ArrayListIntegerStack是使用ArrayList存储,可自动扩容,而ArrayIntegerStack是使用数组的,需要提前定好数组大小。

3.2 简单描述接口的好处.

answer:
比较灵活,可以用一个接口操作不同的类,如果我们需要使用其他的类时,我们只需要再重新用这个接口引用了另一个类就行,这样代码更加简洁。

4.Stack and Queue

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

answer:

import java.util.*;
public class Main2015033 {

	public static void main(String[] args) {
		Stack<Character> stack = new Stack<Character>();
        Scanner in = new Scanner(System.in);
        String str = in.next(); 
        for (int i = 0; i < str.length(); i++) {
            stack.push(str.charAt(i));
        }
    
        for (int j= 0; j < str.length();j++) {
            if (stack.pop() != str.charAt(j)) {
                System.out.println("no");
                break;
            }
            else {
                System.out.println("yes");
                break;
            }
        }
     }
}

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

answer:

//奇偶分队
for(i = 0; i < n; i++){
    int temp=in.nextInt();
    if((temp%2)!=0)
        A.add(temp);
    else
        B.add(temp);
}
for(i=0;i<n;i++){
    for(int j=0;j<2;j++){
        if(!A.isEmpty())
            C.add(A.poll());
    }
    if(!B.isEmpty())
        C.add(B.poll());
}
//输出
for(i=0;i<n-1;i++)
    System.out.print(C.poll()+" ");
System.out.print(C.poll());

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

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

//无重复对象
 while(in.hasNext()){
     if(!arr.contains(str)){
           arr.add(str);
            }
     }
//排序
Collections.sort(arr);
//入队
if(arr.size()>10)
        	q.addAll(arr.subList(0, 10));
        else
        	q.addAll(arr);
//输出
for (String queue : q){
    System.out.println(queue);
}

5.1 实验总结

answer:做这题时,先做到数组中无重复对象,再排序即可。

6.选做:加分考察-统计文字中的单词数量并按出现次数排序

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

6.1 伪代码

6.2 实验总结

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

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

7.2 使用集合类改进大作业

参考资料:
JTable参考项目

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

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

3.1. 码云代码提交记录

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

3.2. PTA实验

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

原文地址:https://www.cnblogs.com/gqirong/p/6681811.html