201521123068 《java程序设计》 第7周学习总结

1. 本周学习总结

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

2. 书面作业

1.ArrayList代码分析
1.1 解释ArrayList的contains源代码

//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的boolean值,indexOf()方法判断o是否为null,是则直接返回序号i,否则返回-1。

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

   //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));
   }
   答:index后面的数据都向前移位,实现从指定的位置删除元素。第一行检验index的返回,如果index为负数,可以执行,但是会抛出java.lang.ArrayIndexOutOfBoundsException:-1的数组越界异常信息。第三行代码进行移动量的计算,要删除的元素不是最后一位则进行移动,移动后的数组末尾赋值null,注释中GC对它实行回收操作。

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

答:不需要,Object类是父类,而ArrayList是一个Object数组,所以ArrayList可以存储任何数据元素类型。

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

//Add源代码
public boolean add(E e) {
ensureCapacityInternal(size + 1);  // Increments modCount!!
elementData[size++] = e;
return true;
}
答:Add()方法返回值恒为true,此代码块确定ArrayList容量是否可以再加1。
private void ensureCapacityInternal(int minCapacity) {
    if (elementData == EMPTY_ELEMENTDATA) {
    minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
    }
    ensureExplicitCapacity(minCapacity);
    }
答:ensureCapacityInternal()方法,判断当前的对象数组是否默认是默认的空数组,如果是就在默认容量10和需要的容量中取一个最大值,把这个最大值传递给下一个函数ensureExplicitCapacity。   
private void ensureExplicitCapacity(int minCapacity) {
    modCount++;// overflow-conscious code
    if (minCapacity - elementData.length > 0)
    grow(minCapacity);
}
答:如果当前的数组长度小于我们需要的minCapacity值,即当前数组长度不够,则进行扩容,执行grow(minCapacity)。
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);  
}  
答:让oldCapacity等于当前数组的长度, int newCapacity = oldCapacity + (oldCapacity >>1);代码使得newCapacity=1.5*oldCapacity。

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

//private void rangeCheck(int index)源代码
private void rangeCheck(int index) {
    if (index >= size)
    throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
答:外部类不需要调用rangeCheck()方法,因此使用private将此方法封装。

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

答:将元素加入HashSet(散列集)中,HashSet先调用元素的HashCode()方法得到元素的哈希值,如果元素想要储存的位置为空,直接将此元素储存到该位置上;如果该位置已有其他元素,调用equals()方法比较,如果equals返回true即是重复元素不再储存,如果equals返回false将该元素储存起来。据此可知,需要调用HashCode()方法和equals()方法。

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

答:ArrayListIntegerStack是ArrayList实现的,ArrayList在数组空间为满后会自动扩容,ArrayIntegerStack是通过数组实现,要注意数组的大小。

3.2 简单描述接口的好处。

一个接口实际上是描述了一种规范,它只说明了这个接口需要实现什么需求,而没有强制规定这个需求如何实现。

因此对于接口来说,不同的实现过程可以有相同的实现结果。

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

package HUIwen;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

//定义IntegerStack接口(同05集合5-1)
interface IntegerStack{
   public Integer push(Integer item);
   public Integer pop();
   public Integer peek();
   public boolean empty();
   public int size();
}

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

@Override
public Integer push(Integer item) {
   if(item==null){
   return null;
   list.add(item);
   return item;}
   
@Override
public Integer pop() {
   if(list.size()==0)
      return null;
   return list.remove(list.size()-1);}
   
@Override
public Integer peek() {
   if(list.size()==0)
	   return null;
   return list.get(list.size()-1);
}

@Override
public boolean empty() {
	return list.size()==0?true:false;
}

@Override
public int size() {
	return list.size();
}

@Override
public String toString() {
	return  list.toString();
}
}

//Main方法
import java.util.Scanner;
import test1.ArrayIntegerStack;
public class Main201521123068 {
public static void main(String[] args) {
	IntegerStack arrayIntegerStack=new ArrayListIntegerStack();
	Scanner str=new Scanner(System.in);
    System.out.println("输入字符串:");
    while(str.hasNext()){
    	String s=str.next();
    	System.out.println(isPalindrome(s));
    }
}  
	public static boolean isPalindrome(String s) {
	// TODO Auto-generated method stub
	   if(s.length()<=1){
	     return true;
		}
	   else if(s.charAt(0)!= s.charAt(s.length()-1)){
         return false;
	   }
		 return isPalindrome(s.substring(1,s.length()-1));
}

运行结果:

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

答:队列要先判断是否为空,缺少此步骤导致null的重复输出。

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

答:想法是做一个SortedMap,这个Map的Comparator接口是根据字母顺序排列的规则,用space做分格放到Map中,Word是这个单词,count是单词出现的次数。

7.面向对象设计大作业-改进
7.1 完善图形界面(说明与上次作业相比增加与修改了些什么)
7.2 使用集合类改进大作业
参考资料:
JTable参考项目

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

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

3.1. 码云代码提交记录

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

3.2. PTA实验

  • 编程(5-1, 5-2, 5-3(选做), 5-6)
  • 实验总结已经在作业中体现,不用写。
原文地址:https://www.cnblogs.com/bmr666/p/6681073.html