Java-javaAPI 知识点

一、API: Application Programming Interface

 

二、String和StringBuffer类

    ①String类:对象中的内容初始化就不可再更改;
       StringBuffer类:封装内容可以改变;
       构造方法String(byte[]bytes,int offset,int length)//用于将字符数组转换成字符串,从字符数组中的offset位置起,length个字节组成的字符串对象;
       equalsignoreCase()方法:忽略大小写,比较两个字符的值是否相等;
       indexOf(int ch)一个字符在字符串中首次出现的位置,没有则返回-1;从指定位置开始查找:indexOf('ch',num);
       substring(int beginIndex,int endIndex)
②编程实例:从键盘读取并打印字符串,直到bye退出。 
   public class ReadLine
    {
           public static void main(String [] args)
             {
                   byte buf[]=new byte[1024];
                   String strInfo=null;//定义一个字符串,将buf转化成字符,此处对于方法内部定义的变量,需有显式初始化
                   int pos=0;
                   int ch=0;//放到外面才可用
                  System.out.println("please enter info:");
                  while(true)
                  {
                             try
                            {           
                                  ch=System.in.read();
                           }
                             catch(Exception e)
                           {
                                  e.printStackTrace();
                          }
                switch(ch)
               {
                   case '/r'://是回车不进行处理
                                  break;
                    case '/n'://换行,则将输入字节转换成字符串
                    strInfo= new String(buf,0,pos);//buf里面第0个字节开始pos个内容装换成字符串
                     if(strInfo.equals("bye"))
                                  return ;
                     else
                                   System.out.println(strInfo);
                                   pos=0;
                                   break;
                     default:
                     buf[pos++]=(byte)ch;
                             }
                    }
             }
    }


三 、基本数据类型和对象包装类
基本数据类型和对象包装类的对应关系:
boolen-Boolen/byte-Byte/char-Character/short-Short/int-Integer/long-Long/float-Float/double-Double

字符串转换成整数方法举例(三种方法)
代码:
class TestInteger
{
        public static void main(String[] args)
         {
                  int w = Integer.parseInt(args[0]);  //第一种方法
                  int h = new  Integer(args[1]).intValue(); //第二种方法
                  //int h = Integer.valueOf(args[1]).intValue(); 第三种方法
                  for(int i=0;i<h;i++)
                    {
                            StringBuffer sb=new StringBuffer();//StringBuffer允许改变字符串内容
                            for(int j=0 ;j<w;j++)
                            {
                                 sb.append('*');
                            }
                 System.out.println(sb.toString());
                                  }
                 }
}


四、集合类
①用于存储一组对象,其中的每个对象称之为元素,常用的有:Vector、Enumeration、ArrayList、Collection、Iterator、Set、List


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)
                      {
                              System.out.println(e.getMessage());
                      }
                   if(b=='/r' || b== '/n')
                              break;
                   else
                  {
                               int num=b-'0';//得出数字字符所对应整数,如“1” -“0”= 49-48 = 1!!!!
                                v.addElement(new Integer(num));//增加元素
                  }
               }
              int sum=0;
              Enumeration e=v.elements();    
               while(e.hasMoreElements())
               {
                               Integer intObj=(Integer)e.nextElement();
                                sum += intObj.intValue();
                 }
               System.out.println(sum);
                       }
   }


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

 

对ArrayList实例对象中的元素进行排序:
import java.util.*;
public class TestSort
{
 public static void main(String[] args)
 {
  ArrayList al=new ArrayList();
  al.add(new Integer(1));
  al.add(new Integer(3));
  al.add(new Integer(2));
  System.out.println(al.toString()); //排序前,调用toString转换为字符串
  Collections.sort(al);//所有成员方法都是静态的
  System.out.println(al.toString()); //排序后
 }
}

 

五、hasetable类与Properties类

①hasetable类
可以像Vector一样动态存储一系列的对象,而且对存储的每一个对象(称为值)都要安排另一个对象(成为关键字)与之相关联
注意,用作关键字的类必须覆盖Object.haseCode方法和Object.equals方法。

class MyKey
{
 private String name;
 private int age;
 public MyKey(String name,int age)
 {
  this.name = name;
  this.age = age;
 }
 public String toString()
 {
  return new String(name + "," + age);
 }
 public boolean equals(Object obj)
 {
  if(obj instanceof MyKey)
  {
   MyKey objTemp = (MyKey)obj;//类型转换,转换成MyKey
   if(name.equals(objTemp.name) && age==objTemp.age)
   {
    return true;
   }
   else
   {
    return false;
   }
  }
  
  else
  {
   return false;
  }
 }
 public int hashCode()
 {
  return name.hashCode() + age;
 }
}

②Properties类

Properties.store 存储Properties对象的内容,每个属性的关键字和值都必须是String类型。
 
*使用Properties把程序的启动运行次数记录在某个文件中,并打印次数
 import java.util.*;
import java.io.*;
class PropertiesFile
{
 public static void main(String[] args)
 {
  Properties settings=new Properties();
  try
  {
   settings.load(new FileInputStream("c://count.txt"));//load方法
  }
  catch(Exception e)
  {
   settings.setProperty("Count",new Integer(0).toString());// String.valueOF(0)
  }
  int c=Integer.parseInt(settings.getProperty("Count"))+1;
  System.out.println("这是本程序第"+c+"次被使用");
  settings.put setProperty("Count",new Integer(c).toString());//将整数c转换成字符串,非String类型时处理
  try
  {
   settings.store(new FileOutputStream("count.txt"),//存储到文件中
   "This Program is used:");//标题信息
  }
  catch(Exception e)
  {
   System.out.println(e.getMessage());
  }
 }
}

 

System与Runtime类
①System类
   exit方法:结束JVM运行

   currentTimeMillis方法:
  使用方法:    long endTime = System.currentTimeMillis();
                    System.out.println(""+(endTime - startTime));

  getProperties:获取系统属性
  setProperties:设置系统属性
   查看并打印系统属性
import java.util.*;
public class SystemInfo
{
 public static void main(String[] args)
 {
  Properties sp=System.getProperties();提取所有的属性
  Enumeration e=sp.propertyNames();//表示集合中所有元素的抽象机制,Enumeration的propertyNames方法得到所有的属性
  while(e.hasMoreElements())//是否有更多元素
  {
   String key=(String)e.nextElement();//有,用nextElement()取出下一个元素
   System.out.println(key+" = "+sp.getProperty(key));
  }
 }
  }


②Runtime类:不能通过new创建一个Runtime实例对象,用Runtime.getRuntime静态方法来获得实例对象的引用。
  JAVA程序启动记事本程序,5秒后被关闭。
 public class TestRuntime
{
 public static void main(String[] args)
 {
  Process p=null;
        try
  {
   p=Runtime.getRuntime().exec("notepad.exe TestProperty.java");
            Thread.sleep(5000);
     }
     catch(Exception e)
     {
       e.printStackTrace();
     }
     p.destroy();//关闭p对应进程
 }
}


六、与日期和时间有关的类
常用的类:Date、DateFormat和Calender类
①Calendar :315天后日期,来理解各种方法

②Timer与TimerTask类
   scedule方法主要有如下重载形式;
   TimerTask task 指定期的执行任务代码

③编程:10秒运行计算器:
 /*new timer()创建的对象默认作为非daem线程启动
 new Timer(true).schedule()
 run方法后加结束任务线程代码:Timer.cancel()41‘30“处解决方案*/


④用simpleDataFormat转换时间格式:

import java.util.*;
import java.text.*;
public class TestDateFormat
{
 public static void main(String[] args)
 {
  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)
  {
   System.out.println(e.getMessage());
  }
 }
}

七、Math与random类
      math类包含了所有用于几何和三角形运算的方法
      random类是一个伪随机函数 按某种算法,若使用相同初值,随机序列都是相同固定的,通过不带参数的构造函数使用当前时间作为初  始值,减少随机数相同的可能性

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值