JavaAPI 1-9的内容

来源:互联网 发布:数控g92螺纹编程格式 编辑:程序博客网 时间:2024/06/03 23:49

一:API的概念
Windows API
Java API:JDK中提供的各种java类,如System类

JCreator同时只能有一个活动工程

 

二:
String类和StringBuffer类
     位于java.lang包中,可以直接使用,不必导入
     String类对象中的内容一旦被初始化就不能再改变
     StringBuffer类用于封装内容可以改变的字符串
     用toString方法转换成String类型
     String x = "a"+4+"c";编译时等效于:
     String x= new StringBuffer().apend("a").append("4").append("c").toString;
     字符串常量(如"hello")实际上是一种特殊的匿名String对象
     构造方法:String(byte[] bytes,int offset,int length)
     equalsIgnoreCase
     indexOf()
     substring(int beginIndex)
     substring(int beginIndex,int endIndex)

     编程实例:逐行读取键盘上输入的字符,直到输入内容为"bye"时,结束程         序 
     class ReadLine
     {
           public void static main()
  
           {     
                   int ch=0;
                   byte[] buff=new byte[1024];
                   String infor = new String();
                   int pos=0;
                   System.out.println("Please input information:");
                   while(true)
                   {   
                        try
                        {
                           ch=System.in.read()
                        }
                        catch(exception e)
                        {
                           e.printStackTrace();
                        }
                        switch(ch)
                        {
                              case '/r':
                                        break;
                              case '/n':
                                        if(infor.equals("bye"))
                                        return;
                                        infor= new String(buff,0,pos);
                                        System.out.println(infor);
                                        pos=0;
                                        break;
                              default:
                                        buff[pose++]=char(ch);
                        }
                       
                       
                   }  
           }
     }

 

三:基本数据类型的对象包装类
    基本数据类型的对象包装类的作用
        int          Integer
        short        Short
编程实例:在屏幕上打印出一个星号组成的矩形,矩形的宽度和高度通过启动程序时传递给main的参数指定,并比较下面2段代码的运行效率:
  Sring sb=new String;          StringBuffer sb=new StringBuffer();
  for(int j=0;j<w;j++)          for(int j=0;j<w;j++)
  {                             {
     sb = sb + '*';                  sb.append('*');
  }                             }

public class TestInteger
{
public static void main(String[] args)
       {
             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(j=0;j<w;j++)
                 sb.append('*');
                 System.out.println(sb.toString());
       }
}

 

四:集合类
    集合类用于存储一组对象,其中的每个对象称为元素,常用的集合类有,       Vector,Enumeration,ArrayList,Collection,Iterator,Set,List等集合       类和接口
       Vector类与Enumeration接口
       编程举例:将键盘上输入的一个数字序列中的每位数字存储在Vecter对象       中,然后在屏幕上打印出每位数字相加的结果,例如:输入32,打印出5
public class Testvector
{     public static void main(String[] args)
      {      Vector v = new Vecror();
             int b = 0;
             System.out.println("Please enter a number:");
             while(true)
             {
                   try
                   {
                       b = System.in.read();
                   }
                   catch(exception e)
                   {
                       e.PtintStackTrace();
                   }
                   int num = b - '0';
                   if(b == '/r' || b == '/n')
                   {
                      break;
                   }

                   else if(num < 0 || b > 9)
                   {
                      System.out.println("Please enter a number                       between 0 - 9");
                      break;
                   }
                   else
                   {
                       v.addElement(new Integer(num));
                   }
             } 
             int sum=0;
             Enumeration e = v.elements();
             while(e.hasMoreElements())
             {
                     Integer obj = Iteger(e.nextElement());
                     sum += obj.intValue();
             }
             System.out.println(sum);
      } 
}

  Collection接口和Iterator接口
      Vector线程同步
      ArrayList不同步
  Collection,Set,List的区别
      C是S,L的父类
      C各元素对象直接没指定的顺序,允许有重复元素和多个null元素
      S各直接没有指定顺序,不允许有重复元素,最多有1个null元素对象
      L有指定顺序,允许重复和多个null
     

五:Hashtable类
   不仅可以像Vector一样动态存储一系列对象,而且对存储的每个对象(称为值)   都要安排另外一个对象(称为关键字) 与之想关联
   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("one");
  if(n != null)
  {
     System.out.println("one="+n);
  }

   用在关键字的类必须覆盖Object.hashCode和Object.equals方法
   编程举例:使用自定义类作为Hashtable的关键字
public class Keytest
{
    String name = null;
    int age = 0;
    public boolean equals(Obj obj)
    {
        if(obj instanceof Keytest)
        {
            if(obj.name.equals(name) && obj.age==age)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }
  
   public int hashCode()
   {
        return name.hashCode() + age; 
   }
}

 

 

六:Properties类
     Properties类是Hashtable的子类
     增加了将Hashtable对象中的关键字和值保存到文件和从文件中读取关键字          到Hashtable对象中的方法
     如果要用Properties.store方法存储Properties对象中的内容每个属性的关        键字和值都必须是String类型
    编程实例:使用Properties把程序的启动运行次数记录在某个文件中,每次              运行时打印出运行次数
    System与Runtime类
          System类
              exit方法 结束java虚拟机运行
              currentTimeMillis方法 返回long
              Java虚拟机的系统属性
              getProperties和setProperties方法
           Runtime类
               Runtime.getRuntime静态方法获得Runtime实例对象的引用
                   

 

七:与日期和时间有关的类
    最常用的几个类:Date,DateFormat,Calendar
    Calendar类
           Calendar.add方法
           Calendar.get
           Calendar.set
           Calendar.getInstance静态方法
           GregorianCalendar子类

Timer与TimerTask类
    schedule方法有以下几种重载形式:
        schedule(TimerTask task,long delay)
        schedule(TimerTask task,Date time)
        schedule(TimerTask task,long delay,long period)
        schedule(TimerTask task,Date firstTime,long period)
TimeTask类实现了Runnable接口,要执行的任务由它里面的run方法来完成。
   

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

原创粉丝点击