第7周-集合

1. 本周学习总结

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

2. 书面作业

1ArrayList代码分析

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判断遍历ArrayList的时候使用的方法,若o为equals,则返回序号,如果o为null,就没有equals方法。

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

删掉某个元素后,把后面的元素全部前移,最后再把size-1位置赋null。

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

使用ArrayList存储数据是数据类型是object类,它是所有类的父类,不用考虑元素的类型,像1.1,1.2中的ArrayList里元素也没有定义。

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

内部数组容量不够时,会自动调用grow方法生成一个1.5倍大的数组将原数组元素放进去。

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

/**
 * Checks if the given index is in range.  If not, throws an appropriate
 * runtime exception.  This method does *not* check if the index is
 * negative: It is always used immediately prior to an array access,
 * which throws an ArrayIndexOutOfBoundsException if index is negative.
 */
private void rangeCheck(int index) {
    if (index >= size)
        throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}

生命为private,则方法只能在类内部实现,而rangeCheck方法只是用来判断数组是否超界,只在该类内部有效。

2HashSet原理

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

元素加入HashSet中时,HashSet会调用HashCodeC()方法,得到一个'hashcode'值确定元素在列表中的位置;如果桶中已有其他元素,则调用equals()方法与已有元素进行比较。;如果比较结果为真,则用新的值替换旧的值;如果比较结果为假,则将该元素插入桶中。

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

3ArrayListIntegerStack

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

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

5-1中的ArrayListIntegerStack不需要定义数组的大小,list可以对初始容量赋值,而在5-3中的ArrayIntegerStack就需要自己定义数组的大小,需要指针。

2 简单描述接口的好处.

不同的对象可以引同一个方法,可以用接口引用了类,使用相同的方法名,不同的实现。

4Stack and Queue

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

package project002;

import java.util.Scanner;
import java.util.Stack;

public class Main201521044152 {
    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
        String str = in.next();
    System.out.println(isPalindromeNumber(str));
    }

    public static boolean isPalindromeNumber(String str){
        ArrayListTStack myStack = new ArrayListTStack();
	char[] charArray = str.toCharArray();
	  for (int i = 0; i < charArray.length / 2 ; i++) {
	     myStack.push(String.valueOf(charArray[i])); 
	  }
    for(int i = charArray.length / 2 ; i <  charArray.length; i++ ){
	 if( !myStack.pop().equals(String.valueOf(charArray[i])) ) return false;
	    }
	    return true;
    }
    }

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

建立两个队列,奇数放在List1里,偶数放在ListB里,List1输出两个然后Listb输出一个,如果一个队列为空,则跳出循环并且输出队列的剩余元素。

for (int i = 0; i <= n; i++) {
    int x = scanner.nextInt();
    n=i;
    continue;
    if (x % 2 !== 0) {
      List1.add(x);
 } 
    else {
    List2.add(x);
    }
 }

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

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

1 实验总结

要对单词的字母顺序排序后输出,需要用到TreeSet;空格为界限分开每个单词。

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

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

1 伪代码

2 实验总结

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

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

2 使用集合类改进大作业

参考资料:
JTable参考项目

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

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

1. 码云代码提交记录

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

2. PTA实验

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

posted @ 2017-04-08 13:50  毛卓  阅读(242)  评论(1编辑  收藏  举报