201521123113《Java程序设计》第7周学习总结

1. 本周学习总结

2. 书面作业

Q1.ArrayList代码分析

1.1 解释ArrayListcontains源代码

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源代码可见,比较时用到的是object。首先判断传入的object是否为空,否则无法调用equals方法。若o不为空,则遍历ArrayList用equals()方法比较返回一个boolean值。

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));
}

  • 首先 rangeCheck()判断index是否在范围内,如果没有则抛出IndexOutOfBoundsException异常;
  • 然后,对数组进行删除,把index后面的数组往前移一位。

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

  • ArrayList存储数据时不用考虑元素的类型,因为ArrayList用的是object类对象,而object类默认是所有类的父类,也就是说所有类型都可以用ArrayList。

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

  • add源代码中,ensureCapacityInternal(size + 1),当内部容量不够时,ArrayList就会增加容量,就是所谓的动态数组。

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

  • 我觉得在ArrayList.class里的许多方法都用到了rangeCheck()方法,在外部调用仅需要调用这些方法,没必要再调用一次rangeCheck()方法。rangeCheck()方法类似非常底层的方法,用户没必要去调用他。

Q2.HashSet原理

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

  • 散列表用链表数组实现,每个列表被称为桶。通过计算出待存储对象的散列码(hashCode),作为存储的位置,如果遇到冲突则用链表连接。
  • 初始情况下调用hashCode()方法计算对象的哈希码;如果桶中已有其他元素,则调用equals()方法与已有元素进行比较。

Q3.ArrayListIntegerStack

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

  • java06的ArrayListIntegerStack用的是ArraList来实现栈,java05的ArrayIntegerStack用的是数组来实现栈。
  • 在java06的ArrayListIntegerStack中,不需要判断栈满,因为ArrayList是动态数组,会自动扩充容量;不需要用到top指针。

3.2 简单描述接口的好处.

  • 一个类只能继承另一个类,而一个类可以接入多个接口;可以根据不同需求定义不同的方法;提高多人编程的效率。

Q4.Stack and Queue

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

import java.util.*;

public class Main113 {

 public static void main(String[] args) {
	 Scanner in = new Scanner(System.in);
	 while(in.hasNext()){
		 System.out.println(isPalindrome(in.nextLine()));
	 }
 }

 public static  boolean isPalindrome(String str){
  List<Character>  stack = new ArrayList<Character>();
  List<Character>  stack2 = new ArrayList<Character>();
  for(int i=0;i<str.length()/2;i++){
   stack.add(str.charAt(i));
   stack2.add(str.charAt(str.length()-i-1));
  }
  for(int i=str.length()/2-1;i>=0;i--){
   if(stack.remove(i) != stack2.remove(i)){
    return false;
   }
  }
  return true;
 }
}

结果

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

import java.util.*;

public class Six {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		Queue<Integer> List1 = new LinkedList<Integer>();
		Queue<Integer> List2 = new LinkedList<Integer>();
		int n = in.nextInt();
		for (int i = 0; i < n; i++) {
			int x = in.nextInt();
			if (x % 2 == 0) {
				List1.offer(x);
			} 
			else List2.offer(x);
		}
		while (!List1.isEmpty() || !List2.isEmpty()) {
			if(!List2.isEmpty()) 
				System.out.print(List2.poll()+" ");
			if(!List2.isEmpty()) 
				System.out.print(List2.poll()+" ");
			if(!List1.isEmpty()) 
				System.out.print(List1.poll()+" ");
		}
		}
	}

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

import java.util.*;

public class two {
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		TreeSet<String> str=new TreeSet<String>();
		while(in.hasNext()){
			String str1=in.next();
			if(str1.equals("!!!!!")) 
				break;
			str.add(str1);
		}
		System.out.println(str.size());	
			Object[] out = str.toArray();
			if(str.size()<=10)
				for (int i = 0; i < out.length; i++) {
					System.out.println(out[i]);
				}
			else for (int j = 0; j < 10; j++) {
				System.out.println(out[j]);
		}
	}
	
}

5.1 实验总结

  • 用TreeSet可以实现对str内的元素进行排序,不需要再用别的方法;
  • Object[] out = str.toArray()这句可以把str内的元素转成Object数组,好格式化输出形式。

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

posted @ 2017-04-08 20:35  冬冬冬  阅读(202)  评论(0编辑  收藏  举报