容器

1. collection(容器)是用来放东西的,要用容器时要导包java.util里

    collection :(1)Set :唯一,无序(2)List :可重复,有序 ;

    List又分为 :(a)ArrayList :查找快 ;(b)LinkeList :更新快 ;

    为什么要有容器,是因为数组在程序运行的时候不能改变它的长度。

2. 迭代也可以称循环,遍历也是迭代。迭代时要用到迭代器Iterator,规范的话要写上泛型。

    还有就是增强的for循环,此种for循环内部使用的也是Iterator迭代器,但是它不能方法

   索引,只能进行简单的遍历。

import java.util.*;

public class Mg {
    public static void main(String args[]) throws Exception {
        Set s = new HashSet();
        s.add(2);
        s.add("world");
        s.add("String");
        s.add("hello");
       
        Iterator i = s.iterator();      
       
        while(i.hasNext()){
           Object o = i.next();
           System.out.println(o);
        }
    } 
}

增强for循环如下:

import java.util.*;

public class Mg {
    public static void main(String args[]) throws Exception {
        Set s = new HashSet();
        s.add(2);
        s.add("world");
        s.add("String");
        s.add("hello");                     
       
        for(Object o:s){
           System.out.println(o); 
        }
    } 
}

 

3. 一些例如求最大值、绝对值、取整的方法:

    Math.mas(1,5) = 5 ;     //这就是求最大值

    Math.abs(-3) = 3 ;      //这就是求一个数的绝对值

    Math.ceil(1.3456) = 2 ;       //这是取一个数的上限整数值

    Math.floor(1.999) = 1 ;      //这是取一个数的下限整数值

    Math.random() ;         //取一个随机数

4. 包java.io里的类File,创建文件夹:

    File f = new File("d:/aa") ;

    f.mkdir() ;       //这就是在aa文件夹里创建一个新文件夹,如果是创建多个文件夹用 f.mkdirs() ;

    此外还有 f.create.NewFile() ; 和 f.getName() ; 等语句。

5. list的一个类cellections,里面包含一些算法,当要对list中的对象进行比较时,如果是自定义类就必须

    要自定义比较规则,重写compareTo方法,自定义类要实现Comparable接口。  

import java.util.*;

public class Mh{
   public static void main(String args[]) throws Exception {
      List<Person> li = new ArrayList<Person>();
      li.add(new Person("admin",11));
      li.add(new Person("jackk",16));
      li.add(new Person("tomyi",17));
      li.add(new Person("jimhl",14));
      li.add(new Person("windg",12));
     
      System.out.println(li);        //要重写toString方法,否则打印出来有哈希码
      Collections.sort(li);            //因为新定义了类Person,java不能识别,故要自己定义比较规则
      Collections.reverse(li);
      System.out.println(li);
   } 
}
class Person implements Comparable {
   String name;
   int age;
   public Person(String name ,int age){
      this.name = name;
      this.age = age; 
   } 
   public int compareTo(Object o){
      Person p = (Person)o; 
      if(this.age > p.age){
         return 1; 
      }
      else if(this.age < p.age){
         return -1; 
      }
      else {
        return 0 ;
      }
   }
   public String toString (){
      return this.name; 
   }
}

 

6. 输出一个文件夹下的所有文件夹 :

import java.io.*;

public class Mh{
   public static void main(String args[]){
       File f = new File("E:/a");
       Mh.show(f,1);
   } 
  
   public static void show(File f,int level){
      String str = "";
      for(int i = 1; i<level ;i++){
         str+="   "; 
      } 
      File[] files =f.listFiles();
      for(int i=0;i<files.length ;i++){
         System.out.println(str + files[i].getName());
         if(files[i].isDirectory()){           
            show(files[i],++level);           
         }  
      }
   }
}

7. (1)map自定义索引(2)key-value (键-值对)  ,Hash是查找最快的。

    (3)map的迭代方法有 keySet() 和 entrySet() 两种。

import java.util.*;

public class Mh{
    public static void main(String args[]) throws Exception {
       Map<String,String> map = new HashMap<String,String>();
       map.put("一","jimdi");
       map.put("二","fuura");
       map.put("三","domyi");
       map.put("四","centa");
       map.put("五","qurry");
      
       System.out.println(map);
       System.out.println(map.get("一"));         
    } 
}

 

        (a)keySet()方法 

 

import java.util.*;

public class Mh{
    public static void main(String args[]) throws Exception {
       Map<String,String> map = new HashMap<String,String>();
       map.put("一","jimdi");
       map.put("二","fuura");
       map.put("三","domyi");
       map.put("四","centa");
       map.put("五","qurry");
      
       //System.out.println(map);
       //System.out.println(map.get("一"));
      
       Set<String> set = map.keySet();
       for(Iterator<String> i=set.iterator();i.hasNext();){
           String  key = i.next();
           System.out.println(map.get(key));
       }        
    } 
}

(b)entrySet()方法

import java.util.*;

public class Mh{
    public static void main(String args[]) throws Exception {
       Map<String,String> map = new HashMap<String,String>();
       map.put("一","jimdi");
       map.put("二","fuura");
       map.put("三","domyi");
       map.put("四","centa");
       map.put("五","qurry");
      
       //System.out.println(map);
       //System.out.println(map.get("一"));
      
       Set<Map.Entry<String,String>>  set = map.entrySet();
       for(Iterator<Map.Entry<String,String>>  i = set.iterator(); i.hasNext();){
           Map.Entry<String,String>  entry = i.next();
           System.out.println("key: " + entry.getKey() + "  value: " + entry.getValue());
       }
    }        

 

posted on 2012-07-10 18:46  weij  阅读(244)  评论(0编辑  收藏  举报