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

    1. 本周学习总结

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

    2. 书面作业

    ArrayList代码分析
    1.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。

    1.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。

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

    答:不需要。从1.1和1.2的源代码中可以看到其传入的对象是object,Object为一个顶级父类,它是任何类父类,所以说ArrayList存储数据时不用考虑元素类型。
    

    1.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);
    }   
    答:当内部数组容量不够时,首先ensureCapacityInternal方法得到最小扩容量,其次ensureExplicitCapacity判断是否要扩容。

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

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

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

    答:计算出带存储的散列码作为存储的位置依据,将对象存入Hash类集合

      要调用equals和hashCode方法

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

    private ArrayList<Integer> list;
        public ArrayListIntegerStack(){
            list=new ArrayList<Integer>();//使用了Arraylist
        }
     public Integer push(Integer item) {
            // TODO Auto-generated method stub
            if(item==null)
            return null;
            list.add(item);//用到了add方法
            return item;
        }
    public Integer pop() {
            // TODO Auto-generated method stub
            if(list.size()==0)
            return null;
             return list.remove(list.size()-1);//用到remove方法         
        }


    
    
    04-5-3中的ArrayListIntegerStack

    private
    Integer[]arr;//使用数组
    private int top=0;//top指针 public ArrayIntegerStack(int capacity) { arr=new Integer[capacity]; } public Integer pop(){ if(top==0) return null; top--;//出栈需要移动指针 return arr[top]; }
    答:在5-1中编写的ArrayListIntegerStack使用的是ArrayList数组来存放对象,因此相应的push、pop、peek等方法的编写用到了list中的add、remove等方法;5-3中则是用数组来存放对象,并且要有top指针。
    

    3.2 简单描述接口的好处.

    答:根本区别就是一个内部使用数组实现,一个内部使用ArrayList实现,数组的话增删查改都要自己编写方法,ArrayList可以调用已有方法进行操作。

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

    public class Main201521123088 {
        public static void main(String[] args) {
            // TODO Auto-generated method stub
        Scanner sc=new Scanner(System.in);
        String x=sc.next();
        ArrayListStringStack list=new ArrayListStringStack();
        ArrayListStringStack list1=new ArrayListStringStack();
        char [] arr = x.toCharArray();
        for(int i=0;i<arr.length;i++){
            list.push(String.valueOf(arr[i]));
        }
        int q=arr.length/2;
        int k=arr.length%2;
        if(k==0){
            for(int y=1;y<=q;y++){
                list1.push(list.pop());
            }
             for(int b=0;!list.empty()&&!list1.empty();b++){
                    if(!list.pop().equals(list1.pop())){
                        System.out.println("该字符串不是回文");
                        break;
                    }
                    if(list.empty()&&list1.empty()){
                        System.out.println("该字符串是回文");
                    }
                }
        }
        else{
            
            for(int p=1;p<=q;p++){
                list1.push(list.pop());
                
            }  
            list.pop();
             for(int b=0;!list.empty()&&!list1.empty();b++){
                    
                    if(!(list.pop().equals(list1.pop()))){
                        System.out.println("该字符串不是回文");
                        break;
                    }
                    if(list.empty()&&list1.empty()){
                        System.out.println("该字符串是回文");
                        break;
                    }
                }
         }   
        }    
        }

    运行结果:


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

    for (int i = 0; i < n; i++) {
                int number = in.nextInt();
                if (number % 2 == 0) B.add(number);
                else A.add(number);
            }
    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 + " ");
                }
                
            }

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

    答:用TreeSet的add方法将指定的元素添加到此 set(如果该元素尚未存在于 set 中)。更确切地讲,如果该 set 不包含满足 (e==null ? e2==null : e.equals(e2)) 的元素 e2,则将指定元素 e 添加到此 set 中。如果此 set 已经包含这样的元素,则该调用不改变此 set 并返回 false。且就会默认帮我们排好序,最后按要求输出一下即可。

    面向对象设计大作业-改进
    7.1 完善图形界面(说明与上次作业相比增加与修改了些什么)

    7.2 使用集合类改进大作业

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

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

    3.1. 码云代码提交记录

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

  • 相关阅读:
    JavaScrip中构造函数、prototype原型对象、实例对象三者之间的关系
    (字符缓冲流)文本排序案例
    Annotation注解的应用(打印异常信息)
    Annotation(注解)
    Java关键技术强化
    基本数据类型与引用数据类型的区别
    EKT反射
    bootstrap的概念
    Servlet强化
    java数据库连接池
  • 原文地址:https://www.cnblogs.com/lzy-mini/p/6682815.html
Copyright © 2011-2022 走看看