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


    1. 本周学习总结

    2. 书面作业

    Q1.ArrayList代码分析

    1.1 解释ArrayList的contains源代码

    A:先上源代码:

    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的实现即遍历一次ArrayList,若equals,则返回序号,当然,要判断o是否为null,加入o为null就没有equals方法。

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

    A:先检查要删除的位置如果超出List大小,就抛出IndexOutOfBoundsException异常。大概流程就是类似数组删除操作,删掉某个元素后,其位置之后的元素前移,最后再把size-1位置赋null让GC clear it。

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

    A:不用考虑,因为其参数为Objcet类型的对象,所有对象皆为Object。但是我们最好在定义的时候要明确一个存储的对象类型,如ArrayList

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

    A:直接用注释说明。

    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倍的新数组。
    }  
    
    1.5 分析private void rangeCheck(int index)源代码,为什么该方法应该声明为private而不声明为public?
    private void rangeCheck(int index) {
        if (index >= size)
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }  
    

    A:这么做的目的显然是不想让外部使用这个方法,我觉得主要原因就是外部没必要用这个方法吧。rangeCheck方法保证了remove等操作需在容量范围内。既然其作用是这样,假如某些语句让操作范围越界了,就会直接抛出异常,也就是说我们知道rangeCheck的结果了,那为什么还要声明为public呢?

    Q2.HashSet原理

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

    HashSet的存储形式用链表数组实现---〉每个列表被称为桶。

    1.当我们向HashSet中添加一个元素时,HashSet会先调用该对象的hashCode()方法得到其hashCode值,根据该值决定该对象在桶中存储位置。

    2如果桶中已有其他元素,则调用加入对象的equals()方法与已有元素进行比较。如果比较结果为假,则将对象插入桶中。如果比较结果为真,则用新的值替换旧的值。

    但是,如果有两个元素通过equals()方法比较返回true,而它们的hashCode()方法返回值不等,HashSet也会将它们存储在不同的位置。

    Q3.ArrayListIntegerStack

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

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

    A:主要就是内部实现数组与动态数组的区别,ArrayIntegerStack实例化的时候需要规定大小,而ArrayListIntegerStack使用list可以自动扩容。此外,ArrayListIntegerStack不需要使用指针,可以直接通过list的一些方法来进行删除,查找等操作。

    public ArrayIntegerStack(int n) {
    	this.arr = new Integer[n];
    }
    

    public ArrayListIntegerStack(){
    	this.list = new ArrayList<Integer>();
    }
    
    3.2 简单描述接口的好处.

    :相同方法,不同实现。正如ArrayListIntegerStack与ArrayIntegerStack。 即可以让我们根据需求,通过不同方式去实现同一个目的。

    Q4.Stack and Queue

    4.1 编写函数判断一个给定字符串是否是回文,一定要使用栈,但不能使用java的Stack类(具体原因自己搜索)。请粘贴你的代码,类名为Main你的学号。
    public class Main201521123076 {
    public static void main(String[] args) {
    	Scanner in = new Scanner(System.in);
    	String str = in.next();
    	System.out.println(isPalindromeNumber(str));
    }
    
    public static boolean isPalindromeNumber(String str){
    	ArrayListStringStack myStack = new ArrayListStringStack();
    	char[] charArray = str.toCharArray();
    	for (int i = 0; i < charArray.length / 2 ; i++) {
    		myStack.push(String.valueOf(charArray[i]));   //char是基本类型,所以这里要把其转化成String
    	}
    	for(int i = charArray.length / 2 ; i <  charArray.length; i++ ){
    		if( !myStack.pop().equals(String.valueOf(charArray[i])) ) return false;
    	}
    	return true;
    }
    }
    

    这里用到的ArrayListStringStack就是5-1中自己所写的栈,只不过把Integer全部换成String。

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

    A,B两个队列按要求依此出队即可,主要是要判断哪个元素是最后一个poll的(a1,a2,b)

    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 + " ");
    		}
    		
    	}  
    

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

    题集jmu-Java-05-集合之5-2 统计文字中的单词数量并按单词的字母顺序排序后输出 (不要出现大段代码)
     if(set.size() < 10)
    		for(String str : set){
    			System.out.println(str);
    		}
    	 else 
    	 {	 
    		for (int i = 0; i < 10 ; i++) {
    		System.out.println(set.toArray()[i]);
    	}
    
    5.1 实验总结

    A:由于题目要求要按字母顺序输出,所以用TreeSet就会默认帮我们排好序,最后按要求输出一下即可。

    选做:加分考察-统计文字中的单词数量并按出现次数排序

    题集jmu-Java-05-集合之5-3 统计文字中的单词数量并按出现次数排序(不要出现大段代码)
    6.1 伪代码
    while(in.hasNext())
    	 if(map.get(s) == null)
    		map.put(s, 1);
    	  else 
    		map.put(s, map.get(s)+ 1);
    //将单词及其出现次数置入HashMap
    
    List<Entry<String,Integer>> list = new ArrayList<Entry<String,Integer>>(map.entrySet());
    Collections.sort(list,new Comparator(){});
    for (int i = 0; i < 10; i++) {
    	System.out.println(list.get(i));
    }
    
    6.2 实验总结

    A:map没有根据value排序的方法,所以要转为list排序,后输出即可。

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

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

    新增用户注册与登入验证功能,使用map储存用户名密码至本地文件。

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

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

  • 相关阅读:
    stagefright框架 Video Playback的流程
    ubuntu 10.10 安装 无线网卡驱动
    ffmpeg 播放 m3u8 ts 流时 av_read_frame 流程
    错误:expected classname before ‘{’ token
    avcodec_decode_video2 第三个参数 got_picture_ptr 的含义
    ndk 编译 ffmpeg
    Windows Phone 7中用好Silverlight开发利器
    利用Visual Studio 2010流程模板实现Scrum敏捷开发(下)
    VS2010中使用IntelliTrace来进行调试
    在Windows Azure中实现和调试一个WCF服务(下)
  • 原文地址:https://www.cnblogs.com/slickghost/p/6679221.html
Copyright © 2020-2023  润新知