第七章 Java高级 API

第七章 Java高级 API

    概念 就是application programming interface的简称

     Windows API就是windows操作系统提供的各种函数

javaAPI就是JDK中的java

学习编程语言 必须掌握其语法规则 多多掌握API 以及积累编程经验 同时还要注意编程专业程序须熟知专业程序的API

   API可以现学现用 编程需要交流 老手操作很重要

以适用为准则 适可而止

Java工具软件JCreator

特点 集成编辑软件功能

工程式管理 对所有文件进行管理

产生应用程序的基本框架 支持调试与断点调试

以及支持各种辅助工具

JCreator的工作区可以管理多个工程

一个工作区只能有一个活动的工程

工作区 JCW 与工程 jcp 是两个独立的事物

1、String 类与StringBuffer

    String 类与StringBuffer进行字符串的封装操作 位于java.lang中 可以直接使用

String类对象的内容一旦初始化就不可修改

StringBuffer类用于封装可以修改的字符串

toSTring转化成String

String x="a"+2+"b"+......形成字符串

StringBuffer=new StringBuffer.append()toString();来构成字符串

字符串常量是用" "

String str="hello"

String(byte[]bytes,int offest,int length)

定义一个字节数组中起始位置是offset 长度为length的字符串

Indexof(int ch)用于字符首次出现的位置

subString(int beginIndex)返回子字符串

subString(int beginIndex)intendIndex)

返回起始和终止位置之间的字符串

public class readlin

{

/**

 * Method main

 *

 *

 * @param args

 *

 */

public static void main(String[] args)

  {

// TODO: Add your code here

byte buf[]=new byte[1024];

String str=null;

int pos=0;

int ch=0;

while(true)

{   try{

ch=System.in.read();

     }

     catch(Exception e)

     {e.printStackTrace();

     }

switch(ch)

{ case '/r':

break;

case '/n':

str=new String(buf,0,pos);

if(str.equals("bye"))

{return;}

else

{System.out.println(str);

 pos=0;

 break;

}

default:

buf[pos++]=(byte)ch;

}

}

  }

}

2、基本数类型的对象封装

基本数据类型不具有数据对象性 故而需要封装

boolean--Boolean    char=Character等等

public class testInteger {

/**

 * Method main

 *

 *

 * @param args

 *

 */

public static void main(String[] args) {

// TODO: Add your code here

int w=new Integer(args[0]).intValue();

int h=Integer.parseInt(args[1]);

for(int i=0;i<h;i++)

{ StringBuffer sb=new StringBuffer();

for(int j=0;j<w;j++)

{sb.append('*');

}

System.out.println(sb.toString());

}

}

}

3、集合类

集合类是一组用于存储的一组对象 其中每个对象称之为元素 经常会用到有的Vector   Enumeration   ArraryList  collection  set  list

3.1  Vector 类 不知道保存的数目

例子 将键输入的一个数字序列中的美味数字存储在Vector中 然后打印相加的结果

import java.util.*;

public class testVector

{

public static void main(String args[])

{

int b=0;

Vector v=new Vector();//可以存储各种类型的数据

System.out.println("please enter number:");

while(true)

{ try { b=System.in.read();}

catch(Exception e){e.printStackTrace();}

  if(b=='/r'||b=='/n')

   break;

   else

   {int num=b-'0';

   v.addElement(new Integer(num));

      }

}

int sum=0;

Enumeration e=v.elements();//提供了访问各种数据结构的一种机制

while(e.hasMoreElements())

{

Integer intObj=(Integer)e.nextElement();//不是指向下一个元素 而是指向当前元素

sum+=intObj.intValue();//不能使用nextElement()的直接返回对象 所以要记得要用类型转换

}

System.out.print(sum);

}

}

3.2  e.nextElement()指示器指向正指向的那个对象

如果为空 则e.hasMoreElement()false

Arrarylist需要进行同步处理

Collectionsetlist接口的父类 其中各个元素之间没有指定顺序 而且允许有重复和空对象 故而没有办法排序 set中各个元素无指定顺序 但不允许有空对象 不允许重复

List指定元素顺序 允许有重复和空对象 Arrarylist就是要实现List接口中的类

Collection主要操作集合对象 各种操作方法操作集合类对象

import java.util.*;

public class  testCollection

{

public static void main(String args[])

{

int b=0;

ArrayList v=new ArrayList();//可以存储各种类型的数据

System.out.println("please enter number:");

while(true)

{ try { b=System.in.read();}

catch(Exception e){e.printStackTrace();}

  if(b=='/r'||b=='/n')

   break;

   else

   {int num=b-'0';

   v.add(new Integer(num));

      }

}

int sum=0;

Iterator e=v.Iterator();//提供了访问各种数据结构的一种机制

while(e.hasNext())

{

Integer intObj=(Integer)e.next();//不是指向下一个元素 而是指向当前元素

sum+=intObj.intValue();//不能使用nextElement()的直接返回对象 所以要记得要用类型转换

}

System.out.print(sum);

}

}

3.3 Hashtable类 

高级数据结构类用于高级检索 不仅能像Vector一样动态存储一系列的对象 而且对每一个对象(称之为值)安排另一个对象与之关联

Hashtable number=new Hashtable();

Number.put("one",new Integer(1));

检索

Integer n= (Integer)number.get("two");

If(n!null)

{System.out.println("two"=+n);

}

用作关键字的类必须覆盖Object.hasCode方法和Object.equals方法

Object.hasCode返回值是由地址码转换而来的 如果对象的值相同 则Object.hasCode不相同 这也是为什么要覆盖的原因 

Hashtable()Enumeration返回关键字 Enumeration取出所有集合的机制 get()一直取出

import java.util.*;

public class HashtableTest {

/**

 * Method main

 *

 *

 * @param args

 *

 */

public static void main(String[] args) {

// TODO: Add your code here

Hashtable number=new Hashtable();

number.put(new MyKey("zhangshan",18),new Integer(1));

number.put(new MyKey("lisi",15),new Integer(2));

number.put(new MyKey("wangwu",20),new Integer(3));//进行比较后再取出来

Enumeration e=number.keys();

while(e.hasMoreElements())

{

MyKey key = (MyKey)e.nextElement();///取出关键字

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

    System.out.println(number.get(key));//关键字等于他所对应的值 直接取出值不进行比较

}

}

}

3.4 Properties

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

例子 使用Properties把程序的启动运行次数纪录在某个文件中 每次运行时候打印出运行次数

import java.util.Properties;

import java.io.*;

class PropertiesFlie {

/**

 * Method main

 *

 *

 * @param args

 *

 */

public static void main(String[] args) {

// TODO: Add your code here

long startTime =System.currentTimeMillis();

Properties settings=new Properties();//定义一个新的对象

try {

settings.load(new FileInputStream("count.txt"));//异常处理 假如没有输入

}catch(Exception e)

{

e.printStackTrace();//打印异常信息

settings.setProperty("count",String.valueOf(0));//0次运行 并整数转换成字符串

    }

int c=Integer.parseInt(settings.getProperty("count"))+1;//不运行为第一次要加1

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

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

settings.setProperty("count",new Integer(c).toString());//必须是字符串格式

try{settings.store(new FileOutputStream("count.txt"),"Program is used:");}

catch(Exception e)

{e.printStackTrace();

}

long endTime = System.currentTimeMillis();

System.out.print("totle running time"+(endTime-startTime));

}

}

4、System类 

与系统有关 所有方法均为静态类 标准输入输出等

Exit方法 正常结束 传值为非正常结束传值为非0

currentTimeMillis方法 

Java虚拟机的系统属性 可以被操作系统使用也可以增加新的系统属性

getProperties  setProperties获取和设置当前系统的属性

Runtime封装了JAVA虚拟机类型 很多方法与System方法重写 runtime.getRuntime获得对象的一个静态方法

Runtime.exe就是一个进程对象

例子 java程序中启动一个windows记事本的运行实例 并在运行实中打开这个java程序的源文件 启动的记事本程序5秒钟后关闭

5、日期 有关的类 

Date DataFormat Calendar 抽象类

Calendar.add方法

Calendar.get 获取年月日

Calendar.set修改年月日

Calendar唯一子类是GregorianCalendar  Calendar.getInstance静态方法返回子类对象

Timer 

TimerTask

定时器类Timerjava.util包中。使用时,先实例化,然后使用实例的schedule(TimerTask task, long delay)方法,设定指定的任务task在指定的延迟delay后执行(只执行一次)。定时器任务类TimerTask是抽象类,继承并重写其run()方法,可实现具体任务。

cancel()方法结束这个定时器。

schedule(TimerTask task, long delay, long period)方法设定指定任务task在指定延迟delay后进行固定延迟peroid的执行(多次执行)。

timer.schedule(new MyTask(),long time1,long timer2);

第一个参数,是 TimerTask 类,在包:import java.util.TimerTask .使用者要继承该类,并实现 public void run() 方法,因为 TimerTask 类 实现了 Runnable 接口。

第二个参数的意思是,当你调用该方法后,该方法必然会调用 TimerTask 类 TimerTask 类 中的 run() 方法,这个参数就是这两者之间的差值,转换成汉语的意思就是说,用户调用 schedule() 方法后,要等待这么长的时间才可以第一次执行 run() 方法。

第三个参数的意思就是,第一次调用之后,从第二次开始每隔多长的时间调用一次 run() 方法。

Mathrandom 伪随机数产生器 相同启动数字启动的Random随机数相同

import java.util.*;

import java.text.SimpleDateFormat;

public class testCalendar {

/**

 * Method main

 *

 *

 * @param args

 *

 */

public static void main(String[] args) {

// TODO: Add your code here

Calendar cl = Calendar.getInstance();

System.out.print(cl.get(Calendar.YEAR)+""+cl.get(cl.MONTH)+""+cl.get(cl.DAY_OF_MONTH)+"日 "+cl.get(cl.HOUR_OF_DAY)+":"

+cl.get(cl.MINUTE)+":"+cl.get(cl.SECOND)+":");

cl.add(cl.DAY_OF_YEAR,315);

System.out.print(cl.get(Calendar.YEAR)+""+cl.get(cl.MONTH)+""+cl.get(cl.DAY_OF_MONTH)+"日 "+cl.get(cl.HOUR_OF_DAY)+":"

+cl.get(cl.MINUTE)+":"+cl.get(cl.SECOND)+":");

        SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd");

        SimpleDateFormat sdf2=new SimpleDateFormat("yyyyMMdd");

        try{

        Date d =sdf1.parse("2003-03-15");

System.out.println(sdf2.format(d));

}catch(Exception e){e.printStackTrace();

}

new Timer().schedule(new TimerTask(){//Timer 做为非daemon线程的话 会一直执行

public void run()

{try{

Runtime.getRuntime().exec("calc.exe");

}

catch(Exception e){}

}//结束任务线程的代码

//Timer.cancel();

//this.cancel();并不取消任务线程

},3000);

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值