java API学习记录

API(ApplicationProgrammingInterface)


1.String和StringBuffer类位于java.lang中

Stringx="a"+4+"c";

字符串常量实际上就是一种特殊的匿名String对象

System.in.read();可以从键盘读取一个字节,需要捕捉IOException异常


[code="java"]publicclassReadLine
{
publicstaticvoidmain(String[]args)
{
byte[]buf=newbyte[1024];
StringstrInfo=null;
intpos=0;
intch=0;
while(true)
{
try{
ch=System.in.read();
}catch(IOExceptione){
e.printStackTrace();
}
switch(ch)
{
case'/r':
break;
case'/n':
strInfo=newString(buf,0,pos);
if(strInfo.equals("bye"))
{
return;
}
else
{
System.out.println(strInfo);
pos=0;
break;
}
default:
buf[pos++]=(byte)ch;
break;
}
}
}
}


构造方法:String(byte[]bytes,intoffset,intlength)

indexOf(intch)方法:用于返回字符在字符串首次出现的位置,没有返回-1

Substring(intbeginIndex,intendIndex):返回其间的字符

toString():返回一个String类型对象

2.基本数据类型的对象包装类

许多方法中都使用了Object对象,如果需要使用这样的方法就必须使用包装类

第三种方法与前两种不同的是第三种方法转换之后是一个Integer类型

集合类用于存储一组对象,其中每个对象称之为元素,经常会用到的 Vector,Enumeration,ArrayList,Collection,lterator,Set,List等

如果不能预先确定预先要保存的对象的数目,或者需要方便获得某个对象的存放 位置时,可以选择Vector类

[code="java"]publicclassTestVector
{
@SuppressWarnings("unchecked")
publicstaticvoidmain(Stringargs[])
{
Vectorv=newVector();
intb=0;
System.out.println("pleaseenternumber:");
while(true)
{
try{
b=System.in.read();
}catch(IOExceptione){
e.printStackTrace();
}
if(b=='/r'||b=='/n')
{
break;
}
else
{
intnum=b-'0';
v.addElement(newInteger(num));
//向Vector对象中加入这个整数对象
}
}
intsum=0;
Enumeratione=v.elements();
//v.elements()返回一个Enumeration接口对象
while(e.hasMoreElements()){//如果还有元素,指示器将返回true
IntegerintObj=(Integer)e.nextElement();
//nextElement()返回的是一个对象
//返回指示器正在指向的那个对象,并将指示器指向下一个对象
sum+=intObj.intValue();
//intValue可以把Integer对象转换为int类型
}
System.out.println(sum);
}
}

(2).Collection接口与Iterator接口


[code="java"]importjava.io.IOException;
importjava.util.ArrayList;
importjava.util.Iterator;

publicclassTestCollection
{
@SuppressWarnings("unchecked")
publicstaticvoidmain(Stringargs[])
{
ArrayListv=newArrayList();
intb=0;
System.out.println("pleaseenternumber:");
while(true)
{
try{
b=System.in.read();
}catch(IOExceptione){
e.printStackTrace();
}
if(b=='/r'||b=='/n')
{
break;
}
else
{
intnum=b-'0';
v.add(newInteger(num));
//向ArrayList对象中加入这个整数对象
}
}
intsum=0;
Iteratore=v.iterator();
//v.iterator()返回一个Enumeration接口对象
while(e.hasNext()){//如果还有元素,指示器将返回true
IntegerintObj=(Integer)e.next();
//next()返回的是一个对象
//返回指示器正在指向的那个对象,并将指示器指向下一个对象
sum+=intObj.intValue();
//intValue可以把Integer对象转换为int类型
}
System.out.println(sum);
}
}

以下是JDK文档中的提示:

(3).Collection,Set,List区别:

Collection各对象之间没有指定的顺序,允许有重复元素和多个null元素对象

List各元素对象之间有指定顺序,允许重复对象,和多个null元素对象

[code="java"]importjava.util.ArrayList;
importjava.util.Collections;
publicclassTestSort
{
@SuppressWarnings("unchecked")
publicstaticvoidmain(String[]args)
{
ArrayListal=newArrayList();
al.add(newInteger(1));
al.add(newInteger(3));
al.add(newInteger(2));
System.out.println(al.toString());
Collections.sort(al);//用来操作集合类对象
System.out.println(al.toString());
}
}

(4).Hashtable类

代码如下:

[code="java"]importjava.util.Enumeration;
importjava.util.Hashtable;

publicclassHashtableTest
{
@SuppressWarnings("unchecked")
publicstaticvoidmain(String[]args)
{
Hashtablenumbers=newHashtable();
numbers.put(newMyKey("zhangsan",18),newInteger(1));
numbers.put(newMyKey("lisi",15),newInteger(2));
numbers.put(newMyKey("wangwu",20),newInteger(3));
Enumeratione=numbers.keys();
//取出所有关键字的集合(Integer)
while(e.hasMoreElements()){
MyKeykey=(MyKey)e.nextElement();
System.out.print(key.toString()+"=");
//key隐式的调用toString方法,可以直接写成key...
System.out.println(numbers.get(key));
//这里的key是直接从hastable里面取出来的,不用再比较了
//key是hastable的一部分,这样检索比较方便
}
System.out.println(numbers.get(new MyKey("zhangsan",18))); }
}
publicclassMyKey
{
privateStringname=null;
privateintage=0;
publicMyKey(Stringstring,inti){
this.name=string;
this.age=i;
}
publicStringtoString()
{
returnname+","+age;
}
//equals方法,hashCode方法并不是被程序调用的,而是被get方法调用
//numbers.get(key)
publicbooleanequals(Objectobj)
{
if(objinstanceofMyKey)
{
MyKeyobjTemp=(MyKey)obj;
if(name.equals(objTemp.name)&&age==objTemp.age)
{
return true;
}
else
{
return false;
}
}
else{
returnfalse;
}
}
publicinthashCode(){
returnname.hashCode()+age;
}
}

(5).Properties类是Hashtable的子类

示例代码:

#Programisused:

count=0

[code="java"]importjava.util.Properties;
importjava.io.*;

publicclassPropertiesFile
{
publicstaticvoidmain(Stringargs[])
{
Propertiessettings=newProperties();
try{
settings.load(newFileInputStream("C://Users//姜康 //Desktop//java//count.txt"));
}catch(IOExceptione){
e.printStackTrace();
settings.setProperty("count",String.valueOf(0));
System.out.println("!!");
}
//settings.get("count");需要转换成字符串
intc=Integer.parseInt(settings.getProperty("count"))+1;
//getProperty直接得到一个字符串
System.out.println("这个第"+c+"次运行");
//settings.put("count",newInteger(c).toString());
//Integer(c).toString()把整数类型转换为string类型
//put方法可以接收非字符串对象做参数,setProperty只能是字符串
settings.setProperty("count",newInteger(c).toString());
try{
settings.store(newFileOutputStream("C://Users//姜康 //Desktop//java//count.txt"),"Programisused:");
}catch(IOExceptione){
System.out.println(e.getMessage());
}
//把properties属性列表加入到输出流中,第二个参数是设置标题
}
}
(6).System与Runtime类

JVM的系统属性

Runtime类

不能通过new来创建Runtime

实例程序:

[code="java"]importjava.util.*;
publicclassTestProperties、
{
@SuppressWarnings("unchecked")
publicstaticvoidmain(String[]args)
{
Propertiessp=System.getProperties();
Enumeratione=sp.propertyNames();
while(e.hasMoreElements())
{
Stringkey=(String)e.nextElement();
System.out.println(key+"="+sp.getProperty(key));
}
}
}
下面一个例子是打开一个文件,然后几秒钟后关闭

[code="java"]packageapi.test;
importjava.io.IOException;
importjava.util.*;

publicclassTestProperties
{
@SuppressWarnings("unchecked")
publicstaticvoidmain(String[]args)
{
//Process为一个类,代表JVM启动的一个子线程
Processp=null;
try{
p = Runtime.getRuntime().exec("notepad.exeC://Users// 姜康//Desktop//java//Test.java");
}catch(IOExceptione1){
//TODOAuto-generatedcatchblock
e1.printStackTrace();
}
try{
Thread.sleep(5000);
}catch(InterruptedExceptione1){
//TODOAuto-generatedcatchblock
e1.printStackTrace();
}
p.destroy();
}
}

(7).与日期和时间有关的类

Calendar类:add()方法

Set()方法修改年月日...

GregorianCalendar子类



@SuppressWarnings("static-access")

Calendarc1=Calendar.getInstance();

System.out.print(c1.get(c1.YEAR)+"年"+c1.get(c1.MONTH)+"月"+c1.get(Calendar.DAY_OF_MONTH)+"日");

c1.add(c1.DAY_OF_YEAR,315);

System.out.println(c1.get(c1.HOUR)+":"+c1.get(c1.MINUTE)+":"+c1.get(c1.SECOND));

}



[code="java"]importjava.util.Date;
importjava.text.ParseException;
importjava.text.SimpleDateFormat;
publicclassTestCalendar
{
publicstaticvoidmain(String[]args)
{
SimpleDateFormatsdf1=new
SimpleDateFormat("yyyy-mm-dd");
SimpleDateFormatsdf2=new
SimpleDateFormat("yyyy年mm月dd");
Dated=null;
try
{
d=sdf1.parse("2003-03-15");
}catch(ParseExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
System.out.println(sdf2.format(d));
//用于把指定对象的值输出
}
}
(8).Timer/TimerTask类

与每个Timer对象相对应的是单个后台线程,用于顺序地执行所有计时器任务。计时器任务应该迅速完成。如果完成某个计时器任务的时间太长,那么它会“独占”计时器的任务执行线程。因此,这就可能延迟后续任务的执行,而这些任务就可能“堆在一起”,并且在上述令人讨厌的任务最终完成时才能够被快速连续地执行。

Schedule(TimerTasktask,longdelay)

TimerTask实现Runable接口,要执行的任务由它里面实现的run方法完成

[code="java"]importjava.util.Timer;
importjava.util.TimerTask;
publicclassTestTimer{
publicstaticvoidmain(Stringargs[])
{
//使用匿名对象来完成,无法访问到Timer对象
/*newTimer().schedule({
//需要覆盖子类的run方法
publicvoidrun(){
try{
Runtime.getRuntime().exec("calc.exe");
}catch(Exceptione){
e.printStackTrace();
}
}
},1000);*/
//结束任务线程的代码
Timertm=newTimer();
tm.schedule(newMyTimerTask(tm),3000);
}
}
classMyTimerTaskextendsTimerTask{
@SuppressWarnings("unused")
privateTimertm=null;
//传递过来的是tm对象
publicMyTimerTask(Timertm){
this.tm=tm;
}
publicvoidrun(){
try{
Runtime.getRuntime().exec("calc.exe");
}catch(Exceptione){
e.printStackTrace();
}
tm.cancel();
//可以结束线程
}
}

(9).Math与Random类

Random类是一个伪随机数产生器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值