java 第二课

 

API(Application Programming Interface)
Java API--是JDK中提供的各种java类
Java开发工具
Borland公司的Jubilder
IBM公司的Visual Age
Sun公司的Sun ONE Studio
赛门铁克的Visual cafe
Jcreator


String和StringBuffer  --位于java.lang
String类对象中的内容一旦被初始化就不能再改变
StringBuffer类用于封装内容可以改变的字符串
用toString方法转换成String类型
String x="a"+4+"c";编译时等效于
String x=new StringBuffer().append("a").append(4).append("c").toString();
字符串常量(“Hello”)实际上是一种特殊的匿名String对象
String s1=“hello”; String s2="hello" 用“=”操作符是ture
String s1=new String("hello"); String s2=new String("hello"); 用“=”操作符是false
编程实列:逐行读取键盘输入,知道输入的内容为“bye”时,结束程序。
public class ReadLine throws IOException{
 public static void main(String args){
 byte [] buf=new byte[1024];//输入的内容不能超过1024
 String strInfo=null;  //定义字符串对象
 int pos=0;
 int ch=0;
 while(true){
 
   ch=System.in.read();
   switch(ch)
 {
  case '\r':
   break;
  case '\n':
   strInfo=new String(buf,0,pos);
   if(strInfo.equal("byte")){
    return;
}
 else{
  System.out.println("strInfo");
  pos=0;
  break;
}
 default:
   buf[pos++]=(byte)ch;
 
}
}
}

String 类的常用成员方法
构造方法:
String(byte[] byte,int offset,int length)
equalslgnoreCase 方法:不区分大小写
indexOf(int ch)返回字符首次出现的问题

System.out.println("hell word!".indexOf('o'));

substing(int beginIndex)
System.out.println("hello word".substring(6));//意思就是用第六个字符开始,后面的都作为子字符串返回
substring(int beginIndex,int endIndex)返回的是从beginIndex到endIndex-1的字符


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

打印*矩形
public class TestInteger{
 int w=new Integer(args[0].inValue());
 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());
}
}

集合类
Vector Enumeration ArrayList Collection Lterator Set List
编程举例:将键盘输入的一个数字序列中的每位数字存储在Vector对象中,然后在屏幕上打印出每位数字相加的结果,例如,输入32,打印出5,输入1234,打印出10.
import java.util.*;
public class TestVector{

 public static void main(String args[])throws
IOException{
 int b=0;
 Vector v=new Vector();
 System.out.println("please enter number:");
 while(true){
 int b=System.in.read();
 if(b=='\r'||b=='\n')
 break;
 else{
 int num=b-'0';//  '1'=49'0'=48;
  v.addElement(new Integer(num));
 }
}
 int sum=0;
 Enumeration e=v.elements();
 while(e.hasMoreElements()){
 Integer intObj=(Integer)e.nextElement();//返回指示元素--一个指示器--返回的是一个object
 sum+=intObj.intValue();
}
 System.out.println(sum);
}
}

 

public class TestSort{
 public static void main(String args[]){
 ArrayList al=new ArrayList();
 a1.add(new Integer(1));
 a1.add(new Integer(3));
 a1.add(new Integer(2));
 System.out.println(a1.toString());
 Collection.sort(a1);
 System.out.println("a1.toString()");

}
}

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


Hashtable类:
Hashtable numbers=new Hashtable();
numbers.put("one",new Integer(1));
numbers.put("two",new Integer(2));
numbers.put("three",new Integer(3));

Integer n=(Integer)numbers.get("two")
if(n!=null)
{
 System.out.println("two="+n);
}


Properties类
Properties类是Hashtable的子类--所有的属性都是String
编程举例:使用Properties把程序的启动运行次数记录在men个文件中,每次运行时打印出他的运行次数

public class PropertiesFile{

public static void main (String args[]){
   Properties setting=new Properties();
   setting.load(FileInputStream("coun.txt"));
   setting.get("count");//因为这个里得到的是object
  try{
   setting.getProperty("count");
}catch(Exception e){
  setting.setProperty("count",String.valueOf(0));//整数转化为字符串
}
   int c=Integer.parseInt(setting.getProperty());
   System.out.println("这是第"++"运行");
    setting.put("count",new Integer(c).toString());//
    setting.setProperty("count",new Integer(c).toString());
    try{
    setting.store(FileOutputStream("count.txt"),"Program is used:");//存入的文件
}catch(Exception r){e.printStackTrace();}
}
}


SystemRuntime类
-exit方法--正常操作要终止程序传0,程序有异常,要异常终止程序传一个非0值
-currentTimeMillis方法--long--返回1970年1月1日0时0分0秒 
                      
public class PropertiesFile{

public static void main (String args[]){
  long startTime=System.currentTimeMillis();
   Properties setting=new Properties();
   setting.load(FileInputStream("coun.txt"));
   setting.get("count");//因为这个里得到的是object
  try{
   setting.getProperty("count");
}catch(Exception e){
  setting.setProperty("count",String.valueOf(0));//整数转化为字符串
}
   int c=Integer.parseInt(setting.getProperty());
   System.out.println("这是第"++"运行");
    setting.put("count",new Integer(c).toString());//
    setting.setProperty("count",new Integer(c).toString());
    try{
    setting.store(FileOutputStream("count.txt"),"Program is used:");//存入的文件
}catch(Exception r){e.printStackTrace();}
}
 long endTime=Sys.currentTimeMillis();//检测一段程序运行的时间值
 System.out.println("total runtime time:"+(endTime-starTime));//从开始到最后用了多少时间
}


java虚拟机的系统属性
java -dAAA=b -DBB=a MyClass  增加系统属性一个AAA一个BBB
取出java虚拟机的系统属性
getProperties和setProperties
取出系统中所有属性的名称

public class TestProperties{
 public static void main(String args[]){
 Properties sp=Syste.getProperties();
 Enumeration e=sp.propertyName();
 while(e.hasMoreElements()){
 String key=(String)e.netxElement();
 System.out.println(key+"="+sp.getProperty(key));
}
}
}

Runtime类
-不能直接创建实例对象
Runtime.getRuntime--静态方法
编程实例:在java程序中启动一个Windows记事本程序的运行实例,并在运行实例中打开这个java程序的源文件,启动的记事本程序5秒钟后被关闭。
public class TestProperties{
 public static void main(String args[]){
 Properties sp=Syste.getProperties();
 Enumeration e=sp.propertyName();
 while(e.hasMoreElements()){
 String key=(String)e.netxElement();
 System.out.println(key+"="+sp.getProperty(key));
}
Process p=null;//启动的一个进程
try{
p=Runtime.getRuntime().exec("notepad.exe TestProperties.java");
Thread.sleep(5000);
p.destroy();//关闭
}catch(Exception ex){
e.pringStackTrace();
}
}
}

与日期和时间有关的类
Date,DateFormat,Calendar
Calendar类:
--Calendar.add--在他上面可以增加若干个年月日
--Calendar.get--获取
--Calendar.set--修改
--Calendar.getInstance静态方法
子类--GregorianCalendar
编程实例:计算出距当前日期时间315天后的日期时间,并用“XXX年XX月XX日XX小时:XX分XX秒”的格式输出。
public calss TestCalendar{
 public static void main(Stringargs[]){
 Calendar c1=Calendar.getInstance();
 System.out.println(c1.get(Calendar,YEAR)+"年"+c1.get(c1.Month)+"月"+c1.get(c1.DAY_OF_MONTH)+"日"+c1.get(c1.HOUR)+"小时"+c1.get(c1.MINUTE)+"分"+c1.get(c1.SECONDS)+"秒");
C1.add(c1.DAY_OR_YEAR,315);
 System.out.println(c1.get(Calendar,YEAR)+"年"+c1.get(c1.Month)+"月"+c1.get(c1.DAY_OF_MONTH)+"日"+c1.get(c1.HOUR)+"小时"+c1.get(c1.MINUTE)+"分"+c1.get(c1.SECONDS))+"秒";

}

}

Date类
java.text.DateFormat与java.text.SimpleDateFormat子类
编程实例:将"2002-03-15"格式的日期字符串换成"2002年03月15日"的格式
public calss TestCalendar{
 public static void main(Stringargs[]){
 Calendar c1=Calendar.getInstance();
 System.out.println(c1.get(Calendar,YEAR)+"年"+c1.get(c1.Month)+"月"+c1.get(c1.DAY_OF_MONTH)+"日"+c1.get(c1.HOUR)+"小时"+c1.get(c1.MINUTE)+"分"+c1.get(c1.SECONDS)+"秒");
C1.add(c1.DAY_OR_YEAR,315);
 System.out.println(c1.get(Calendar,YEAR)+"年"+c1.get(c1.Month)+"月"+c1.get(c1.DAY_OF_MONTH)+"日"+c1.get(c1.HOUR)+"小时"+c1.get(c1.MINUTE)+"分"+c1.get(c1.SECONDS))+"秒";
SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf2=new SimpleDateFormat("yyyy年MM月dd日");
try{
Date d=sdf1.parse("2003-03-15");
System.out.println(sdf2.format(d));
}catch(Exception e){
e.printStackTrace();
}
}
}

Time与TimerTask类
schedule方法:
schedule(TimerTask,long delay)
schedule(TimerTask,Date time)
schedule(TimerTask,long delay,long period)
schedule(TimerTask task,Date firstTime,long period)
TimerTask类实现了Runnable接口,执行任务时由它里面实现的run方法类完成
编程实例:程序启动运行后30秒启动Windows自带的计数器程序。
public calss TestCalendar{
 public static void main(Stringargs[]){
 Calendar c1=Calendar.getInstance();
 System.out.println(c1.get(Calendar,YEAR)+"年"+c1.get(c1.Month)+"月"+c1.get(c1.DAY_OF_MONTH)+"日"+c1.get(c1.HOUR)+"小时"+c1.get(c1.MINUTE)+"分"+c1.get(c1.SECONDS)+"秒");
C1.add(c1.DAY_OR_YEAR,315);
 System.out.println(c1.get(Calendar,YEAR)+"年"+c1.get(c1.Month)+"月"+c1.get(c1.DAY_OF_MONTH)+"日"+c1.get(c1.HOUR)+"小时"+c1.get(c1.MINUTE)+"分"+c1.get(c1.SECONDS))+"秒";
SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf2=new SimpleDateFormat("yyyy年MM月dd日");
try{
Date d=sdf1.parse("2003-03-15");
System.out.println(sdf2.format(d));
}catch(Exception e){
e.printStackTrace();
}
class MyTimerTask extends TimerTask()
{
private Timer tm=null;
public MyTimerTask(Timer tm){
 this.tm=tm;
}
 public void run(){
try{
 Runtime.getRuntime().exec("calc.exe");
}catch(Exception e){
 e.printStackTrace();
}
//结束任务线程的代码
tm.cancel();
TimerTask.cancel();//
 
}
}
Timer tm=new Timer();
tm.schedule(new MyTimerTask(tm),30000);
//new Thread(new MyTimerTask().star())
}
}

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

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值