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


    1. 本周学习总结

    2.书面作业

    Q1.ArrayList代码分析

    1.解释ArrayList的contains源代码

    源代码:

    //contains()方法
     public boolean contains(Object o) {
         return indexOf(o) >= 0; 
    } 
    //indexOf()方法 
    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; }
    
    
    分析:在indexOf()方法中对传进来的值o判断是否为null,因为值o若为null,则不可以使用equals进行比较。如果找到了值o,则返回对应的数组下标;如果满意找到,则返回-1,那么contains()方法就会返回false。
    

    2.解释E remove(int index)源代码

    源代码:
    //remove代码
    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));
    }
    
    分析:首先在remove()方法中调用rangeCheck()方法,是用来判断传入的index是否超过size,若超过,则抛出IndexOutOfBoundsException异常。然后这个remove()方法就是将对应下标的元素取出,再将后面的元素全部往前移一位,将最后一位,即size-1的位置置null。
    

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

    不需要;因为其参数为object类型的对象,而object类是所有类的父类,所以说无论元素是什么类型,ArrayList都可以存储。
    

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

    源代码:
    //add代码
    public boolean add(E e) {
          ensureCapacityInternal(size + 1); 
          elementData[size++] = e;
          return true;
    //ensureCapacityInternal代码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);
    }   
    }
    
    //grow代码
     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);
    }   
    
    分析:首先在add()方法中调用ensureCapacityInternal(),用来调整List的容量;如果超过了容量,则要调用grow()方法,int newCapacity = oldCapacity + (oldCapacity >> 1);就是增加原来容量的一半,即新容量为为旧容量的1.5倍; elementData = Arrays.copyOf(elementData, newCapacity);将旧数组中的数据拷贝至新数组中,说明增加容量不是在旧数组的基础上增加的,而是直接引用了一个全新的数组。
    

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

    源代码:
    private void rangeCheck(int index) {
        if (index >= size)
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }  
    
    分析:因为rangeCheck()方法只需要判断数组是否越界,没有其他的作用,外部不需要对它进行修改或者访问,所以说声明应为private。
    

    Q2.HashSet原理

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

    HashSet的元素排列没有顺序。加入时,先调用hashcode()方法查找相应的桶,如果桶中有其他元素则调用equals()方法进行比较,结果为假即桶中不存在相同元素时,直接插入,否则用新的替换旧的值。
    

    Q3.ArrayListIntegerStack

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

    集合5-1中的ArrayListIntegerStack接口采用的是list列表来存储栈元素,而5-3采用的是数组的形式。采用列表进行存储不需要固定的长度,不用定义top来指向栈的顶部,入栈时不用判断栈是否为空,且列表的类库中已有很多方法可供调用。
    

    2.简单描述接口的好处

    一个接口只有方法的特征没有方法的实现,因此这些方法可以在不同的地方被不同的类实现,而这些实现可以具有不同的功能。如 IntegerStack中的peek,push,pop方法在ArrayListIntegerStack中实现时可根据存储方式的不同,而以不同的形式实现这些方法。
    

    Q4.Stack and Queue

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

    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() {
        if(list.isEmpty())
            return null;
        return list.remove(list.size()-1);
        }
    }
    public class Main201521123117 {
        public static void main(String[] args) {
            Scanner in=new Scanner(System.in);              
            String str=in.next();
            int len=str.length()/2;
            String flag="true";
            StringStack stack1=new ArrayListStringStack();
            StringStack stack2=new ArrayListStringStack();
            for(int i = 0;i<len;i++){
                stack1.push(String.valueOf(str.charAt(i)));
                stack2.push(String.valueOf(str.charAt(str.length()-i-1)));
            }
            for(int j=len-1;j>=0;j--){
                if(!(stack1.pop().equals(stack2.pop()))){
                    flag="flase";
                    break;
                }
            }
            System.out.println(flag);
        }
    }
    

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

    Queue<Integer>A=new LinkedList<Integer>();
    Queue<Integer>B=new LinkedList<Integer>();
    Queue<Integer>C=new LinkedList<Integer>();
    for(i = 0; i < n; i++){
        int temp=in.nextInt();
        if((temp%2)!=0)
            A.add(temp);
        else
            B.add(temp);
    }
    for(i=0;i<n;i++){
        for(int j=0;j<2;j++){
            if(!A.isEmpty())
                C.add(A.poll());
        }
        if(!B.isEmpty())
            C.add(B.poll());
    }
    for(i=0;i<n-1;i++)
        System.out.print(C.poll()+" ");
    System.out.print(C.poll());
    

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

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

    Set<String> set = new TreeSet();     //TreeSet可以确保集合元素处于排序状态
    
    while(in.hasNext()){
        String s = in.next();
        if (s.equals("!!!!!"))break;
            set.add(s);
    }                                    //add添加新单词到表尾
    System.out.println(set.size());      //size统计表长即单词数
    for循环输入
    

    实验总结:

    TreeSet是SortedSet接口的唯一实现类,TreeSet可以确保集合元素处于排序状态。TreeSet支持两种排序方式,自然排序 和定制排序,其中自然排序为默认的排序方式。向TreeSet中加入的应该是同一个类的对象。
    TreeSet判断两个对象不相等的方式是两个对象通过equals方法返回false,或者通过CompareTo方法比较没有返回0。
    

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

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

    添加结算模块
    

    2.使用集合类改进大作业

    public class ShoppingCart implements Search{
        ArrayList<Good> car = new ArrayList<Good>();
    
        public void add(Good c)
        {
            this.car.add(c);
        }
        .......
    }
    

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

  • 相关阅读:
    ST (Sparse Table:稀疏表)算法
    P3379 【模板】最近公共祖先(LCA)
    AT1357 n^p mod m(洛谷)
    poj2018 Best Cow Fences
    P1024 一元三次方程求解
    poj2456
    poj1064
    P2047 [NOI2007]社交网络(洛谷)
    poj1734
    洛谷P2886 [USACO07NOV]牛继电器Cow Relays
  • 原文地址:https://www.cnblogs.com/llxyy/p/6682850.html
Copyright © 2020-2023  润新知