黑马程序员06常用API

来源:互联网 发布:百度云 seo 编辑:程序博客网 时间:2024/05/22 06:04

------- android培训、java培训、期待与您交流! ----------

概念:API全名:application(应用) programming(程序) interface(接口)

API是应用程序编程接口

Java API

Java API就是sun公司提供给我们使用的类,这些类将底层的实现封装了起来,我们不需要关心这些类是如何实现的,只需要学习这些类如何使用。

我们可以通过查帮助文档来了解Java提供的API如何使用

 

一、String

String是一个特殊的对象,一旦被初始化,就不会被改变。.

String s1="abc"; s1是一个类类型变量,“abc”是一个对象。
String s2=new String("abc");

s1和s2的区别:
s1在字符串常量池中创建了一个abc字符串
s2在堆中创建了两个对象一个是默认对象一个是字符串对象。

*********************************
==和equals的区别

==比较的是地址,equals比较的是内容。
*********************************
1.获取 
   1.1 获取字符串长度
     int length();
   1.2 根据位置获取字符
     char charAt(int index);
   1.3 根据字符获取在字符中的位置
      int indexof(int ch) 返回的是ch在字符串中第一个出现的位置
      int indexof(int ch,int FromIndex) 从fromIndex指定位置开始,获取ch在字符串中出现的位置
      int indexof(String str); 返回的是str在字符串中第一个出现的位置
      int indexof(String str,int FromIndex) 从fromIndex指定位置开始,获取str在字符串中出现的位置
      反响索引一个字符出现的位置。
      int lastindexof(int ch) 返回的是ch在字符串中第一个出现的位置
      int lastindexof(int ch,int FromIndex) 从fromIndex指定位置开始,获取ch在字符串中出现的位置
      int lastindexof(String str); 返回的是str在字符串中第一个出现的位置
      int lastindexof(String str,int FromIndex) 从fromIndex指定位置开始,获取str在字符串中出现的位置


    1.4获取字符串中的一部分字符串,也叫子串。  
      String subString(int beginindex,intendindex) ;
      String subString(int beginindex) 

2.判断
   2.1两个字符串是否相同
      equals(Object obj)
      equalsIgnoreCase(String str)
   2.2字符串中是否包含某个字符串
      contains(String str)
   2.3两个字符串是否以指定字符串开头或结尾
      boolean Startswith(String);
      boolean endswith(String);
   2.4字符串是否为空
      boolean isEmpty();  




3.字符串转换
   
   3.1将字符串变成字符串数组
       String[] split(String regex);
   3.2将字符串变成字符数组
      char[] toCharArray();
   3.1将字符串变成字节数组
      byte[] getBytes();
   3.4将字符串数组变成字符串
      构造函数 String(char[])
              String(char[],offset,count)将字符数组中的一部分转成字符串。
       静态函数 static String copyValueof(char[])
                static String copyValueof(char[],offset,count)将字符数组中的一部分转成字符串。
   3.5将字符串的字母大小写转换
       String toUppercase();大写
       String toUppercase();小写
   3.6将字符串的内容替换
       String repalce(char oldch,char newch);
       String repalce(String s1,String s2);
   3.7将字符串两端空格去掉
       String trim();
   3.8将字符串进行连接
       String concat(String);

4.比较 
 compareTo();小返回负数 等返回0 大返回正数

二、StringBuffer和StringBuilder 

StringBuffer
是个字符串缓冲区对象,用于存储数据的容器
特点:1.长度是可变的
     2.可以存储不同类型数据
     3.最终要转换成字符串使用
     4.可以对字符串进行修改




功能 
1.添加
  append(data)将指定数据添加到已有数据的结尾处
  insert(位置,字符串) 将数据插入到指定位置
2.删除
  delete(start,end)  删除缓冲区中的数据,包含start不包含end
  deleteCharAt(ine index)删除指定位置元素
  StringBuffer.delete(0,sb.length());
3.查找
  char charAt(index);
  int indexof(string)
  int lastindexof(String);
4.修改 
  StringBuffer  replace(start,end.String)
  void           setCharAt(index,char)
 
  setlength()设置长度
5.反转
  StringBuffer reverse();


6.将缓冲区中的指定数据存储到字符数组中


  void getChars(int srcBegin,int srcEnd,char[] dst,int dstbegin)




StringBuilder  线程不同步的,StringBuffer是同步的。
建议使用StringBuilder效率快。


JAVA 升级 :提高效率 简化书写  提高安全性




三、基本数据类型包装类


基本数据类型     引用数据类型


   byte             Byte
   short            Short
   int              Integer
   long             long
   float            Float
   char             Character
   boolean          Boolean
   double           Double
   


基本数据类型包装类最常见的作用
就是用于基本数据类型和字符串类型之间的转换


基本数据类型转换成字符串 
  基本数据类型+“”
  基本数据类型.toString(基本数据类型值)


字符串转换成基本数据类型
 
  ***.Parse***(***类型字符串);




进制转换


十进制--其他进制
toBinaryString();二进制
toOctolString();八进制
tohexString();十六进制 




其他进制--十进制
parseInt(string,进制);




四、其他类
System:描述系统的一些信息


        preperties();获取系统信息


         Properties prop =new System.getProperties();
         是hashtable 的子类。用map的方法去除该类集合中的元素。该集合中存储的都是字符串,没有泛型定义。


        String calue=(String)prop.get(obj);
        System.out.println(obj+":"+value);


        //如何在系统中自定义一些特有信息?
        System.setProperty("mykey","myvalue");




Runtime类:使用了单例设计模式。
         
         static Runtime getRuntime();


      process p= r.exec("*.exe")//执行文件


        p.destory();杀掉进程。


Date类 
       获取当前时间 
        Date date=new DAte();
       将模式封装到SimpleDateformat对象中 自定义风格
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日  hh:mm:ss");
        调用format方法让模式格式化指定FAte对象
        String time=sdt.format(d);
      
Calendar类


Calendar c=Calendar.getInstance();
c.get(Calendar.Year);//获取年
c.get(Calendar.Month);//获取月
c.get(Calendar.DayofMonth);//获取日
c.get(Calendar.DayofWeek);//获取星期
更改日期
c.add(Calendar.字段,12)


1.获取任意年的二月
c.set(year,4,2)//获取默认一年的五月2日
c.add(Calendar.Day_of_Month,-1)


Math-Random类


cell();返回>参数的最小整数
floor()返回<参数的最大整数
round()返回四舍五入
pow(a,b)a 的b次方
random();大于等于0.0且小于1.0的伪随机double值
0 0