集合和单元测试的菜

   为了迎接新的一年,我精心准备12道JAVA风味的菜和你一起品尝。

首先我们来介绍下主角: 集合

      集合:存储对象数据 的集合容器。成分有:Collection,List,ArrayList,LinkedList,Vector,Set,HashSet,TreeSet。口味不同,必须要我们一起尝过才知道。


//创建Persion类class Person{    int id;    String name;    //重写Person类为    public Person(int id,String name) {      super();      this.id = id;      this.name = name;    }        //没有对String重写是不能输出字符本身的    @override    public String toString(){      return "编号:"this.id+"姓名: "+ this.name;    }    @override    public int hashCode(){      return this.id;    }    //如果hashset key 重复需要重写equal方法     @override    public boolean equals(object obj) {      Person p = (Person)obj;      return this.id == p.id;    }    }
//Demo1.javapublic class Demo1 {//主调用无返回值函数,带有字符参数    public static void main(String[] args) {   //实例化persion的对象        HashSet<Person> set = new HashSet<Person>();       set.add(new Person(id:213,name:"非可"));       set.add(new Persion(id:342,name:"保护"));       System.out.println("集合的元素:”+set);   }          }  


单元测试框架(Junit)

1.一个类如果需要测试,那么该类就应该对应着一个测试类,测试类的命名规范 : 被测试类的类名+ Test.2. 一个被测试的方法一般对应着一个测试的方法,测试的方法的命名规范是:test+ 被测试的方法的方法名

//Demo1.java
public class Demo1 {  @Test //注解  public   void getMax(int a, int b){  /*  int a = 3;    int b = 5 ;*/    int max = a>b?a:b;    System.out.println("最大值:"+max);  }
 @Test  public void sort(){    int[] arr = {12,4,1,19};    for(int i = 0 ; i  < arr.length-1 ; i++){      for(int j = i+1 ; j<arr.length ; j++){        if(arr[i]>arr[j]){          int temp = arr[i];          arr[i] = arr[j];          arr[j] = temp;        }      }    }       System.out.println("数组的元素:"+Arrays.toString(arr));     }  }//批量执行单元测试import org.junit.runner.RunWith;import org.junit.runners.Suite;
@RunWith(Suite.class)@Suite.SuiteClasses({  TestFeatureLogin.class,  TestFeatureLogout.class,  TestFeatureNavigate.class,  TestFeatureUpdate.class})
public class FeatureTestSuite {  // the class remains empty,  // used only as a holder for the above annotations}

吃完这道菜希望你对集合和单元测试有个全新的味觉。