黑马程序员-----集合框架类(四) 高级for循环、方法的可变参数及静态导入

 

 

1.1 高级for循环(示例1)

格式:
for(数据类型 变量名 : 被遍历的集合(Collection)或者数组)
{

}

对集合进行遍历。
只能获取集合元素。但是不能对集合进行操作。

迭代器除了遍历,还可以进行remove集合中元素的动作。
如果是用ListIterator,还可以在遍历过程中对集合进行增删改查的动作。

1.1.2 传统for和高级for有什么区别呢?

高级for有一个局限性。必须有被遍历的目标。

 1 示例1:
 2 import java.util.*;
 3 
 4 class ForEachDemo 
 5 {
 6     public static void main(String[] args) 
 7     {
 8 
 9         
10         ArrayList<String> al = new ArrayList<String>();
11 
12         al.add("abc1");
13         al.add("abc2");
14         al.add("abc3");
15 
16 
17         for(String s : al)
18         {
19             //s = "kk";
20             System.out.println(s);
21         }
22 
23         System.out.println(al);
24         /*
25         Iterator<String> it = al.iterator();
26 
27         while(it.hasNext())
28         {
29             System.out.println(it.next());
30         }
31         */
32 
33         int[] arr = {3,5,1};
34 
35         for(int x=0; x<arr.length; x++)
36         {
37             System.out.println(arr[x]);
38         }
39         for(int i : arr)
40         {
41             System.out.println("i:"+i);
42         }
43 
44 
45         HashMap<Integer,String> hm = new HashMap<Integer,String>();
46 
47         hm.put(1,"a");
48         hm.put(2,"b");
49         hm.put(3,"c");
50 
51         Set<Integer> keySet = hm.keySet();
52         for(Integer i : keySet)
53         {
54             System.out.println(i+"::"+hm.get(i));
55         }
56 
57 //        Set<Map.Entry<Integer,String>> entrySet = hm.entrySet();
58 //        for(Map.Entry<Integer,String> me : entrySet)
59 
60         for(Map.Entry<Integer,String> me : hm.entrySet())
61         {
62             System.out.println(me.getKey()+"------"+me.getValue());
63         }
64 
65     }
66 }

 

PS:建议在遍历数组的时候,还是希望使用传统for。因为传统for可以定义脚标。

 

 

2.1方法的可变参数,JDK1.5版本出现的新特性(示例2)
在使用时注意:可变参数一定要定义在参数列表最后面。

 1 示例2
 2 class  ParamMethodDemo
 3 {
 4     public static void main(String[] args) 
 5     {
 6         //show(3,4);
 7         /*
 8         //虽然少定义了多个方法。
 9         但是每次都要定义一个数组。作为实际参数。
10 
11         int[] arr = {3,4};
12         show(arr);
13 
14         int[] arr1 = {2,3,4,5};
15         show(arr1);
16         */
17 
18         /*
19         可变参数。
20         其实就是上一种数组参数的简写形式。
21         不用每一次都手动的建立数组对象。
22         只要将要操作的元素作为参数传递即可。
23         隐式将这些参数封装成了数组。
24 
25         */
26         show("haha",2,3,4,5,6);
27         //show(2,3,4,5,6,4,2,35,9,"heh");
28         //show();
29 
30     }
31     public static void show(String str,int... arr)
32     {
33         System.out.println(arr.length);
34     }
35     /*
36     public static void show(int[] arr)
37     {
38         
39     }
40     */
41     /*
42     public static  void show(int a,int b)
43     {
44         System.out.println(a+","+b);
45     }
46     public static  void show(int a,int b,int c)
47     {}
48     */
49 }

 

3.1 StaticImport 静态导入(示例3)

当类名重名时,需要指定具体的包名。
当方法重名是,指定具备所属的对象或者类

示例3:
import java.util.*;
import static  java.util.Arrays.*;//导入的是Arrays这个类中的所有静态成员。
import static java.util.Collections.*;

import static  java.lang.System.*;//导入了System类中所有静态成员。

class  StaticImport //extends Object
{
    public static void main(String[] args) 
    {

        out.println("haha");
        int[] arr = {3,1,5};

        sort(arr);
        int index = binarySearch(arr,1);
        out.println(Arrays.toString(arr));
        System.out.println("Index="+index);

        ArrayList al = new ArrayList();
        al.add(1);
        al.add(3);
        al.add(2);

        out.println(al);

        sort(al);
        out.println(al);
    }
}

 

posted on 2015-06-15 15:51  若诺  阅读(215)  评论(0编辑  收藏  举报