zoukankan      html  css  js  c++  java
  • 201521123035《Java程序设计》第七周学习总结

    1. 本周学习总结

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

    参考资料:
    XMind

    2. 书面作业

    1.ArrayList代码分析

    1.1 解释ArrayListcontains源代码

    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;
    }
    
    contains遍历ArrayList,indexOf方法对于传入的是否是null进行了区分。
    

    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;
        }
    
    private void rangeCheck(int index) {
            if (index >= size)
                throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
        }
    
    在 E remove中,如果numMoved是最后一个元素的话,就向前移动一个位置。rangeCheck方法检查index是否越界。
    

    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));
        }
    
    public IndexOutOfBoundsException(String s) {
            super(s);
        }
    
    private String outOfBoundsMsg(int index) {
            return "Index: "+index+", Size: "+size;
        }
    
    这个方法是在内部实现的,外部不用访问所以不用public。
    

    2.HashSet原理

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

    HashSet中,元素是随机存取的,没有排列顺序,需要调用equals()与hashCode()方法。
    

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

    3.ArrayListIntegerStack

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

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

    感觉ArrayListIntegerStack代码简单了很多,而且只需要新建一个Main类,后者还要新建IntegerStack类和ArrayIntegerStackl类
    

    3.2 简单描述接口的好处.

    接口可以实现不相关类的相同行为和了解对象的交互界面。

    4.Stack and Queue

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

    public class Main201521123035 {
            public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            System.out.println("请输入字符串:");
            while (scan.hasNext()) {
                ArrayListStack stack = new ArrayListStack();
                String s = scan.next();
                String s1="";
                for (int i = 0; i < s.length(); i++) {
                    s1 = s.substring(i, i + 1);
                    stack.push(s1);
                }
                s1 = "";
                for (int i = 0; i < s.length(); i++) {
                    s1 = s1 + stack.peek();
                    stack.pop();
                }
                if (s.equals(s1)) {
                    System.out.println("是");
                }else {
                    System.out.println("否");
                }
                System.out.println("请输入字符串:");
            }
            scan.close();
        }
    }
    

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

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

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

    5.1 实验总结

    实验总结:要多参考类似代码,举一反三,遇到不会的就和同学交流,但是一定要清楚每条代码是什么意思。
    

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

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

    3.1. 码云代码提交记录

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

    3.2. PTA实验

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

  • 相关阅读:
    当老板如何带团队?
    创业者第一法宝-了解自己
    交流才能交易,交易才能交心
    集合框架
    MySQL一些命令语法
    JS组成整理
    git中可以pull但是push提示Everything up-to-date的情况
    循环判断以及文件的使用--练习1
    Hello World !
    linux文件权限修改
  • 原文地址:https://www.cnblogs.com/wuling15/p/6681046.html
Copyright © 2011-2022 走看看