1. 本周学习总结

201521123061 《Java程序设计》第七周学习总结

2. 书面作业


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;

由代码可以看到contain方法是通过调用indexof方法来判断是否包含,indexof方法是先判断是否为空,再通过遍历数组中所有成员来查看是否含有目标对象,如果含有目标对象返回此目标的下标,这时contain中得到一个大于零的数返回true表示存在;没有则返回-1,此时indexof返回一个小于零的数,contain返回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;
}

先判断是否越界,然后计算移除后要往前移动的元素个数(numMoved),当numMove大于零的时候将index+1移动到 index的位置;因为删除了一个元素所以numMoved的表达式中要减一,而elementData是一个缓存数组通常会预留一些容量所以要把没用到的置为null。

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

不需要,ArrayList中定义的是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++;
}

 private void ensureCapacityInternal(int minCapacity) {  
        if (elementData == EMPTY_ELEMENTDATA) {  
            minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);  
        }  
  
        ensureExplicitCapacity(minCapacity);  
    }  

ensureCapacityInternal会判断是否要扩容

1.5 分析private voidrangeCheck(intindex)源代码,为什么该方法应该声明为private而不声明为public?

避免被外界访问,当调用了rangeCheck(int index)时,就会在数组越界时发出提醒,而且这是一种内部方法没必要声明成Public


HashSet原理

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

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

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

使用HashSet中的contain方法那和List中的contain方法的速度简直不可同日而语,

        public boolean contains(Object o) {  
        return map.containsKey(o);  
      }

List中是遍历而HashSet是利用hashCode快速查找到并利用equals进行比较,当比较值为真则覆盖原来的值若是假则插入到hash桶中。

ArrayListIntegerStack

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

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

在ArrayListIntegerStack中内部数组是无需定义长度的自动扩容,而在ArrayIntegerStack中要规定数组长度大小;而且list的方法更便捷并不用像ArrayIntegerStack中要利用指针进行添加删除操作。

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

public ArrayListIntegerStack(){
	list=new ArrayList<Integer>();
	}

public Integer push(Integer item) {
  if(item == null)
	return null;
	if(top == stack.length)
		return null;
	stack[top++]= item;
	return item;
}

public Integer push(Integer item) {
	if(item==null) 
		return null;
	list.add(item);
	return item;
}

3.2 简单描述接口的好处.

接口定义了方法,当某个类中需要这种方法就可以通过调用该接口来使用接口中的方法。

Stack and Queue

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

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

 public static boolean isHuiWen(String text){
	 ArrayListIntegerStack stack=new ArrayListIntegerStack();
	 /
	  *
	  *ArrayListIntegerStack为本周实验5-1中定义过的栈
	  */
	  
	 char []b=text.toCharArray();
	 int length=b.length ;
	 for(int i=0;i<length/2;i++)
	 {
		 if(b[i]!=b[length-i-1]){
			 return false;
		 }
		 return true;
	 }
 }

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

建立两个队列A和B,将放在数组里的数字根据奇数偶数分别加入到队列A与B中,再根据题意输出

    Queue<Integer> A= new ArrayDeque<Integer>();
	Queue<Integer> B= new ArrayDeque<Integer>();

     if(nums[j]%2==0){
    		   B.add(nums[j]);
    	   }
    	   else A.add(nums[j]);

    Integer Anum1 = A.poll();
		if(Anum1 != null){
			if(B.isEmpty() && A.isEmpty())
			System.out.print(Anum1);
			else 
				System.out.print(Anum1 + " ");	
		}
		
		Integer Anum2 = A.poll();
		if(Anum2 != null){
			if(B.isEmpty() && A.isEmpty())
			System.out.print(Anum2);
			else 
				System.out.print(Anum2 + " ");	
		}

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

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


	Set<String> strset=new TreeSet<String>();

	for(i=0;;i++){
		str=in.next();
		if(str.equals("!!!!!")) break;
		else strset.add(str);
	}

	for(String s:strset){
		x=x+1;
		System.out.println(s);
		if(x==10) break;
	}

5.1 实验总结

TreeSet会默认按顺序把成员排序,只需将元素加入到set中就Ok了。

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

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

201521123061 《Java程序设计》第七周学习总结
201521123061 《Java程序设计》第七周学习总结

7.2 使用集合类改进大作业

利用Set储存用户的信息就不会重复。

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

题目集:jmu-Java-05-集合
3.1. 码云代码提交记录
201521123061 《Java程序设计》第七周学习总结
3.2. PTA实验

编程(5-1, 5-2, 5-3(选做), 5-6)
实验总结已经在作业中体现,不用写。

相关文章:

  • 2021-05-16
  • 2021-10-26
  • 2022-03-07
  • 2022-02-23
  • 2021-06-03
  • 2021-12-02
  • 2021-07-18
  • 2021-07-10
猜你喜欢
  • 2021-06-06
  • 2022-02-22
  • 2022-01-10
  • 2021-11-08
  • 2021-08-27
  • 2022-01-23
  • 2021-07-10
相关资源
相似解决方案