java API

1  Hashtable

Hashtable不仅可以像Vector一样动态存储一系列的对象,而且对存储的每一个对象都要安排另一个对象(成为关键字)与之相关联。用作关键字必须覆盖Object.hashCode方法和Object.equals方法。

 一下举例说明:package com.Text3;

 

public class MyKey {

   

       private String name=null;

      

       public boolean equals(Object obj){

           if(obj instanceof MyKey){

              MyKey objtemp= (MyKey)obj;

              if(name.equals(objtemp.name)){

                  return true;

              }else{

                  return false;

              }

           }else{

              return false;

           }

           }

        public int hashCode(){

        return name.hashCode();

        }

        public MyKey(String name){

        this.name=name;

        }

        public String toString(){

            return name;   

        }

}

 

package com.Text3;

 

import java.util.Enumeration;

import java.util.Hashtable;

 

public class HashtableText {

 

       /**

        * @param args

        */

       public static void main(String[] args) {

              // TODO Auto-generated method stub

 

              Hashtable numbers=new Hashtable();

              numbers.put(new MyKey("张三"), new Integer(1));

              numbers.put(new MyKey("李四"), new Integer(3));

              numbers.put(new MyKey("王五"), new Integer(2));

              Enumeration e =numbers.keys();

              while(e.hasMoreElements()){

                     MyKey key=(MyKey)e.nextElement();

                     System.out.print(key.toString()+"=");

                     System.out.println(numbers.get(key));

              }

       }

}

2               Propertise

1Propertise类是hashtable的子类。

2)增强了将Hashtable对象中的关键字和值保存到文件和从文件中读取关键字和值到Hashtable对象中的方法。

3)如果要用Propertises.store方法存储Propertise对象中飞内容,每个属性的关键字和值都必须是String类型

  举例说明:使用Propertises把程序的启动运行次数纪录在某文件中,每次运行时打印出它的运行次数:package com.Text3;

 

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.util.Properties;

 

public class PropertiseFile {

 

    /**

     * @param args

     */

    public static void main(String[] args) throws Exception  {

       // TODO Auto-generated method stub

       try{

       Properties Settings=new Properties();

       Settings.load(new FileInputStream("count.txt"));

       Settings.getProperty("count",Settings.getProperty("count",String.valueOf(0)));

       int c=Integer.parseInt(Settings.getProperty("count"))+1;

       System.out.println("这是第"+c+"次运行");

       Settings.put("count", new Integer(c).toString());

      

       Settings.store(new FileOutputStream("count.txt"), "program is used:");

       }catch(Exception e){

          

       }

    }

}

3              System

-        exit方法

-        currentTimeMillise方法

-        java虚拟机的系统属性

-        getPropertisesetPropertise方法

举例获得系统的属性:package com.Text3;

 

import java.util.Enumeration;

import java.util.Properties;

 

public class PropertiseText {

 

/**

 * @param args

 */

public static void main(String[] args) {

    // TODO Auto-generated method stub

 

    Properties sp =System.getProperties();

    Enumeration e=sp.propertyNames();

    while(e.hasMoreElements()){

        String str=(String)e.nextElement();

        System.out.println(str+"="+sp.getProperty(str));

    }

}

}

4              MathRandom

-Math类包含了所有用于几何和三角运算的方法

-Random类是一个伪随机数产生器

如:Random ran=new Random();

 

5              学习API的方法

(1)       有了某一领域的专业知识,再看一些范例程序,才能更容易掌握和理解一些新的API

(2)       不要看什么java API大全之类的书籍

(3)       结交一些程序员朋友,或上一些技术论坛

(4)       不能纸上谈兵,要敢于动手实践

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值