201521123008《Java程序设计》第七周实验总结

时间:2023-03-09 06:38:28
201521123008《Java程序设计》第七周实验总结

1.本周学习总结

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

201521123008《Java程序设计》第七周实验总结

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

判断列表中是否包含指定的不为null的元素,若有,则返回序号,contains再返回true;如果没有,就跳到循环外面,返回-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;
}

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

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

不需要

1.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);
}  

增加数组的长度

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值,根据该值决定该对象在列表中的存储位置。

如果列表中已有其他元素,则调用加入对象的equals()方法与已有元素进行比较。如果比较结果为假,则将对象插入列表中。如果比较结果为真,则用新的值替换旧的值。

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

3.ArrayListIntegerStack

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

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

ArrayListIntegerStack是用ArrayList来实现栈,ArrayIntegerStack是用数组来实现栈。因此,ArrayIntegerStack可能会出现栈满,但ArrayListIntegerStack就可以自动扩容。

3.2 简单描述接口的好处.

接口的好处是相同方法,不同实现。就好像ArrayListIntegerStack与ArrayIntegerStack,让我们根据需求,通过不同的方式去实现同样的目标

4.Stack and Queue

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

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
interface StringStack{
    public String push(String item);
    public String pop();
}
class ArrayListStringStack implements StringStack{
    private List<String> list;

    public ArrayListStringStack() {
        list=new ArrayList<String>();
    }
    @Override
    public String push(String item) {
        if(item==null)
            return null;
        list.add(item);
        return item;
    }

    @Override
    public String pop() {
        // TODO Auto-generated method stub
        if(list.isEmpty())
            return null;
        return list.remove(list.size()-1);
    }

}
public class Main201521123008 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        StringStack stack=new ArrayListStringStack();
        String m=sc.next();
        int t=1;
        for(int i=0;i<m.length();i++)
        {
            stack.push(String.valueOf(m.charAt(i)));
        }
        for(int i=0;i<m.length();i++)
        {
            if(String.valueOf(m.charAt(i)).equals(stack.pop()))t=1;
            else
            {
                t=0;
                break;
            }
        }
        if(t==1)System.out.println("true");
        else System.out.println("flase");
        }

}

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

while(!A.isEmpty() || !B.isEmpty())
    {
        Integer a1 = A.poll();
        if(a1 != null){
            if(B.isEmpty() && A.isEmpty())
            System.out.print(a1);
            else
                System.out.print(a1 + " ");
        }

        Integer a2 = A.poll();
        if(a2 != null){
            if(B.isEmpty() && A.isEmpty())
            System.out.print(a2);
            else
                System.out.print(a2 + " ");
        }
        Integer b = B.poll();
        if(b != null){
            if(B.isEmpty() && A.isEmpty())
            System.out.print(b);
            else
                System.out.print(b + " ");
        }

    }  

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

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

 Set<String> str = new TreeSet<String>();
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String s = sc.next();
            if (!s.equals("!!!!!"))
                str.add(s);
            else
                break;
        }
        System.out.println(str.size());
        for (int i = 0; i < 10; i++) {
            System.out.println(str.toArray()[i]);
        }

5.1 实验总结

使用TreeSet,会默认帮我们排好序。

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

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

6.1 伪代码

6.2 实验总结

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

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

7.2 使用集合类改进大作业

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

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

3.1. 码云代码提交记录

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

201521123008《Java程序设计》第七周实验总结

3.2. PTA实验

编程(5-1, 5-2, 5-3(选做), 5-6)

实验总结已经在作业中体现,不用写。