javawebday11(junit debug调试 泛型 遍历 list set map 使用通配符T)

来源:互联网 发布:兼职网络维护 编辑:程序博客网 时间:2024/06/06 09:43
/* * 使用debug 调试程序 看程序里面数据的变化 * 使用debug第一步需要设置一个断点(让程序运行停止在这一行) *  显示出来行号 *  双击左边 出现一个圆点 表示设置一个断点 * 使用debug as方式 运行程序 *  提示是否进入 *  在断点地方有高亮条 表示程序停止在这一行 没有向下运行 * 可以让程序向下执行  *  使用 step over 快捷键 F6(单步执行) *  resume F8 表示调试结束 程序直接向下运行 *      比如当前的断点之后还有断点 跳到下一个断点 *      如果当前断点后面没有断点 程序直接运行结束 * debug 另外一个用途 *  查看程序的源代码 *  F5 step into 进入到方法 *  F7 step return 返回 * 快捷键 alt / 代码提示 * 代码修复  ctrl 1 * 快速导包 ctrl shift o * 单行注释 ctrl / * 多行注释 ctrl shift /  * 取消多行注释 ctrl shift \ * 删除行 ctrl d * 代码的格式化 ctrl shift f 有时候不行  sorce format  * 有缩进的效果 *  * 查找 ctrl f     * junit的使用 *  单元测试 *  测试对象是一个类中的方法 *  junit不是javase的一部分 要导入jar包 *  junit 版本 3.x 4.x *      单元测试方法时候 方法命名规则 public void 方法名(){} 不能有参数 *  使用注解方式运行测试方法 在方法上面 *      @Test           public void test() {            Test01 test = new Test01();            test.add();        }        选中方法名称 运行  run as -- junit test        当出现绿色的条表示方法测试通过        当出现了红棕色 表示测试不通过    运行类中多个测试方法 点击类中的其他位置 run as -- junit test       @Ignore 表示这个方法不进行单元测试    @Before 在每个方法之前运行    @After 在每个方法之后运行    断言        Assert.assertArrayEquals("测试期望的值","方法运行的实际的值"); *  jdk  1.1 1.2 1.4  5.0 *泛型 枚举 静态导入 自动拆装箱  增强for 可变参数 *反射  *泛型的简介 *  使用泛型的理由 *      一般使用在集合上 *          比如现在把字符串类型的值放入集合中 这个时候 这个值放到集合中 失去原本的类型 只能是object类型 *          这个时候 对这个值进行类型转换 容易出现类型转换错误 这个时候使用泛型 可以在编译的时候暴露出这个错误          *  在集合上使用泛型 *      常用集合 list set map *      泛型语法 集合 <String> 比如List<String> *  在泛型里面写是一个对象 String 不能写基本的数据类型 比如int *      写基本的数据类型对应包装类 *      int Integer *      short Short *      byte Byte *      long Long *      float Float *      double Double *      char    Character *      boolean Boolean *  在list使用 *      @Test        public void testList() {            List<String> list = new ArrayList<String>();            list.add("aa");            list.add("bb");            list.add("cc");            //遍历list集合 有几种方式 三种            //普通for循环 迭代器 增强for            //普通for循环            for (int i = 0; i < list.size(); i++) {                String s = list.get(i);                System.out.println(s);            }            System.out.println();            //增强for            for (String string : list) {                System.out.println(string);            }            //迭代器遍历            System.out.println();            Iterator<String> it = list.iterator();            while(it.hasNext()) {                System.out.println(it.next());            }        }    在set 上使用            @Test        public void testSet() {            Set<String> set = new HashSet<String>();            set.add("aa");            set.add("bb");            set.add("cc");            //遍历set 2种方式            //迭代器 增强for            //使用增强for遍历            for (String string : set) {                System.out.println(string);            }            System.out.println();            //使用迭代器遍历            Iterator<String> it = set.iterator();            while(it.hasNext()) {                System.out.println(it.next());            } *泛型使用在方法上 *  定义一个数组 实现指定位置上数组元素的交换 *  方法逻辑相同 知识数据类型不同 使用泛型方法 *  public static <T> void swap1(T[] arr,int a,int b ) {        T temp = arr[a];        arr[a] = arr[b];        arr[b] = temp;    }*泛型在类上的使用    在一个类上定义一个类型 这个类可以在类里面直接使用    public class  Test<T>{    在类里面可以直接使用T的类型    T aa;    public void test(T bb){    写一个静态方法 在类上面定义的泛型 不能在静态方法里面使用    }       }        */
public class Test01 {    public static void main(String[] args) {        //创建一个数组 实现 1 3 位置交换        Integer[] arr = {1,2,3,4,5};//需要把int 写成包装类 Integer 才能使用泛型方法        swap1(arr,0,2);        System.out.println(Arrays.toString(arr));        //创建一个string类型的数值 实现aa bb的交换        String[] arr2 = {"aa","bb","cc","dd"};        swap1(arr2,0,1);        System.out.println(Arrays.toString(arr2));    }    /*     * 使用泛型方法 需要定义一个类型 使用大写字母表示 T这个T表示任意的类型     * 写在返回值之前 void之前     *  表示定义了一个类型 这个类型是T     * 在下面就可以使用这个类型了T     */    public static <T> void swap1(T[] arr,int a,int b ) {        T temp = arr[a];        arr[a] = arr[b];        arr[b] = temp;    }    private static void swap(String[] arr2, int i, int j) {        String temp = arr2[i];        arr2[i] = arr2[j];        arr2[j] = temp;    }    private static void swap(int[] arr, int i, int j) {        //定义一个中间变量        int temp = arr[i];        arr[i] = arr[j];        arr[j] = temp;     }    public void add(){        List<String> list = new ArrayList<String>();        list.add("a");        list.add("b");        list.add("c");        //把a值取出 类型转换 转换int类型 会类型转换的错误    }}
原创粉丝点击