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

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是一个用来判断某个ArrayList中是否包含某个对象,如果该对象存在,则indexOf返回一个大于0的数,如果不存在,则返回-1,如果indexOf方法返回的数是一个大于0的数,则contains返回true,如果返回的是-1,则contains方法返回false。

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;
    }
  • 先判断删除位置是否越界,当numMove大于零的时候将index+1移动到 index的位置,将list末尾元素置空(null),返回被移除的元素。

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

  • 不用考虑。因为其在操作的时候都是Object类的对象,Object类是其他所有类的父类。

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++;
    }
  • 判断加入的元素是否超过数组容量,如果超过,则扩大数组容量。

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

  • 因为避免外部访问,private即把他设为私有,因为rangeCheck是内部方法,所以要保证封装性,不用public声明。

2.HashSet原理

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

  • 当从HashSet中访问元素时,HashSet先计算该元素的hashCode值,然后到该hashCode对应的位置取出该元素。
  • hashCode方法,equals方法。

3.ArrayListIntegerStack

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

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

  • 存储方式不同,前者使用ArrayList,后者使用Integer数组。很显然前者不需要定义长度,后者需要定义长度,而且还需要使用指针操作。

3.2 简单描述接口的好处.

  • 接口定义了方法,当某个类中需要这种方法就可以通过调用该接口来使用接口中的方法,使用接口可以使程序结构清晰,各个类之间的关系很明显。

4.Stack and Queue

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

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

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

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

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

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

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

while (in.hasNext()) {
		String str = in.next();
		if(str.equals("!!!!!"))
			break;
		else if (!strSet.contains(str))
			strSet.add(str);
	}
	System.out.println(strSet.size());
	strSet.toArray();
	if (strSet.size() <= 10)
		for (String s : strSet) {
			System.out.println(s);
		}
	else
		for (String s : strSet) {
			if (m-- <= 0)
				break;
			System.out.println(s);
		}
}

3.码云提交记录

原文地址:https://www.cnblogs.com/xjn12138/p/6682988.html