201521123118《java程序与设计》第七次作业

1. 本周学习总结

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

1109877-20170408150642582-1769431784.png

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;
    }

由源代码可以看出,contains方法中实现了另一个方法indexOf,仔indexOf中通过equals的方法依次和数组elementData数组进行比较,如果有相等的,则返回当前位置,在通过和contains 与数字0比较,返回一个boolean型。
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;
    }
    public boolean remove(Object o) {
        if (o == null) {
            for (int index = 0; index < size; index++)
                if (elementData[index] == null) {
                    fastRemove(index);
                    return true;
                }
        } else {
            for (int index = 0; index < size; index++)
                if (o.equals(elementData[index])) {
                    fastRemove(index);
                    return true;
                }
        }
        return false;
    }
   private void rangeCheck(int index) {
        if (index >= size)
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }
  private void fastRemove(int index) {
        modCount++;
        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
    }

rangCheck是范围检查,如果清除的地址超过数组大小,则会抛数组下标越界的异常。把数组下标读出来,再把后面的元素向前移,然后返回被删的元素,并且size-1=null。
1.3 结合1.1与1.2,回答ArrayList存储数据时需要考虑元素的类型吗?
不需要,将类型添加到ArrayList中都会变成object型,因为object是所有类的父类,所以只是不是基本类型,都可以存到ArrayList中
1.4 分析add源代码,回答当内部数组容量不够时,怎么办?


public boolean add(E e) {
    ensureCapacityInternal(size + 1);  // Increments modCount!!
    elementData[size++] = e;
    return true;
}
public void add(int index, E e) {
            rangeCheckForAdd(index);
            checkForComodification();
            parent.add(parentOffset + index, e);
            this.modCount = parent.modCount;
            this.size++;
        }
 

都会检查内部数组的容量是否不够了,如果是,它就会以当前容量的两倍来重新构建一个数组,将旧元素Copy到新数组中,然后丢弃旧数组,在这个临界点的扩容操作,应该来说是比较影响效率的。

1.5 分析private void rangeCheck(int index)源代码,为什么该方法应该声明为private而不声明为public?
这是一个封装的问题,在判断是否数组越界,是在内部进行的,不需要引用到外部,所以直接用private就行。

2. HashSet原理

2.1 将元素加入HashSet(散列集)中,其存储位置如何确定?需要调用那些方法?
用Hash算法算出一个Hash(哈希)码值返回,HashSet会用Hash码值去和数组长度取模,模(这个模就是对象要存放在数组中的位置)相同时才会判断数组中的元素和要加入的对象的内容是否相同,如果不同才会添加进去。存入HashSet的集合对象中的自定义类必须覆盖hashCode(),equals()两个方法,才能保证集合中元素不重复。
2.2 选做:尝试分析HashSet源代码后,重新解释1.1

3. ArrayListIntegerStack

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


  private List<Integer> list;
    public ArrayListIntegerStack(){
        list = new ArrayList<Integer>();
    }

private Integer[] a;
    public ArrayIntegerStack(int x){
        a=new Integer[x];
    }

首先就是数组的区别,动态数组不需要定义一个数组的大小,不用担心越界。
ArrayListIntegerStack中对方法都可以用到源代码中的方法,这样写起来很方便,而自定义接口需要自己处理对数组的增加删除,位置移动,较为复杂。
3.2 简单描述接口的好处.
相同的方法,不同的实现。

4. Stack and Queue

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

class ArrayListStringStack{
    public List stack;
    public ArrayListStringStack(){
        this.stack=new ArrayList();
    }
    public char push(char o) {
        // TODO Auto-generated method stub
        stack.add(o);
        return  o;
    }
    public char pop() {
        // TODO Auto-generated method stub
            return (char) stack.remove(stack.size()-1);
    }

}
public class Main201521123118{
    public static void main(String[] args){
        Scanner sc= new Scanner(System.in);
        String str=sc.next();
        System.out.println(isPalindromeNumber(str));
}

    private static boolean isPalindromeNumber(String str) {
        // TODO Auto-generated method stub
        ArrayListStringStack mystack=new ArrayListStringStack();
        char[] ch=str.toCharArray();
        for(int i =0;i<str.length();i++){
            mystack.push(ch[i]);
        }
        for(int j =0;j<str.length();j++){
            if(ch[j]!=mystack.pop())
                return false;
        }
        return true;
    }
}

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

    while(!a.isEmpty() || !b.isEmpty()){
        if(!a.isEmpty()){
            if(flag<2){
                System.out.print(a.poll()+" ");
                flag++;
            }
            else{
                System.out.print(b.poll()+" ");
                flag=0;
            }   
            }
        else
            System.out.print(b.poll()+" ");
        }
    }

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

5.1 实验总结
通过TreeSet进行排序在输出。

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

1109877-20170408195319597-1987694043.png

转载于:https://www.cnblogs.com/zjwl/p/6682814.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值