学习打卡第一天

 1 package Exception;
 2 
 3 public class Student {
 4     private void speak(int m)throws MyException
 5     {
 6         if(m>1000) 
 7         {
 8             throw new MyException("该数大于1000");
 9         }
10     }
11     
12 
13     public static void main(String[] args) {
14         Student student = new Student();
15         try
16         {
17             student.speak(1010);
18         }catch(MyException e) 
19         {
20             System.out.println(e.getMessage());
21         }
22 
23     }
24 
25 }
26 class MyException extends Exception
27 {
28     String message;
29     public MyException(String ErrorMessage) 
30     {
31         message = ErrorMessage;
32     }
33     public String getMessage() 
34     {
35         return message;
36     }
37 }

以上是今日学习的异常处理以及捕捉,在编程中需要去考虑你写的程序会出现的情况,并且去处理它,保证不让你的程序崩溃。

 1 package list;
 2 import java.util.*;
 3 public class ListTest {
 4     LinkedList<Integer>list = new LinkedList<Integer>();//ArrayList与LinkedList的区别在于ArrayList插入对象和删除对象时比LinkedList慢
 5     public void add(int n) 
 6     {
 7         list.add(n);
 8     }
 9     public void remove(int index) 
10     {
11         list.remove(index);
12     }
13     public int getSize() 
14     {
15         return list.size();
16     }
17     public int get(int index) 
18     {
19         return list.get(index);
20     }
21     public static void main(String[] args) {
22         ListTest list = new ListTest();
23         for(int i =1;i<=100;i++) 
24         {
25             list.add(i);
26         }
27         list.remove(10);
28         for(int i =0;i<list.getSize();i++) 
29         {
30             System.out.print(list.get(i)+" ");
31             if(i%10 == 0 && i != 0) 
32             {
33                 System.out.println();
34             }
35         }
36     }
37 
38 }
 1 package set;
 2 
 3 public class Student implements Comparable<Object>{
 4 
 5         int id;
 6         String name;
 7         char sex;
 8         public Student(int id,String name,char sex) 
 9         {
10             this.id = id;
11             this.name = name;
12             this.sex = sex;
13         }
14         public void print() 
15         {
16             System.out.println("学号:"+this.id);
17             System.out.println("姓名:"+this.name);
18             System.out.println("性别:"+this.sex);
19         }
20         public int compareTo(Object o) 
21         {
22             Student s = (Student)o;
23             int result = this.id>s.id ? 1 : (this.id == s.id ? 0 : -1);
24             return result;
25         }
26 }

 

package set;

import java.util.Iterator;
import java.util.TreeSet;

public class SetTest {
    TreeSet<Student> set = new TreeSet<Student>();//TreeSet和TreeMap 会自动排序 需要去实现comparaTo函数
    public void add(Student s) 
    {
        set.add(s);
    }
    public void remove(Student s) 
    {
        set.remove(s);
    }
    public void allShow() 
    {
        Iterator it = set.iterator();
        while(it.hasNext()) 
        {
            Student s = (Student)it.next();
            s.print();
        }
    }
    public static void main(String[] args) {
        SetTest set = new SetTest();
        Student s1 = new Student(125,"李丽",'女');
        Student s2 = new Student(215,"李伟",'男');
        Student s3 = new Student(145,"李春江",'男');
        set.add(s1);
        set.add(s2);
        set.add(s3);
        set.allShow();

    }

}

以上是今日学习的集合类,基本类型存放可以存放在数组中,对象一般存放在集合中,集合中可以自动增长长度,以上Student类和SetTest类在同一packge下没有写在同一个java文件下,知道了TreeMap和TreeSet会进行自动排序,但是需要自己去实现那个comparaTo函数,不然会报错,以上就是今日所学习。

posted @ 2019-03-20 23:00  oops_w  阅读(257)  评论(0编辑  收藏  举报