A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 沙迦 中级黑马   /  2015-4-25 01:04  /  532 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

                                forecah循环(增强for循环)
/*
* 增强for循环:
* 格式:
*                 for(数组或者Collection集合中元素类型 变量名 : 数组或者Collection集合对象)
*                 {
*                         使用变量名即可。
*                 }
*
* 作用:简化数组和Collection集合的变量。
* 注意:增强for是用来替代迭代器的。不能再用增强for的时候,用集合对象对集合进行改变。(可以使用列表迭代器改变)
*/
public class ForDemo {
        public static void main(String[] args) {
                // 整型数组
                int[] arr = { 1, 2, 3, 4, 5 };
                // 普通for
                for (int x = 0; x < arr.length; x++) {
                        System.out.println(arr[x]);
                }
                System.out.println("--------------");
                // 增强for
                for (int x : arr) {
                        System.out.println(x);
                }
                System.out.println("--------------");

                // 字符串数组
                String[] strArray = { "hello", "world", "java" };
                // 增强for
                for (String str : strArray) {
                        System.out.println(str);
                }
                System.out.println("--------------");

                // 集合
                ArrayList<String> array = new ArrayList<String>();
                array.add("hello");
                array.add("world");
                array.add("java");
                for (String str : array) {
                        System.out.println(str);
                }
                System.out.println("--------------");

                // 增强for和迭代器我们一般只选一种。
                // 增强for是来替代迭代器的。
                // ArrayList<String> array2 = new ArrayList<String>();
                // array2.add("hello");
                // array2.add("world");
                // array2.add("java");
                // ConcurrentModificationException

                ArrayList<String> array2 = null;
                // NullPointerException
                for (String str : array2) {
                        if (str.equals("world")) {
                                array2.add("EE");
                        }
                }
        }
}
****************************************************************************************************************
                                泛型在类和方法上的使用
/*
* 我能不能不用重载方法,就可以实现同一个方法,输出不同类型的数据呢?
* 防止在增加后取出数据后 数据类型发现转变为使用。 比如存入一个Integer类型后获取到的是一个String类型。
*/
public class ToolTest {
        public static void main(String[] args) {
                Tool t = new Tool();
                t.show("hello");
                t.show(10);
        }
}
/*
* 泛型类:把泛型定义在类上。
*/
public class Tool2<QQ> {//自定义泛型中的类型是QQ类型,接收和获取到的都是QQ类型
       
        public void show(QQ qq) {
                System.out.println(qq);
        }

}
public class Tool2Test {
        public static void main(String[] args) {
                // Tool2 t = new Tool2();
                // t.show("hello");
                // t.show(10);

                Tool2<String> t = new Tool2<String>();
                t.show("hello");

                Tool2<Integer> t2 = new Tool2<Integer>();
                t2.show(10);
        }
}




/*
* 你为了保证方法传递不同的参数,你就在类上明确了类型。//这样可以一个类上面定义很多个类型
* 这样的话,你有没有发现一个问题呢?
* 它要是能够在调用方法的时候,才去明确类型该有多好呢?
*
* 泛型方法:把泛型加在方法上。
*/
public class Tool {
        public <BYD> void show(BYD byd) {
                System.out.println(byd);
        }
}

package cn.itcast_08;

public class ToolTest {
        public static void main(String[] args) {
                Tool t = new Tool();
                t.show("hello");
                t.show(10);
        }
}
这个同理迭代器 不能在操作数组的时候对数组里面的内容直接修改  否则并发修改异常

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马