博客园 首页 联系 订阅 管理

1. 本周学习总结

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

2. 书面作业

1. ArrayList代码分析

1.1 解释ArrayList的contains源代码

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;
}
public boolean equals(Object obj) {
return (this == obj);
}

ArrayList是允许重复的,有序的元素的集合,但当我们想用它来放入不同的元素时,就可以使用contains()方法

contains()方法就是用来遍历Object里的数组,如果找到与标记相同的值则返回所在位置,否则返回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;
}

可以看出是在删除元素并将位置前移的操作,先判断一下所传入的参数是否超出原来数组的长度,若超出则抛出异常
否则将元素删除,后面的元素前移,最后将多余的引用(已被前移)置为null。

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

ArrayList是一个数组列表,初始数组数据类型都为Object类型,而Object又是所有类的父类,所以,任何类型的元素都是可以存储在ArrayList中的

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

源代码:

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

/**
* Inserts the specified element at the specified position in this
* list. Shifts the element currently at that position (if any) and
* any subsequent elements to the right (adds one to their indices).
*
* @param index index at which the specified element is to be inserted
* @param element element to be inserted
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public void add(int index, E element) {
if (index > size || index < 0)
throw new IndexOutOfBoundsException(
"Index: "+index+", Size: "+size);

ensureCapacity(size+1); // Increments modCount!!
System.arraycopy(elementData, index, elementData, index + 1,
size - index);
elementData[index] = element;
size++;
}
int newCapacity = oldCapacity + (oldCapacity >> 1);

当容量不够时将当前容量的1.5倍赋值给新的容量

if (newCapacity - minCapacity < 0)

判断新容量是否足够,足够则使用当前新容量创建新数组,不够就将数组长度设置为需要的长度

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

2. HashSet原理

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

HashSet类被称为集合,该容器中只能存储不重复的对象。
HashSet对象根据哈希算法计算出散列码,找到存储位置后根据相应的哈希码,找到对应的桶

调用方法:

调用 HashMap 的 containsKey 判断是否包含指定 key

equals() 方法,分别对各属性进行判断,是否相等

hashCode() 方法,有可能两者并不相等,但是hash码相同,所以要结合起来比较

2.2 将元素加入HashSet中的时间复杂度是多少?是O(n)吗?(n为HashSet中已有元素个数)

添加元素的时候不需要遍历,是直接通过哈希算法来计算位置,所以时间复杂度为O(1)。

3. ArrayListIntegerStack

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

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

public class ArrayListIntegerStack implements IntegerStack{
private List arrList=new ArrayList();}

//

class ArrayIntegerStack implements IntegerStack{

Integer[] Stack;

private static int top=0;

public ArrayIntegerStack(int n){

Stack = new Integer[n];

}
存储形式的不同

上次的题目中使用的是数组的方法来存储栈
而这次采用的则是动态数组来进行存储

ArrayList不用设定头指针,可以通过序号直接找到元素,进行删除,返回的操作,。

3.2 结合该题简单描述接口的好处,需以3.1为例详细说明,不可泛泛而谈。

接口IntegerStack和我们上次作业中的接口一样,只要重新写一个类来实现这个接口,
然后根据要求实现其中的方法即可,而不用重新写一个栈的类然后编写方法实现,后者明显增加了我们的代码量

4. Stack and Queue

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

import java.util.List;
import java.util.LinkedList;
import java.util.Scanner;

interface IntegerStack1{ //接口定义
public void push(Character item);
public void pop();
public Character peek();
public boolean empty();
public int size();

}
class Stack implements IntegerStack1{ //实现类
List stack = new LinkedList();
@Override
public void push(Character item) {
stack.add(item);
}

@Override
public void pop() {
stack.remove(stack.size()-1);
}

@Override
public Character peek() {
return stack.get(stack.size()-1);
}

@Override
public boolean empty() {
return stack.isEmpty();
}

@Override
public int size() {
return stack.size();
}

}
public class Main201621123039 {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in

);
Stack words=new Stack();
String str=sc.nextLine();
boolean flag=true;
char []word=str.toCharArray();
for(char e:word){
words.push(e);
}
for(int i=0;i<words.size();i++){
if(word[i]==words.peek()) words.pop();
else flag=false;
}
if(!flag) System.out.println("no");
else System.out.println("yes");
}
}

str.toCharArray()将输入分拆并储存在word数组中

将word数组内的元素全部入栈

word数组第i个元素(初始为首字符)与栈顶元素比较

falg初始置真,若相等,出栈,循环直到全部比较结束,若不相等则退出循环,flag为假

4.2 题集jmu-Java-05-集合之银行业务队列简单模拟(只粘贴关键代码)。请务必使用Queue接口,并说明你使用了Queue接口的哪一个实现类?



使用LinkedList实现类。

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

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

5.1 实验总结

Treeset具有排序功能且不添加重复的元素,此题中Treeset将数据存储到words集合

words.size()用于输出单词数量

当words.size()<10时,输出words中所有的元素。当words.size()>10时,输出10次words中的元素。

参考资料:

3.码云及PTA

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

3.1. 码云代码提交记录

3.2 截图PTA题集完成情况图


需要有两张图(1. 排名图。2.PTA提交列表图)

3.3 统计本周完成的代码量

需要将每周的代码统计情况融合到一张表中。

周次 总代码量 新增代码量 总文件数 新增文件数
1 73 73 2 2
2 281 281 9 9
3 908 908 16 16
4 1122 214 16 16
5 1557 435 32 11
6 2056 499 45 13
7 2145 89 48 3
8 2446 301 54 6