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

    1. 本周学习总结

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

    2. 书面作业

    1.ArrayList代码分析

    1.1 解释ArrayList的contains源代码

    1.2 解释E remove(int index)源代码

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

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

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

    1.1 源代码
    public boolean contains(Object o) {
    return indexOf(o) >= 0;
    }
    遍历元素,如存在查找的元素,返回true,否则返回false;在源代码的注释中,Returns true if this list contains the specified element.如果此列表包含指定元素,则返回true

    1.2 源代码
    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;
    }
    

    查找此ArrayList中是否包含要找的元素,若有,返回true,否则返回false;在源代码注释中,Removes the element at the specified position in this list.删除此列表中指定位置的元素。

    1.3 不需要,因为其参数为Objcet类型的对象,所有对象皆为Object。

    1.4 源代码注释中,Appends the specified element to the end of this list将指定的元素到这个列表的末尾,当内部数组容量不够时,需要增量,即增加数组长度,ensureCapacityInternal(size + 1); // Increments modCount!!增加数组长度

    1.5private void rangeCheck(int index) {
    if (index >= size)
    throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }
    因为private修饰的实例方法只能在本类被使用。不可被外部类调用,保证了一个封装性。

    2.HashSet原理

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

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

    2.1HashSet查找某个对象时,首先用hashCode()方法计算出这个对象的Hash码,HashCode值决定了该元素的存储位置,根据Hash码到相应的存储区域用equals()方法查找。

    2.2HashSet与ArrayList的contains有异曲同工之妙,最后的结果都是没有重复的元素;HashSet的源代码中用到了HashMap,我个人理解,是不是将hashcode看做key,将数组元素的值看做value,将其通过HashSet存储在散列表。

    3.ArrayListIntegerStack

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

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

    3.2 简单描述接口的好处.

    3.1存储方式不同,前者使用ArrayList,虽然本质也是数组存储,但不需要一开始就要考虑数组长度,后者是数组存储,并且需要一个指针top来确定栈顶元素;前者不需要考虑栈满的情况,后者受一开始输入的数组长度的约束,需要考虑栈满的情况

    3.2接口拥有更大的灵活性,同时接口也是可插入性的保证

    4.Stack and Queue

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

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

    4.1

    for (int i = 0; i < n; i++) {
    if(q1.size()2){
    System.out.printf(q1.poll()+" "+q1.poll()+" ");
    if(q2.size()>=1)
    System.out.printf(q2.poll()+" ");
    }
    if(Num[i]%2
    1)
    q1.add(Num[i]);
    else
    q2.add(Num[i]);
    }
    while(q1.size()>1){
    System.out.printf(q1.poll()+" ");
    if(q1.size()1)
    System.out.printf(q1.poll()+"");
    while(q2.size()>1)
    System.out.printf(q2.poll()+" ");
    if(q2.size()
    1)
    System.out.printf(q2.poll()+"");
    in.close();
    }

    4.2

        while(!link1.isEmpty()||!link2.isEmpty())
        {
            Integer a1=link1.poll();
            if(a1!=null)
            {
                if(link1.isEmpty()&&link2.isEmpty())
                System.out.println(a1);
                else
                System.out.print(a1+" ");
            }
            Integer a2=link1.poll();
            if(a2!=null)
            {
                if(link1.isEmpty()&&link2.isEmpty())
                System.out.println(a2);
                else
                System.out.print(a2+" ");
            }
            Integer b=link2.poll();
            if(b!=null)
            {
                if(link1.isEmpty()&&link2.isEmpty())
                System.out.println(b);
                else
                System.out.print(b+" ");
            }
        }
    

    结果如下:

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

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

    5.1 实验总结

    while (input.hasNext()) {
    String word = input.next();
    if (word.equals("!!!!!"))
    break;
    //不同的单词加入集合中
    else if (!str.contains(word))
    str.add(word);
    }
    统计文本出现的词汇并存储,此时要想到Set,Set集合的对象是不重复的,但要是对对象进行排序,需要的是TreeSet,TreeSet具有排序功能,HashSet可以用作无序的词汇存储,因而本题不使用HashSet。

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

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

    3.1. 码云代码提交记录

  • 相关阅读:
    iframe高度自适应方法
    mysql left join对于索引不生效的问题
    禁止百度转码和百度快照缓存的META声明
    使用graphviz绘制流程图
    安装php扩展sphinx-1.2.0.tgz和libsphinxclient0.9.9
    5种主要的编程风格和它们使用的抽象
    Nodejs调用Aras Innovator服务,处理AML并返回AML
    使用Rancher管理Docker
    docker容器间通信
    使用Portainer管理Docker
  • 原文地址:https://www.cnblogs.com/handsome321/p/6682888.html
Copyright © 2011-2022 走看看