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

第7周-集合

  1. 本周学习总结
其他注意点:

(1)List主要特征:
    其元素以线性方式存储
    集合中允许存放重复对象
(2)Set中的对象:
    不按特定方式排序
    没有重复对象
    Set最多有一个null元素
(3)hashCode与equals方法:
    如果一个两个对象通过equals方法比较为true
    那么他们的hashCode方法返回的值也要相等
(4)TreeSet支持两种排序方式:
    a.Comparable
      放入TreeSet中的类必须实现Comparable接口
    b.Comparator
      放入TreeSet中的类不必实现Comparable接口,但是必须创建一个实现Comparator接口的类

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;
    }
从源代码可以看出调用了indexOf方法,indexOf方法对于传入的是否是null进行了区分,如果找到了,就直接返回,找不到跳出循环外面,就返回-1,,那么contains()方法也会返回false。

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;
    }
remove函数通过RangeCheck(index)函数来判断所删除的位置是否超过了原列表的size(),然后将查找到的元素置为null,进行size()--操作

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

不需要,但是不能是基本数据类型

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

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

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);
    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;
}
如果容量够了,就直接以给的长度创建数组,当内部数组容量不够时,就进行扩充

1.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是不希望外部类使用它

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

首先使用hashCode(),计算出元素的哈希码,根据哈希码找到散列表中对应的列表(称为桶),如果存储的位置已经有元素占用,则利用equals()方法进行比较,不相同则插入桶中,相同则替换掉原来的元素。

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

不同之处在于前者使用了ArrayList的动态数组,不需要判断是否栈满,用完的时候会自动扩充,后者是Integer数组

3.2 简单描述接口的好处.

好处是我们可以使用一个接口来操作不同的类,至于操作哪一个类,就通过由动态绑定来决定。

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

package two;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

public class Main201521123066 {

	public static void main(String[] args) {

		Scanner s = new Scanner(System.in);
		while (s.hasNext()) {
			ArrayList<String> str = new ArrayList<String>();
			String str1 = s.next();
			for (int i = 0; i < str1.length(); i++) {
				String s1 = str1.substring(i, i + 1);
				str.add(s1);
			}
			String s1 = "";
			for (int i = str1.length() - 1; i >= 0; i--) {
				s1 = s1 + str.get(i);
			}
			if (str1.equals(s1)) {
				System.out.println("是回文");
			} else {
				System.out.println("不是回文");
			}
		}
	}

}

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

使用队列来写,关键代码如下:

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

5.1 实验总结

1.可以用Iterator
(也可以利用TreeSet自动排序功能(后来从舍友处学来))
2.用Set,不允许内容重复

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

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

3.1. 码云代码提交记录

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

原文地址:https://www.cnblogs.com/zxl3066/p/6682959.html