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

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;
}
作用是验证该ArrayList中是否包含某个对象,由indexOf(Object o)返回第一个出现的元素o的索引,如果不存在,则返回-1。
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;
}

查找此ArrayList中是否包含要找的元素,若有,返回true,否则返回false;在源代码注释中,Removes the element at the specified position in this list.删除此列表中指定位置的元素。
1.3 结合1.1与1.2,回答ArrayList存储数据时需要考虑元素的类型吗?

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

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

对源代码分析如下,内部数组容量不够时,可以通过扩大数组长度,也可以改变容器的大小,或者将数组转移到更大的容器。

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

rangeCheck只需要被remove引用,对下标是否越界进行判断,因此使用private就够了,private的权限范围比public小,但是对rangeCheck来说,声明为private完全能够发挥它的作用,使用public反而有点浪费且没什么必要。

2. HashSet原理

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

当向HashSet中添加一个元素时,HashSet会调用该对象的hashCode()方法得到其hashCode值,然后根据该值决定该对象的存储位置,但是如果有两个元素通过equals()方法比较返回true,而它们的hashCode()方法返回值不等,HashSet也会将它们存储在不同的位置。

3. ArrayListIntegerStack

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

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

ArrayIntegerStack实例化的时候需要规定大小,而ArrayListIntegerStack使用list可以自动扩容。此外,在ArrayIntegerStack中,出栈入栈等操作需要用到指针,而ArrayListIntegerStack使用list则不需要使用指针,可以直接通过list的一些方法来进行删除,查找等操作。

3.2 简单描述接口的好处.

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

4. Stack and Queue

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

class StackArray{
ArrayList<Character> arr = new ArrayList<Character>();
public StackArray(String s){
    for(int i=0;i<s.length();i++)
        arr.add(s.charAt(i));
}
public Character push(Character c){
    arr.add(c);
    return c;
}
public Character pop() {
    return arr.remove(arr.size() - 1);
}
public int size(){
    return arr.size();
}

}
public class Main201521123015 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
StackArray str = new StackArray(s);
for(int i=0;i<str.size();i++){
if(s.charAt(i) != str.pop()){
System.out.println("NO");
return;
}
}
System.out.println("YES");
}
}
4.2 题集jmu-Java-05-集合之5-6 银行业务队列简单模拟。(不要出现大段代码)

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 list;
}//mu-Java-05-集合中5-1的自定义stack,将List的对象类型变为Character,其他不变

public class Main201521123015{
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");
}
}

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

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

TreeSet是SortedSet接口的唯一实现类,TreeSet可以确保集合元素处于排序状态。TreeSet支持两种排序方式,自然排序 和定制排序,其中自然排序为默认的排序方式。向TreeSet中加入的应该是同一个类的对象。

TreeSet判断两个对象不相等的方式是两个对象通过equals方法返回false,或者通过CompareTo方法比较没有返回0。

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

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

添加结算模块

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

posted on 2017-04-08 20:21  王华俊  阅读(147)  评论(1编辑  收藏  举报