Java第六次作业--异常处理和Java类集

(一)学习总结

1.用思维导图对本周的学习内容进行总结。

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() + "
" );
                 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" );
       }
    }

printStackTrace方法异常信息和出现异常的位置

Exception thrown in method3

java.lang.Exception: Exception thrown in method3
	at PrintExceptionStack.method3(PrintExceptionStack.java:22)
	at PrintExceptionStack.method2(PrintExceptionStack.java:18)
	at PrintExceptionStack.method1(PrintExceptionStack.java:14)
	at PrintExceptionStack.main(PrintExceptionStack.java:6)

getMessage 方法异常信息

Exception thrown in method3

异常的传播过程

  • 在Java程序的执行过程中,如果出现了异常事件,就会生成一个异常对象。生成的异常对象将传递给Java运行时系统,这一异常的产生和提交过程称为抛弃(throw)异常。
  • 当Java运行时系统得到一个异常对象时,它将会寻找处理这一异常的代码,找到能够处理这种类型异常的方法后,运行时系统把当前异常对象交给这个方法进行处理,这一过程称为捕获(catch)异常。
  • 如果Java运行时系统找不到可以捕获异常的方法,则运行时系统将终止,相应的Java程序也将退出。

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

•如果删除的是books集合的最后一个对象,运行结果:

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

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

运行结果为:

[Student id=2, name=Rose, Student id=2, name=Rose, Student id=1, name=Jack]

这两个同学的信息输入时,分别进行了实例化,两个对象的HashCode值不同,所以判断为不同元素
添加equals()方法与HashCode()方法
运行结果:

[Student id=1, name=Jack, Student id=2, name=Rose, ]

5.其他需要总结的内容。
对于Java的异常处理机制理解较为浅显,捕获异常和声明抛出异常需要多加巩固;
迭代器的应用完全不理解,需要课下咨询其他同学;
对于Collection接口(List接口和Set接口)和Map接口(HashMap类和TreeMap类)之间的联系比较混淆,需要多做几个实例来完全理解它们之间的关系,它们所实现的功能。

(二)实验总结

1.模拟KTV点歌系统
分别用LinkedList和ArrayList集合,实现一个模拟KTV点歌系统的程序。实现以下功能:
(1)显示歌曲列表
(2)添加歌曲到列表
(3)删除歌曲
(4)将歌曲置顶
(5)将歌曲前移一位
(6)退出
题目扩展:歌曲包括曲名、演唱者。增加排序显示歌曲列表功能。
设计思路:
LinkedList方法时,add()添加歌曲,remove()删除歌曲,addFirst()置顶歌曲
遇到的问题:

linkedList.remove("遇见");
System.out.println("删除歌曲:"+linkedList.get(3));
     

发现这样输出的不是要删除的歌曲

解决方案:
应该先写要输出的内容,在进行删除

System.out.println("删除歌曲:"+linkedList.get(3));
linkedList.remove("遇见");
     

设计思路:
ArrayList方法时,add()添加歌曲,remove()删除歌曲
遇到的问题:
不会置顶歌曲,addFirst()方法不能使用
解决方案:
先将歌曲add(0,"歌曲名称"),再将原位置歌曲删除,因为已经顶端添加一首,所以本来歌曲位置后移一位

allList.add(0,"说谎");
	     System.out.println("置顶歌曲:"+allList.get(0));
	     allList.remove(3);
     

(三)代码托管(务必链接到你的项目)

https://git.oschina.net/hfja/java-CS02hfj.git

原文地址:https://www.cnblogs.com/hfja/p/6805146.html