Java API

Java API (应用程序编程接口)


Java中定义了String和StringBuffer两个类来封装字符串,并提供了一系列操作字符串的方法。位于java.lang包中(不需要调用包也可直接使用)。

1、String类和StringBuffer类
1.1 String类

三种构造方法进行String类的初始化:
String() =>创建空参数的字符串
String str1=new String();

String(char [] value) =>指定的字符数组创建对象
String str2=new String("java");

String(String value) =>指定的字符串内容创建对象
//创建内容为字符数组的字符串
char []charArray=new char[]{'A','B','C'};
String str3=new String(charArray);

String特点:
1,字符串都是对象
2,初始化后不可被更改(因为是常量)
3,通过String类的构造函数可以知道,将字节数组或者字符串转成字符串

String 类的基本操作
1.字符串基本的操作
int length() =>返回字符串长度
int indexOf(int ch) =>返回指定字符在此字符串第一次出现的索引位
int lastIndexOf(int ch) =>返回指定字符在此字符串最后一次出现的索引位
int charAt(int index) =>返回指定位置上的字符
substring(int beginIndex,int endIndex)=>返回一个新字符串,它是此字符串的一个子字符串。

StringDemo2 

 1 public class StringDemo2 {
 2     public static void main(String[] args) {
 3         String str="abcdecfcfjsjeijlsfkejjac";
 4         System.out.println("字符串长度length:"+str.length());
 5 //        int length=str.length();//明确返回值int类,.出来的排序都是int型靠前
 6 //        System.out.println("length:"+length);
 7         
 8         //------字母c出现的位置------
 9         int index=str.indexOf('c');//获取c字符第一次出现的位置
10         System.out.println("index="+index);
11         //------第二个字母c出现的位置------
12         int index1=str.indexOf('c',index+1);        
13         System.out.println("index1="+index1);
14         //------第三个字母c出现的位置------
15         int index2=str.indexOf('c',index1+1);        
16         System.out.println("index2="+index2);
17         //------最后一个字母c出现的位置------
18         System.out.println("lastindex="+str.lastIndexOf('c'));
19         
20         //------获取指定位置上的字符------
21         str="suzhouxueyuan";
22         char ch=str.charAt(3);
23 //        char ch=str.charAt(30); //StringIndexOutOfBoundsException:字符串角标越界
24         System.out.println("ch="+ch);
25         
26         //------获取部分字符串------
27         System.out.println("原串:"+str);
28         String s=str.substring(6,9);//包含头,不包含尾
29         System.out.println("被截取s="+s);
30         
31     }
32 }

2.String 方法查找练习:
1,字符串是否以指定的字符串开头,结尾同理
  boolean startsWith(String);
  boolean endsWith(String);
2,字符串中是否包含另一个字符串
  boolean contains(String);
  int indexOf(String);//返回-1,表示不存在
3,字符串中另一个字符串出现的位置
  int indexOf(String);
4,将字符串中指定的字符串替换成另一个字符串
  String replace(oldChar,newChar);//没有替换内容时,返回为原字符串
5,字符串比较大小

//让对象具有比较大小的功能只需要实现Comparable接口即可
  int result="ab".compareTo("ac");
  System.out.println("result:"+result);

扫描二维码关注公众号,回复: 971320 查看本文章

6,将字符串转成一个字符数组,或者字节数组
  char[] chs=str.toCharArray();
  byte[] bytes=str.getBytes();
7,将字母字符串转换成大小写的字母字符串
  toUpperCase();
  toLowerCase();
8,将字符串按照指定的方式分解成多个字符串
  String []=str.split(String);

StringTest

 1 //String 类常用的方法
 2 public class StringTest {
 3     public static void main(String[] args) {
 4         String str="StringDemo.java";
 5         System.out.println(str.startsWith("Demo"));//false
 6         System.out.println(str.contains("Demo"));//true
 7         System.out.println(str.indexOf("Demo"));//角标6的位置开始出现
 8         
 9         //Demo替换成Test
10         String s=str.replace("Demo", "Test");
11         System.out.println("s="+s);
12         
13         char[] chs=str.toCharArray();
14         byte[] bytes=str.getBytes();
15         
16         System.out.println("将str转为大写:"+str.toUpperCase());
17         System.out.println("将str转为小写:"+str.toLowerCase());
18 
19         //分割字符串
20         str="zhangsan,lisi,wangwu";
21         String []names=str.split(",");//正则表达式,遇到,就切分
22         for (int i = 0; i < names.length; i++) {
23             System.out.println(names[i]);
24         }
25         
26         
27         //让对象具有比较大小的功能只需要实现Comparable接口即可
28         int result="ab".compareTo("ac");
29         System.out.println("result:"+result);
30     }
31 }

字符串判断重点:== 和 equals()方法

== 用于比较两个字符串地址
equals 用于比较两个字符串中的字符

1.2 StringBuffer类
1,是一个字符串缓冲区,相当于一个容器;
2,长度可变,任意类型(转成字符串进行添加);
3,容器对象提供很多对容器的操作功能(增删改查);
4,必须所有的数据最终变成一个字符串

StringBuffer 增删改练习

 1 public class StringBufferDemo {
 2     public static void main(String[] args) {
 3         System.out.println("1,添加--------------------------");
 4         add();
 5         System.out.println("2,删除--------------------------");
 6         remove();
 7         System.out.println("3,修改--------------------------");
 8         alter();
 9     }
10 
11     private static void add() {
12         
13         StringBuffer sb=new StringBuffer();
14         sb.append("abcdefg");//末尾追加
15         System.out.println("append 末尾追加:"+sb);
16         sb.insert(1,"boom");//角标指定位置追加
17         System.out.println("insert 指定位置追加:"+sb);
18     }
19 
20     private static void remove() {
21         
22         StringBuffer sb=new StringBuffer("abcdefg");
23         System.out.println("原串:"+sb);
24         sb.delete(1, 3);//1,3=>开区间(不包括第三个角标)
25         System.out.println("指定范围删除:"+sb);
26         sb.deleteCharAt(3);
27         System.out.println("指定位置删除:"+sb);
28         sb.delete(0, sb.length());
29         System.out.println("清空缓冲区......");
30     }
31 
32     private static void alter() {
33         
34         StringBuffer sb =new StringBuffer("abcdefg");
35         System.out.println("原串:"+sb);
36         sb.setCharAt(2, 'a');//setCharAt 单引号(' ')
37         System.out.println("修改指定位置字符:"+sb);
38         sb.replace(2, 6, "java");//replace 双引号("") 开区间
39         System.out.println("指定位置替换字符或者字符串:"+sb);
40         
41         System.out.println("字符翻转:"+sb.reverse());
42         
43     }
44 
45 }

StringBuffer和数组的区别:
数组存储完可以单独操作每一个元素,每一个元素都是独立的;
字符串缓冲区,所有存储的元素都被转成字符串且最后拼成一个大的字符串。

StringBuffer和String不同之处:
1,String类表示的字符串常量,创建后长度内容无法改变。
   StringBuffer表示字符容器,内容长度可随时该变。
2,String类覆盖了Object类equals()方法,StringBuffer并没有

3,String类对象可用连接符 + 进行连接,StringBuffer则编译出错

StringBuilder和StringBuffer区别:
StringBuilder:非同步,单线程访问效率高。
StringBuffer: 同步的,多线程访问安全。
相同点:提供的功能一样.

添加链:不断的添加数据后,要对缓冲区的最后数据进行操作,必须转成字符串toString();

 1         /**
 2          * 添加链方法
 3          * 不断的添加数据后,要对缓冲区的最后数据进行操作,必须转成字符串toString();
 4          */
 5         String str=sb.append("a").append("b").append("c").toString();
 6         System.out.println("添加链方法:"+str);
 7         
 8         String s1="a"+5+"c";
 9         System.out.println("s1="+s1);
10         String s2=new StringBuffer().append("a").append("5").append("c").toString();
11         System.out.println("s2="+s2);


缓冲区的应用:无论多少数据,无论什么类型,最终变成字符串,就可以用StringBuffer容器

 1 public class StringBufferTest {
 2 /**
 3  * 将int 型数组元素转换成字符串 格式:[12,34,45,67]
 4  * @param args
 5  */
 6     public static void main(String[] args) {
 7         int []arr={12,34,45,67};
 8         String str=toString_2(arr);
 9         System.out.println("str="+str);
10 
11     }
12 /**
13  * 缓冲区的应用:无论多少数据,无论什么类型,最终变成字符串,就可以用StringBuffer容器
14  * @param arr
15  * @return
16  */
17     public static String toString_2(int[] arr) {
18         StringBuffer sb=new StringBuffer();
19         sb.append("[");
20         for (int i = 0; i < arr.length; i++) {
21             if (i!=arr.length-1) {
22                 sb.append(arr[i]+",");
23             } else {
24                 sb.append(arr[i]+"]");
25             }
26         }
27         return sb.toString();
28 }
29 
30     public static String toString(int[] arr) {
31         //用字符串连接
32         String str="[";
33         for (int i = 0; i < arr.length; i++) {
34             if (i!=arr.length-1) {
35                 str+=arr[i]+",";
36             }else{
37                 str+=arr[i]+"]";
38             }
39                 
40         }
41         return str;
42     }
43 
44 }

2、Runtime类
Runtime类用于表示虚拟机运行时的状态,它用于封装于JVM虚拟机的进程。

 RuntimeDemo 

 1 import java.io.*;
 2 public class RuntimeDemo {
 3     public static void main(String[] args) throws Exception{
 4         Runtime rt=Runtime.getRuntime();
 5         System.out.println("处理器的个数:"+rt.availableProcessors()+"个");
 6         System.out.println("空闲内存数量:"+rt.freeMemory()/1024/1024+"M");
 7         System.out.println("最大可用内存数量:"+rt.maxMemory()/1024/1024+"M");
 8         
 9         /**
10          * exec() 用于执行dos命令
11          */
12 //        rt.exec("notepad.exe");//抛出IOException异常
13         Process p=rt.exec("notepad.exe");//
14         /**
15          * Thread类静态方法sleep(long millis)使程序修免3秒
16          */
17         Thread.sleep(3000);//程序休眠
18         p.destroy();//3秒后自动关闭进程
19         
20     }
21 }

3、包装类:将基本数据类型的值包装为引用数据类的对象。Java将基本数据类型值封装成对象。可提供更多的操作基本数值的功能。

装箱=>将基本数据类型的值转为引用数据类型
拆箱=>将引用数据类型的对象转为基本数据类型

WrapperDemo 

 1 public class WrapperDemo {
 2     public static void main(String[] args) {
 3 //        System.out.println("int_max="+Integer.MAX_VALUE);
 4         
 5         /**
 6          * toXxxString 将十、八、十六进制转二进制
 7          * parseInt    将其他进制转 十、八、十六进制
 8          */
 9         System.out.println("十进制转二进制:"+Integer.toBinaryString(10));
10         System.out.println(Integer.toHexString(15));//十六进制
11         System.out.println(Integer.toOctalString(10));//八进制
12         
13 //         parseInt("string",x)=>字符串以进制形式转换,解析 string 时使用的基数【字符串符合相应进制转换条件,否则抛出异常】
14          
15         System.out.println(Integer.parseInt("1010",2));
16         
17         //1,字符串转基本数值
18         System.out.println(("123")+3);//连接符,并没有作加法运算
19         System.out.println(Integer.parseInt("123")+3);//字符串转数值
20 //        System.out.println(Integer.parseInt("a123")+3);//NumberFormatException
21         System.out.println(Integer.toString(13545));
22 //        基本数值===>包装对象
23         Integer i=new Integer(111);
24         Integer ii=new Integer("222");
25         Integer iii=Integer.valueOf(333);
26         System.out.println(ii);
27 //        包装对象===>基本数值
28         int num=i.valueOf(2);
29         System.out.println(num);
30         
31         
32     }
33 
34 }

 Java中的三个日期类:Date、Calendar、Dateformat

Java API 提供很多类,学习查看API文档。如:Math类、Random类等

猜你喜欢

转载自www.cnblogs.com/cao-yin/p/9068974.html