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;
}
遍历元素,如存在查找的元素,返回true,否则返回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;
}
rangeCheck判断传入的数值是否越界,越界则报错;没有越界则将对应下标的元素取出来,然后将下标之后的元素全部前移一位,最后一位变为NULL,size减一最后返回被删除的元素
1.3 结合1.1与1.2,回答ArrayList存储数据时需要考虑元素的类型吗?
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);
}
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);
// minCapacity is usually close to size, so this is a win:
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倍的新数组
1.5 分析private void rangeCheck(int index)源代码,为什么该方法应该声明为private而不声明为public?
避免外部调用
2.HashSet原理
2.1 将元素加入HashSet(散列集)中,其存储位置如何确定?需要调用那些方法?
先用hashCode()方法计算出这个对象的Hash码,然后再根据Hash码到相应的存储区域用equals()方法查找
3.ArrayListIntegerStack
题集jmu-Java-05-集合之5-1 ArrayListIntegerStack
3.1 比较自己写的ArrayListIntegerStack与自己在题集jmu-Java-04-面向对象2-进阶-多态、接口与内部类中的题目5-3自定义接口ArrayIntegerStack,有什么不同?(不要出现大段代码)
public ArrayIntegerStack(int n){
this.stack = new Integer[n];
}
public ArrayListIntegerStack() {
list = new ArrayList<Integer>();
}
ArrayIntegerStack需要确定大小,ArrayListIntegerStack可以动态改变大小
3.2 简单描述接口的好处.
接口可以多继承,不仅仅是单一的继承关系,灵活性高,可以在接口中定义多个抽象方法,在类中实现
4.Stack and Queue
4.1 编写函数判断一个给定字符串是否是回文,一定要使用栈,但不能使用java的Stack类(具体原因自己搜索)。请粘贴你的代码,类名为Main你的学号。
public class Main201521123016 {
public static void main(String[] args) {
System.out.println("字符串abcba是否为回文?"+HW("abcdcba"));
}
public static boolean HW(String s){
if(s.length()<=1){
return true;
}
else if(s.charAt(0) != s.charAt(s.length()-1)){
return false;
}
return HW(s.substring(1,s.length()-1));
}
}
4.2 题集jmu-Java-05-集合之5-6 银行业务队列简单模拟。(不要出现大段代码)
5.统计文字中的单词数量并按单词的字母顺序排序后输出
题集jmu-Java-05-集合之5-2 统计文字中的单词数量并按单词的字母顺序排序后输出 (不要出现大段代码)
5.1实验总结
Set<String> str = new TreeSet<String>();
str.toArray();
if (str.size() <= 10)
for (String words : str) {
System.out.println(words);
}
else
for (String words : str) {
if (n-- <= 0)
break;
System.out.println(words);
}
TreeSet具有自动排序功能