zoukankan      html  css  js  c++  java
  • 201521123083 《Java程序设计》第7周学习总结

    1. 本周学习总结

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

    20150423000654004.png

    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;
        }//也没什么高级的招法。也就普通的遍历
    
    private transient Object[] elementData;
    

    1.2 解释E remove(int index)

     public boolean remove(Object o) {
            if (o == null) {
                for (int index = 0; index < size; index++)
                    if (elementData[index] == null) {
                        fastRemove(index);
                        return true;
                    }//如果要删除的数是NULL,那就寻找数组有没有null的元素,那就删除,传进位置调用fastRemove
            } else {
                for (int index = 0; index < size; index++)
                    if (o.equals(elementData[index])) {
                        fastRemove(index);
                        return true;
                    }//遍历有就删除
            }
            return false;//找不到
        }
    
    private void fastRemove(int index) {
            modCount++;//这里做个标记,百度了,跟线程安全有关系,这块不懂.
            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
        }//就是这个位置之前的不动,后面的像前面进一位
    
    

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

    不需要。因为数组里面的元素是object,一切父类的子类,根据多态,其他传什么进来都可以

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

     public boolean add(E e) {
            ensureCapacityInternal(size + 1);  // Increments modCount!!
            elementData[size++] = e;
            return true;
        }
    

    ensureCapacityInternal就是预防内部数组容量不够,继续看下去

      private void ensureCapacityInternal(int minCapacity) {
            if (elementData == EMPTY_ELEMENTDATA) {
                minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
            }//如果数组是空的时候,就要保证数组长度大于等于默认长度,如果小于,那就改成默认
    
            ensureExplicitCapacity(minCapacity);
        }
    
    
      private void ensureExplicitCapacity(int minCapacity) {
            modCount++;
    
            // overflow-conscious code
            if (minCapacity - elementData.length > 0)//这里就是晕原来数组长度不够的问题
                grow(minCapacity);
        }
    

    前面都是判断,,下面开始讲不够了怎么办

      private void grow(int minCapacity) {
            // overflow-conscious code
            int oldCapacity = elementData.length;
            int newCapacity = oldCapacity + (oldCapacity >> 1);//这里有意思,右移一位就是处于2,cpu中这样执行比较块
            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);
        }
    

    1.5 分析private void rangeCheck(int index)

    源代码,为什么该方法应该声明为private而不声明为public?

    因为这些不需要让客户端版知道,客户端只要需要用add 等具体实现就行了,至于这些向检测是否越界,怎么扩容,只需要在内部实现,,不需要让客户端知道

    2. HashSet原理

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

    抛开代码,个人理解,就是根据hashcode的值,加入不同位置,如果该位置已有值,那就判断是不是已经存在的值(==,根据物理地址),如果不是加入后面,否则不加入

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

     public boolean add(E e) {
            return map.put(e, PRESENT)==null;
        }
    
     public V put(K key, V value) {
            if (table == EMPTY_TABLE) {
                inflateTable(threshold);
            }
            if (key == null)
                return putForNullKey(value);
            int hash = hash(key);
            int i = indexFor(hash, table.length);
            for (Entry<K,V> e = table[i]; e != null; e = e.next) {
                Object k;
                if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
                    V oldValue = e.value;
                    e.value = value;
                    e.recordAccess(this);
                    return oldValue;
                }
            }
    
            modCount++;
            addEntry(hash, key, value, i);
            return null;
        }
    

    有上面可以大致看出来,hashSet是通过hashmap来实现的,
    HashSet 中的元素由 HashMap 的 key 保存。

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

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

    首先不用担心长度不够,其次不用栈顶指针。比如pop的话用ArrayList自带的remove就够了,不用自己实现,但是其实都一样,因为ArrayList内部的实现也差不多是我们实现的那些方法,只不过开发者封装给我们,我们直接用就罢了。

    3.2 简单描述接口的好处.

    比如上面那个IntegerStack,刚开始写代码的时候不用考虑要用数组实现还是ArrayList,以后项目可能有变更,我们直接把他抽象成接口,那么以后不管怎么变,我们只需要重新实现接口,而不需要改变原有代码。

    4. Stack and Queue

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

    package work;
    import java.util.Deque;
    import java.util.LinkedList;
    import java.util.Scanner;
    class Stack{
    	Deque<Character> stacklist=new LinkedList<>();
    	public void push(Character c) {
    		stacklist.offerLast(c);
    
    	}
    	public Character pop() {
    		return stacklist.pollLast();
    
    	}
    	public Character peek() {
    		return stacklist.peekLast();
    
    	}
    	@Override
    	public String toString() {
    		return stacklist.toString();
    	}
    	
    	
    }
    
    public class Main201521123083 {
    	private static boolean ishuiwen(String str) {
    		Stack myStack=new Stack();
    		for (int i = 0; i < str.length(); i++) {
    			myStack.push(str.charAt(i));
    		}
    		for (int i = 0; i < str.length(); i++) {
    			if(str.charAt(i)!=myStack.pop())
    				return false;
    				break;
    		}
    		return true;
    	}
    	public static void main(String[] args) {
    		Scanner sc=new Scanner(System.in);
    		System.out.println(ishuiwen(sc.next()));
    	}
    		
    }
    

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

    这题其实就是用两个队列来处理的。然后分别出列,一个出两个,一个出一个

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

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

    直接hashset,hashset默认就是按单词的字母顺序排序的

    6. 选做:加分考察-统计文字中的单词数量并按出现次数排序

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

    这题就是map按照value进行排序的题目。如果是按照key进行排序,还比较块,value排序的思路是先将map中的key-value放入list,然后用Collections.sort对list排序,再将排序后的list放入LinkedHashMap,最后返回LinkedHashMap。

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

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

    图片.png
    新增JTable。一些数据用评论改用Map.

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

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

    3.1. 码云代码提交记录

    图片.png

    3.2. PTA实验

    编程(5-1, 5-2, 5-3(选做), 5-6)

    实验总结已经在作业中体现,不用写

  • 相关阅读:
    153. Find Minimum in Rotated Sorted Array
    228. Summary Ranges
    665. Non-decreasing Array
    661. Image Smoother
    643. Maximum Average Subarray I
    4.7作业
    面向对象编程
    常用模块3
    3.31作业
    常用模块2
  • 原文地址:https://www.cnblogs.com/daikersec/p/6682857.html
Copyright © 2011-2022 走看看