201521123093 java 第七周学习总结

1. 本周学习总结

2. 书面作业

1、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;
}
   答:contains()方法就是用来遍历Object里的数组,如果找到与标记相同的值则返回所在位置,否则返回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));
}
 答:这个代码大概就是在做一个删除元素,位置前移的一个操作。但是首先要先判断一下所传入的参数是否超出原来数组的长度,若超出则抛出异常
     否则将元素删除,后面的元素前移,原来最后元素的位置赋予null 。

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

 答:不需要,就像1.1所用的是Object下的数组,那就说明Object下所有的对象都可以使用。

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

 public boolean add(E e) {
	ensureCapacity(size + 1);  // Increments modCount!!
	elementData[size++] = e;
	return true;
    }

    /**
     * Inserts the specified element at the specified position in this
     * list. Shifts the element currently at that position (if any) and
     * any subsequent elements to the right (adds one to their indices).
     *
     * @param index index at which the specified element is to be inserted
     * @param element element to be inserted
     * @throws IndexOutOfBoundsException {@inheritDoc}
     */
    public void add(int index, E element) {
	if (index > size || index < 0)
	    throw new IndexOutOfBoundsException(
		"Index: "+index+", Size: "+size);  

	ensureCapacity(size+1);  // Increments modCount!!
	System.arraycopy(elementData, index, elementData, index + 1,
			 size - index);
	elementData[index] = element;
	size++;
    }
  答:当数组容量不够用时,会自动加长数组的长度。

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

/**
     * Checks if the given index is in range.  If not, throws an appropriate
     * runtime exception.  This method does *not* check if the index is
     * negative: It is always used immediately prior to an array access,
     * which throws an ArrayIndexOutOfBoundsException if index is negative.
     */
    private void RangeCheck(int index) {
	if (index >= size)
	    throw new IndexOutOfBoundsException(
		"Index: "+index+", Size: "+size);
    }

    /**
     * Save the state of the <tt>ArrayList</tt> instance to a stream (that
     * is, serialize it).
     *
     * @serialData The length of the array backing the <tt>ArrayList</tt>
     *             instance is emitted (int), followed by all of its elements
     *             (each an <tt>Object</tt>) in the proper order.
     */
    答:正如所贴代码中的解释,`RangeCheck`方法只是用来检测是否超出范围,这个只是内部的事情,外部也没用到,只
        需定义为private就可以了,不需要定义为public。

2、HashSet原理

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

  答:Set变量——HashSet对象——根据哈希算法计算出散列码,找到存储位置——根据相应的哈希码,找到对应的桶

3、ArrayListIntegerStack

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

  答:5-1使用list可以自动改动容量,而4-3使用的数组,规定了大小,要改容量的时候比较麻烦,而且需要使用到指针。

3.2 简单描述接口的好处.

   答:所谓接口,就是可以将两个毫不相干的两个东西连接在一起,以这题为例,它们都有IntegerStack接口,使用相同的方法名ArrayListIntegerStack,
       但是这两题所实现的方法不同,即用一个方法可以有不同的实现。

4、Stack and Queue

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

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

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

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

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

while(sc.hasNext()){
			String word = sc.next();
			if(word.equals("!!!!!")){
				break;
			}
			set.add(word);
		}
		System.out.println(set.size());
		for(int i =0;i<10;i++){
			System.out.println(set.toArray()[i]);
		}

5.1 实验总结

  答:刚开始没有头绪,不知道从哪开始,后面用treeset才知道原来这题的代码这么少。。。。
      treeset中本身就有排序的功能,所以就不需要再写排序的代码了。

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

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

7.2 使用集合类改进大作业
参考资料:
JTable参考项目

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

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

3.1. 码云代码提交记录

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

posted on 2017-04-08 20:28  赵铭  阅读(139)  评论(0编辑  收藏  举报

导航