1. 本周学习总结

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(o)的返回值是否不小于0判断是否有这个对象,如果该对象或者元素存在,则indexOf方法返回其位置,如果不存在,则返回-1。

1.2 解释E remove(int index)源代码

public E remove(int index) {
   RangeCheck(index);
    modCount++;
   E oldValue = (E) elementData[index];
    int numMoved = size - index - 1;
    if (numMoved > 0)
    System.arraycopy(elementData, index+1, elementData, index,
             numMoved);
    elementData[--size] = null; // Let gc do its work
    return oldValue;
}

先判断是否越界,根据指定的位置删除元素,向左移动所有后续元素,再将长度减1。

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

不需要

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

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++;
    }
private void ensureCapacityInternal(int minCapacity) {  
        if (elementData == EMPTY_ELEMENTDATA) {  
            minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);  
        }  
  
        ensureExplicitCapacity(minCapacity);  
    }  

判断需要加入新元素的位置是否超过数组容量,如果超过,则先扩大数组容量,使其长度加1。

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

private void rangeCheck(int index) {
        if (index >= size) {
            throw new RuntimeException("range out");
        }
}

避免被外界访问与修改。

2.HashSet原理

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

保存到HashSet中的对象覆盖hashCode()和equals(),将对象加入到HashSet中时,会首先调用hashCode方法计算出对象的hash值,接着根据此hash值调用HashMap中的hash方法,得到要保存的位置,接着找到这个位置的对象,并调用equals方法比较这两个对象是否相等,如果相等则不添加,不相等就添加。

2.2 选做:尝试分析HashSet源代码后,重新解释1.1

3.ArrayListIntegerStack

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

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

jmu-Java-04-面向对象2-进阶-多态、接口与内部类中的自定义接口ArrayIntegerStack中的操作都是自己写的,而题集jmu-Java-05-集合之5-1中的ArrayListIntegerStack中用到list,可以直接使用list中的操作达到要求。

3.2 简单描述接口的好处.

面向接口编程可以对不同类用一个接口来实现方法。各个对象之间的关系是编程重要的一点,小到不同类之间的互通,大到各模块之间的交互,接口的重要性不言而喻。

4.Stack and Queue

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

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

	 public static  boolean judge(String a){
             ArrayListIntegerStack stack=new ArrayListIntegerStack();
	     char [] b=a.toCharArray();
             for (int i = 0; i < b.length; i++) {
                stack.push(b[i]);
              }
	     for(int i=0;i<b.length;i++)
	     {
	         if(b[i]!=stack.pop()){
	             return false;
	         }
	     }
         return true;
	 }
}

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

while(!a.isEmpty() || !b.isEmpty())
    {
        Integer x = a.poll();
        if(x != null){
            if(b.isEmpty() &&a.isEmpty())
            System.out.print(x);
            else 
                System.out.print(x+ " "); 
        }
        
        Integer y = a.poll();
        if(y != null){
            if(b.isEmpty() && a.isEmpty())
            System.out.print(y);
            else 
                System.out.print(y + " "); 
        }
        Integer z= b.poll();
        if(z != null){
            if(b.isEmpty() && a.isEmpty())
            System.out.print(z);
            else 
                System.out.print(z + " ");
        }
        
    }  

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

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

5.1 实验总结

public class Main {

	public static void main(String[] args) {
		 Set<String> set = new TreeSet();  
		
		 Scanner in = new Scanner(System.in);
		 while(in.hasNext()){
			 String s = in.next();
			 if (s.equals("!!!!!"))break;
			 set.add(s);
		 }
		 System.out.println(set.size());
		for (int i = 0; i < 10 ; i++) {
			System.out.println(set.toArray()[i]);
		}
	}
}

3. 码云上代码提交记录及PTA实验总结题目集:jmu-Java-05-集合

3.1. 码云代码提交记录

posted on 2017-04-08 20:11  一包辣条Zz  阅读(212)  评论(0编辑  收藏  举报