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

    1. 本周学习总结

    2. 书面作业

    Q1.ArrayList代码分析
    1.1 解释ArrayList的contains源代码
    1.2 解释E remove(int index)源代码
    1.3 结合1.1与1.2,回答ArrayList存储数据时需要考虑元素的类型吗?
    1.4 分析add源代码,回答当内部数组容量不够时,怎么办?
    1.5 分析private void rangeCheck(int index)源代码,为什么该方法应该声明为private而不声明为public?

    1.1 源代码

     public boolean contains(Object o) {
            return indexOf(o) >= 0;
        }
    
    

    遍历元素,如存在查找的元素,返回true,否则返回false;在源代码的注释中,Returns true if this list contains the specified element.如果此列表包含指定元素,则返回true

    1.2 源代码

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

    查找此ArrayList中是否包含要找的元素,若有,返回true,否则返回false;在源代码注释中,Removes the element at the specified position in this list.删除此列表中指定位置的元素。

    1.3 元素的类型是引用类型,不能存放基本类型,像int类型,要用Integer类型,这样存放的就是int数据,在ArrayList中也可以同时存放不同类型的对象,此时要加上泛型

    1.4 源代码注释中,Appends the specified element to the end of this list将指定的元素到这个列表的末尾,当内部数组容量不够时,需要增量,即增加数组长度,ensureCapacityInternal(size + 1); // Increments modCount!!增加数组长度

    1.5 private void rangeCheck(int index)检查是否越界,This method does *not* check if the index is negative但此方法不会检查指标是负面的,也就是不检测负值,保证index在ArrayList长度范围之内(index < size()),是对ArrayList独特的rangeCheck(int index),故不用public

    Q2.HashSet原理
    2.1 将元素加入HashSet(散列集)中,其存储位置如何确定?需要调用那些方法?
    2.2 选做:尝试分析HashSet源代码后,重新解释1.1

    2.1 计算出待存储对象的散列码,作为存储的位置的依据,发生冲突时,则连接在发生冲突的元素的后面,用链表连接;将对象存入Hash类集合,需要两个方法,public int hashCode()boolean equals(Object o)

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

    3.1 首先,存储方式不同,前者使用ArrayList,虽然本质也是数组存储,但不需要一开始就要考虑数组长度,后者是数组存储,并且需要一个指针top来确定栈顶元素;前者不需要考虑栈满的情况,后者受一开始输入的数组长度的约束,需要考虑栈满的情况

    3.2 接口实现扩展功能,即接口可以多继承,但是类不能,这样就不是单一的继承关系,灵活性高,可以在接口中定义多个抽象方法,在类中实现,在软件升级过程中,可以定义多个接口,避免大规模改动

    Q4.Stack and Queue
    4.1 编写函数判断一个给定字符串是否是回文,一定要使用栈,但不能使用java的Stack类(具体原因自己搜索)。请粘贴你的代码,类名为Main你的学号。
    4.2 题集jmu-Java-05-集合之5-6 银行业务队列简单模拟。(不要出现大段代码)

    4.1
    不能使用java的Stack类,故用jmu-Java-05-集合中5-1的自定义stack,使用ArrayList存储,实现stack的功能

    import java.util.*;
    
    interface TStack{
    	public Character push(Character item);//如item为null,则不入栈直接返回null。如栈满,也返回null.
    	public Character pop();//出栈,如为空,则返回null.
    	public Character peek();//获得栈顶元素,如为空,则返回null.
    	public boolean empty();//如为空返回true
    	public int size();//返回栈中元素数量
    }
    
    class ArrayListTStack implements TStack{
        private List<Character> list;
    }//mu-Java-05-集合中5-1的自定义stack,将List的对象类型变为Character,其他不变
    
    public class Main201521123017{
    	public static void main(String[] args) {
    		Scanner in=new Scanner(System.in);
    		ArrayListTStack stack=new ArrayListTStack();
    		String str=in.nextLine();
    		int t=-1;
    		int x=0;
    		for(int i=0;i<str.length();i++){
    			stack.push(str.charAt(i));
    		}
    		for(int j=str.length()-1;j>=0;j--){
    			if(stack.peek().equals(str.charAt(x))){
    				stack.pop();
    				x=x+1;
    				t=1;
    			}
    			else {
    				t=0;
    				break;
    			}
    		}
    		if(t==1) System.out.println("yes");
    		else System.out.println("no");
    	}
    }
    
    

    截图

    4.2
    Queue接口与List、Set同一级别,都是继承了Collection接口,LinkedList实现了Queue接口

    定义

    Queue<Integer> list1=new LinkedList<Integer>();//A窗口
    Queue<Integer> list2=new LinkedList<Integer>();//B窗口
    
    

    入队

    for(i=0;i<n;i++){
        x=in.nextInt();
        if(x%2==1) list1.add(x);//奇数A窗口
        else list2.add(x);//偶数B窗口
    }
    

    出对

    while(!list1.isEmpty()&&!list2.isEmpty()){
        if(!list1.isEmpty()){
            System.out.print(list1.poll()+" ");
            if(!list1.isEmpty()){
                System.out.print(list1.poll()+" ");
                }
    	}
        System.out.print(list2.poll()+" ");
    }
    

    退出循环,将非空队列的对象全部输出
    (注:PTA显示答案错误,还在修改)

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

    str=in.next();//输入的文本以空格分开将单词存入,输入方式,不能用in.nextLine()
    Set<String> strset=new TreeSet<String>();//用Set来存储,但HashSet只是无重复的存入单词,无序,此时需要用TreeSet,TreeSet具有排序功能reeSet
    
    

    实验总结:统计文本出现的词汇并存储,此时要想到Set,Set集合的对象是不重复的,但要是对对象进行排序,需要的是TreeSet,TreeSet具有排序功能,HashSet可以用作无序的词汇存储,因而本题不使用HashSet

    Q7.面向对象设计大作业-改进
    7.1 完善图形界面(说明与上次作业相比增加与修改了些什么)
    7.2 使用集合类改进大作业

    1. 主要是用JTabel改写界面
      (延迟很高,过了很久才出来,导致JAVA多按了一下添加)

    (将多余的JAVA删除)

    (注:购物车的界面无法跳出,还在修改中)

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

    3.1. 码云代码提交记录

  • 相关阅读:
    图片完全填充CardView区域
    调用系统相机拍照并展示
    Android开发之加载GIF图片
    使用SatelliteMenu创建动画菜单
    swift中简单KVC和KVO的使用
    swift GCD的简单使用
    swift协议的使用方法和注意事项
    swift找window,导航栏的简单使用,Controller通过闭包传值简单示例
    swift中闭包的使用
    swift中UIButton的使用
  • 原文地址:https://www.cnblogs.com/gemola/p/6675731.html
Copyright © 2011-2022 走看看