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

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

通过定义数组elementData[i]来遍历o在给定数组的位置

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代码
private void rangeCheck(int index) {
if (index >= size)
     throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}

先检查要删除的位置如果超出List大小,就抛出IndexOutOfBoundsException异常。删掉某个元素后,后面的元素复制到前面,size相应-1

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

}

通过将旧数组的内容复制到一个新的,大小为原来1.5倍的数组完成的

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

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

不希望外部调用,也不用外部调用

2.HashSet原理

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

当我们向HashSet中添加一个元素时,HashSet会先调用该对象的hashCode()方法得到其hashCode值,根据该值决定该对象在桶中存储位置

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

通过equals和hascode,若都一样则为相等

ArrayListIntegerStack

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

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

ArrayIntegerStack定义时需要确定大小,ArrayListIntegerStack可以动态改变大小,而且可以调用List的方法。

public ArrayIntegerStack(int n) {
this.arr = new Integer[n];
}
public ArrayListIntegerStack(){
this.list = new ArrayList<Integer>();
}

3.2 简单描述接口的好处.

通过接口可以将任务分模块,也可以将底层和应用分隔开。在实际编程中,可以使类使用多种状态,在维护时也比较方便
Stack and Queue
4.1 编写函数判断一个给定字符串是否是回文,一定要使用栈,但不能使用java的Stack类(具体原因自己搜索)。请粘贴你的代码,类名为Main你的学号。
import java.util.*;
class stack{
private LinkedList a =new LinkedList();
public void push( T c){
a.add(c);
}
public T pop() return a.removeLast();
} public T peek()
{
return a.getLast();
}
public boolean empty(){
return a.isEmpty();
}
public String toString() {
return a.toString
}

        }

public class main {
public static void main(String[] args) {
    Stack<Character> a = new Stack<Character>();
 String string = "201521123030";
   // String string = "201521125102";
    
    for (int i = 0; i < string.length(); i++) {
        a.push(string.charAt(i));
    }
    boolean flag = true;
    for (int i = 0; i < string.length(); i++) {
        if (a.pop() != string.charAt(i)) {
            flag = false;
            break;
        }
    }
    
    if (flag) {
    	
        System.out.println("yes");
    } else {
        System.out.println("no");
    }
}

}

4.2 题集jmu-Java-05-集合之5-6 银行业务队列简单模拟。(不要出现大段代码)
// TODO Auto-generated method stu Scanner in=new Scanner(System.in);
queue a=new queue ();
ArrayListIntegerStack stacka=new ArrayListIntegerStack(m);
ArrayListIntegerStack stackb=new ArrayListIntegerStack(m);
for(int i=0;i<m;i++){
Integer num=in.nextInt();
//System.out.println(stack.push(num));
if(num%2==1){
stacka.push(num); }
else stackb.push(num);
}
while(!stacka.empty()||stackb.empty()){
if(!stacka.empty()){
System.out.print(stacka.pop()+" ");
System.out.print(stacka.pop()+" ");
}
if(!stackb.empty()){
System.out.print(stackb.pop()+" ");
}

                    }

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

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

public class Mian {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int i=0,j=0;
String str;
Set<String> setadd=new TreeSet<String>();
for(;;i++){
str=in.next();
if(str.equals("!!!!!"))break;
else setadd.add(str);
System.out.println(setadd.size());
for(String e:setadd){
j=j+1;
System.out.println(e);
if(j==10)break;
}
}
}

5.1 实验总结

新建TreeSet的对象,并通过键盘循环输入字符串,通过for(string e.a)输入集合中的字符串。

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

posted @ 2017-04-08 19:21  周汉麟  阅读(137)  评论(0编辑  收藏  举报