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

    答:可以看出作用是验证该ArrayList中是否包含某个对象,使用for循环Object类下的equals方法进行比较,在实际操作时需在个人类中重写equals方法,否则只会比较其地址值,返回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;
    }
    private void rangeCheck(int index) {
        if (index >= size)
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }
    

    答:remove(int index)用于删除数组容器中指定位置index上的元素,并返回此元素。先另保存并删除index元素,将index后面的所有元素全部往前依次移动一个位置,数组容器的最后位置被腾空,但是仍然持有某个对象的引用,需要把这个多余的引用置为null,返回保存的已删除的元素。

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

    答:需要考虑,从1.1可知某些类型的类中会覆盖Object类中的方法,所以不同类型调用时可能会有不同结果。

    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方法增加容量
            grow(minCapacity);
    }   
    
    private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = elementData.length;
        int newCapacity = oldCapacity + (oldCapacity >> 1); //增加原来容量的一半(右移一位就是/2),也就是说新List的容量是旧的1.5倍
        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倍的新数组。
    }  
    

    答:add方法会在末尾追加一个元素,并增加数组总数。ArrayList的add方法会在已存在的定长的集合中添加元素,当集合装满即内部数组容量不够时,会在添加元素之前进行扩容。

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

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

    答:rangeCheck()保证index在ArrayList长度范围之内,并在越界时抛出IndexOutOfBoundsException异常。所以它是私有的内部方法,只能被ArrayList调用,所以声明为private。

    2.HashSet原理

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

    答:当HashSet储存元素时,调用了hashCode方法来确定储存位置。如果位置相同,则再通过调用equals来确定内容是否相同。

    3.ArrayListIntegerStack

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

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

    答:集合5-1中的ArrayListIntegerStack接口采用的是list列表来存储栈元素,而5-3采用的是数组的形式。采用列表进行存储时不需要固定的长度,不用定义top来指向栈的顶部,入栈时不用判断栈是否为空。

    3.2 简单描述接口的好处.

    答:接口中规定了实现类必须去实现的各种方法,思路清晰,规范了编程流程。同时,不同的方法可以有不同的实现方式,如IntegerStack中的peek,push,pop方法在ArrayListIntegerStack中实现时可根据存储方式的不同,而以不同的形式实现这些方法。

    4.Stack and Queue

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

    public class Main201521123048 {
    
    
    	    public static void main(String[] args) {
    	   
    	        Stack<Character> stack = new Stack<Character>();
    	        Scanner scanner = new Scanner(System.in);
    	        int a = 1;
    	        String string = scanner.next(); 
    	        for (int i = 0; i < string.length(); i++) {
    	            stack.push(string.charAt(i));    
    	        }
    	    
    	        for (int j= 0; j < string.length(); j++) {
    	            if (stack.pop() != string.charAt(j)) {
    	            
    	                System.out.println("为回文数列");
    	                break;
    	            }
    	            else {
    	                System.out.println("不是回文数列");
    	                break;
    	            }
    	        }
    	     }
    	}
    

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

    for (int i = 1; i <= n; i++) {
    int sc = scanner.nextInt();
    if (sc % 2 == 0) {
        B.offer(x);
    } else {
        A.offer(x);
    }
    }
    

    将元素区分成AB两队

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

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

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

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

    5.1 实验总结

    定义一个TreeSet,自动实现元素排序。

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

    3.1. 码云代码提交记录

    3.2. PTA实验

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

  • 相关阅读:
    完整版excel上传导入读写批量数据并将反馈结果写入远程exel中
    将数据写入已有的excel文件
    微服务项目启动问题
    通过POI实现上传EXCEL的批量读取数据写入数据库
    [转] VLAN原理详解
    [转载]git tag — 标签相关操作
    [转载]SQLite3性能优化
    [转载]提升SQLite数据插入效率低、速度慢的方法
    [转载]sqlite3遇到database is locked问题的完美解决
    一个Linux下C线程池的实现(转)
  • 原文地址:https://www.cnblogs.com/fx8023/p/6681169.html
Copyright © 2020-2023  润新知