2.当程序中出现异常时,JVM会依据方法调用顺序依次查找有关的错误处理程序。可使用printStackTrace 和getMessage方法了解异常发生的情况。阅读下面的程序,说明printStackTrace方法和getMessage 方法的输出结果分别是什么?并分析异常的传播过程。

public class PrintExceptionStack {
    public static void main( String args[] )
    {
         try {
             method1();
          } catch ( Exception e ) {
             System.err.println( e.getMessage() + "\n" );
             e.printStackTrace();
          }
    }
   public static void method1() throws Exception
   {
      method2();
   }
   public static void method2() throws Exception
   {
      method3();
   }
   public static void method3() throws Exception
   {
      throw new Exception( "Exception thrown in method3" );
   }
}

对该程序使用getMessage方法输出,结果如下:

Exception thrown in method3

对该程序使用printStackTrace方法输出,结果如下:

java.lang.Exception: Exception thrown in method3
at trys.PrintExceptionStack.method3(PrintExceptionStack.java:23)
at trys.PrintExceptionStack.method2(PrintExceptionStack.java:19)
at trys.PrintExceptionStack.method1(PrintExceptionStack.java:15)
at trys.PrintExceptionStack.main(PrintExceptionStack.java:7)

异常的传播过程:
程序运行后执行try中的method1(),method1()依指令把异常抛出给method2(),method2()又依指令把异常抛出给method3(),method3()依指令抛出异常"Exception thrown in method3",此异常被主函数中的catch捕捉后输出具体错误信息和位置。
输出结果的区别:
getMessage方法:只显示异常名称,方便开发者了解程序出错原因;
printStackTrace方法:打出详细异常(异常名称及出错位置),便于开发者调试。


3.阅读下面程序,分析程序的运行结果,解释产生错误的原因,如果删除的是books集合的最后一个对象,运行的结果又是什么?你能对此作出解释吗?如果在遍历时非要删除集合中的元素,应如何实现?

import java.util.*;
public class Test
{
    public static void main(String[] args) 
    {
        Collection<String> books = new ArrayList<String>();
        books.add("One book");
        books.add("Two book");
        books.add("Three book");
        System.out.println("原始元素之后:"+books);
        Iterator<String> it = books.iterator();
        while(it.hasNext())
        {
            String book = (String)it.next();
            System.out.println(book);
            if (book.equals("One book"))
            {
                books.remove(book);
            }
        }
        System.out.println("移除元素之后:"+books);
    }
}

执行上述程序,结果如下:

原始元素之后:[One book, Two book, Three book]
Exception in thread "main" One book
java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
at java.util.ArrayList$Itr.next(Unknown Source)
at shiyan6.Test.main(Test.java:15)

错误原因:
使用remove()方法删除元素后对象的数量发生改变,而Iterator建立的指向原来对象的单链索引表的内容没有同步改变,因此在程序执行时会出现类似空指针一样获取不到对象的错误。
修改思路:
使Iterator删除后索引表对应同步
(可以由Iterator本身的方法 remove() 来删除对象)
修改方案:

 if (book.equals("One book"))
            {
                books.remove(book);
            }
        }

修改为:

if (book.equals("One book"))
        {
            it.remove();
        }
    }

4.HashSet存储的元素是不可重复的。运行下面的程序,分析为什么存入了相同的学生信息?如果要去掉重复元素,应该如何修改程序。

import java.util.*;
class Student {
    String id;  
    String name;
    public Student(String id, String name) {
        this.id = id;
        this.name = name;
    }
    public String toString() {
        return "Student id=" + id + ", name=" + name ;
    }
}
public class Test
{
    public static void main(String[] args) 
    {
        HashSet<Student> set = new HashSet<Student>();
        set.add(new Student("1","Jack"));
        set.add(new Student("2","Rose"));
        set.add(new Student("2","Rose"));
        System.out.println(set);                
    }
}

HashSet是依据判断栈中存储地址来判断元素是否重复。程序中两个学生信息相同,但是存储位置不同,因此Hashset将其判断为不重复元素。


代码托管(http://git.oschina.net/huwei1022/shiyan06)