201521123024《Java程序设计》第7周学习总结
2017-04-08 19:30
148 查看
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; }
先判断o是否为null,若o为null就没有equals方法;contains的作用是遍历一次ArrayList,若equals,则返回序号。
1.2 解释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)); }
删掉List中某个元素,其之后的元素前移
1.3 结合1.1与1.2,回答ArrayList存储数据时需要考虑元素的类型吗?
不需要,因为其参数为Objcet类型的对象,所有对象皆为Object1.4 分析add源代码,回答当内部数组容量不够时,怎么办?
public boolean add(E e) { ensureCapacityInternal(size + 1); // Increments modCount!! elementData[size++] = e; return true;
private void ensureCapacityInternal(int minCapacity) { if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) { minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity); } ensureExplicitCapacity(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); 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); }
如果容量超了则调用函数grow增加容量。
1.5 分析private void rangeCheck(int index)
源代码,为什么该方法应该声明为private而不声明为public?
private void rangeCheck(int index) { if (index >= size) throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); }
因为private修饰的实例方法只能在本类被使用。不可被外部类调用,保证了一个封装性。
2.HashSet原理
2.1 将元素加入HashSet(散列集)中,其存储位置如何确定?需要调用那些方法?
HashSet查找某个对象时,首先用hashCode()方法计算出这个对象的Hash码,HashCode值决定了该元素的存储位置,根据Hash码到相应的存储区域用equals()方法查找。2.2 选做:尝试分析HashSet源代码后,重新解释1.1
3.ArrayListIntegerStack
3.1 比较自己写的ArrayListIntegerStack与自己在题集jmu-Java-04-面向对象2-进阶-多态、接口与内部类
中的题目5-3自定义接口ArrayIntegerStack
,有什么不同?(不要出现大段代码)
ArrayListIntegerStack是用ArrayList来实现栈,ArrayIntegerStack是用Integer数组来实现栈。3.2 简单描述接口的好处.
接口拥有更大的灵活性,同时接口也是可插入性的保证4.Stack and Queue
4.1 编写函数判断一个给定字符串是否是回文,一定要使用栈,但不能使用java的Stack
类(具体原因自己搜索)。请粘贴你的代码,类名为Main你的学号
。
public class Main201521123024 { public static void main(String[] args) { Stack<Character> stack = new Stack<Character>(); Scanner scanner = new Scanner(System.in); String string = scanner.next(); for (int i = 0; i < string.length(); i++) { stack.push(string.charAt(i)); } for (int j= 0; j < string.length(); j++) { if (stack.pop() != string.charAt(j)) { System.out.println("no"); break; } else { System.out.println("yes"); break; } } } }
4.2 题集jmu-Java-05-集合
之5-6 银行业务队列简单模拟。(不要出现大段代码)
5.统计文字中的单词数量并按单词的字母顺序排序后输出
题集jmu-Java-05-集合之5-2 统计文字中的单词数量并按单词的字母顺序排序后输出 (不要出现大段代码)5.1 实验总结
3. 码云上代码提交记录及PTA实验总结
相关文章推荐
- 20145216史婧瑶《Java程序设计》第7周学习总结
- 20145326 《Java程序设计》第7周学习总结
- 20145322 《Java程序设计》第7周学习总结
- 20145317彭垚 《Java程序设计》第7周学习总结
- 20145211 《Java程序设计》第7周学习总结——沧海横流
- 20145206邹京儒《Java程序设计》第7周学习总结
- 20145218 《Java程序设计》第7周学习总结
- 20145204 《Java程序设计》第7周学习总结
- 20145203盖泽双 《Java程序设计》第7周学习总结
- 20145215《Java程序设计》第7周学习总结
- 20145229吴姗珊 《Java程序设计》第7周学习总结
- 20145214 《Java程序设计》第7周学习总结
- 20145303 《Java程序设计》第7周学习总结
- 20145106《Java程序设计》第7周学习总结
- 20145305 《Java程序设计》第7周学习总结
- 20145227 《Java程序设计》第7周学习总结
- 20145208 《Java程序设计》第7周学习总结
- 20145333 《Java程序设计》第7周学习总结
- 20145212 《Java程序设计》第7周学习总结
- 20145235《Java程序设计》第7周学习总结