第6讲java的API(Application Programming Interface)

第6讲java的API(Application Programming Interface)

String and StringBuffer

String 类的常用成员方法
构造方法:
String(byte[] bytes,int offset,int length)
equalsIgnoreCase方法
indexOf(int ch)方法
substring(int beginIndex)方法
substring(int beginIndex,int endIndex)

基本数据类型的对象包装类
基本数据类型    包装类
boolean  Boolean
byte  Byte
char  Character
short  Short
int  Integer
long  Long
float  Float
double  Double

将一个字符串转换成int(基本类型)的三种方法
int w = new Integer(args[0]).intValue();
int h = Integer.parseInt(args[1]);
//int h = Integer.valueOf(args[1]).intValue();

集合类
Vector类与Enumeration接口
定义:
Vector v = new Vector();
添加:
v.addElement(new Integer(num));
取出:
Enumeration e = v.elements();
while(e.hasMoreElements())
{
 Integer intObj  = (Integer)e.nextElement();
}

在方法中定义变量一定要显示的给变量赋初始值

Collection接口与Iterator接口
例子:ArrayList类与Iterator接口
定义:
ArrayList v = new ArrayList();
添加:
v.add(new Integer(num));
取出:
Iterator e = v.iterator();
while(e.hasNext())
{
 Integer intObj = (Integer)e.next(); 
}

Vector与ArrayList的区别 vector是线程安全的,ArrayList中的方法是不同步的
如果在编程中涉及到同步的情况就需要编程者自己添加同步。

 

Collection、Set、List的区别如下:
-Collection各元素对象之间没有指定的顺序,允许有重复元素和多个null元素对象。
-Set各元素对象之间没有指定的顺序,不允许有重复的元素,最多允许有一个null元素对象。
-List各元素对象对象之间有指定的顺序,允许有重复的元素和多个null元素对象。

Collections.sort(v),注意Collections类提供了很多静态的方法来操作集合对象


Hashtable类
Hashtable不仅可以像Vector一样动态存储一系列的对象,而且对存储的每一个对象(称为值)都安排另一个对象(称为关键字)与之相关联。
使用put方法存放数据
Hashtable numbers = new Hashtable();
numbers.put("one",new Integer(1));
numbers.put("two",new Integer(2));
numbers.put("three",new Integer(3));
Hashtable中不能有重复的关键字,如果put方法执行相同的关键字的时候实现的是更新操作
使用get方法得到指定关键字的值:
Integer n = (Integer)numbers.get("two");
if(n!=null)
{
 System.out.println("two = " +n);
}

注意:用作关键字的类必须覆盖Object.hashCode方法和Object.equals方法
String类实现了hashCode方法可以使用String类做为Hashtable类的关键字类
StringBuffer类没有现实hashCode方法所以不可以使用StringBuffer类做为Hashtable类的关键字类

public boolean equals(Object obj)
{
 if(obj.instanceof MyKey)
 {
  MyKey objTemp = (MyKey)obj;
  if(name.equals(objTemp.name)&& age = obj.age)
  {
   return true;
  }else
  {
   return false;
  }
 
 }else{
  return false;
 }
}

public int hashCode(){
 return name.hashCode() + age;
}

例子:
定义:
Hashtable numbers = new Hashtable();
添加:
numbers.put(new MyKey("zhangsan",18),new Integer(1));
numbers.put(new MyKey("lisi",15),new Integer(2));
numbers.put(new MyKey("wangwu",20),new Integer(3));
取值:
Enumeration e = numbers.keys();
while(e.hasMoreElements())
{
 MyKey key = (MyKey)e.nextElement();
 System.out.print(key+" = ");
 System.out.println(numbers.get(key));
}

Properties类
·Properties类是Hashtable的子类
·增加和从文件中读取关键字和值到Hashtable对象中的方法
·如果要用Properties.store方法存储Properties对象中的内容,每个属性的关键字和值都必须是String类型


System与Runtime类
·System类
   -exit方法
   -currentTimeMillis方法
   -Java虚拟机的系统属性
   -getProperties和setProperties方法

例子:
定义:
//System.setProperties();
Properties sp = System.getProperties();
取值:
Enumeration e = sp.propertyNames();
while(e.hasMoreElements())
{
 String key = (String)e.nextElement();
 System.out.println(key+ "="+sp.getProperties(key));
}

Runtime类
  -Runtime.getRuntime静态方法得到一个Runtime类对象,不可以直接使用new 关键字实例化Runtime类对象
例子:
Process p = null;
p = Runtime.getRuntime().exec("notepad.ext TestRuntime.java");
Thread.sleep(3000);
p.destroy();

与日期和时间有关的类
·最常用几个类:Date、DateFormat和Calendar
·Calendar类
 -Calendar.add方法
 -Calendar.get方法
 -Calendar.set方法
 -Calendar.getInstance静态方法--calendar类是个抽象基类可以通过此方法返回一个calendar类的一个对象
 -GregorianCalendar子类

编程实例:
Calendar cl = Calendar.getInstance();
System.out.println(cl.get(Calendar.YEAR)+"年"+
cl.get(Calendar.MONTH)+"月"+ cl.get(cl.DAY_OF_MONTH)+"日 "+cl.get(cl.HOUR)+"时"+cl.get(cl.MINUTE)+"分"+cl.get(cl.SECOND)+"秒");

注意:DateFormat和SimpleDateFormat都是java.text包下的类
import java.text.DateFormat;
import java.text.SimpleDateFormat
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日");
Date d = sdf1.parse("2011-01-21");
System.out.println(sdf2.format(d));


Timer与TimerTask类
·schedule方法主要有如下几种重载形式:
 -schedule(TimerTask task,long delay) --delay隔多长时间执行前面指定的task
 -schedule(TimerTask task,Date time) --time这个时间执行前面指定的task
 -schedule(TimerTask task,long delay,long period)
 -schedule(TimerTask task,
   Date firstTime,long period)
·TimerTask类实现了Runnable接口,要执行的任务由它里面实现的run方法来完成。


编程实例:程序运行后3秒打开Windows的计算器程序。
Daemon --守护线程 --守护程序 --后台程序
计时器对象 new Timer(true) --new Timer(boolean isDaemon)
calc.exe Windows 计算器程序 --calculator 计算器
定义TimerTask任务类:
class MyTimerTask  extends TimerTask ...run(){....tm.cancel()}

定义一个计时器对象:
Timer tm = new Timer();
tm.schedule(new MyTimerTask(tm),3000);

Math与Random类
·Math类包含了所有用于几何和三角运算的方法
·Random类是一个伪随机数产生器

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值