Java常用API(一)

Java常用API

一.Java API概述

Java写好的包 类 方法的使用—API,即应用程序接口.Java提供的一些预定义的函数,

目的:基于API实现程序的快速编程.只需要了解实现的作用,无需关注源代码

二.数值运算Math类

Math类为Java提供的支持数值运算的类,Math类包含执行数字运算的方法,如基本指数,对数,平方根和三角函数.

public final class Math----完美

1.Math类提供的基本方法:

static double abs(double a) 返回值为 double绝对值。

​ static double acos(double a) 返回值的反余弦值; 返回的角度在0.0到pi的范围内。
​ static double atan(double a)

向上取整:static double ceil(double a) 返回大于或等于参数的最小(最接近负无穷大) double值,等于一个数学整数。

向下取整:static double floor(double a) 返回小于或等于参数的最大(最接近正无穷大) double值,等于一个数学整数。

四舍五入:static long round(double a) 返回参数中最接近的 long ,其中 long四舍五入为。

static double log(double a) 返回的自然对数(以 e为底) double值。

static double log10(double a) 返回一个 double的基数10对数值

static int max(int a, int b) 返回两个 int值中的较大值。

static double random() 返回值为 double值为正号,大于等于 0.0 ,小于 1.0 。

package com.Itstar.demo01;
/**
 * 演示Math类的基本使用
 * 
 * */
public class MathExample {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		double a=0.5;
		
		//求绝对值
		System.out.println(Math.abs(-a));
		//求反正弦值
		System.out.println(Math.asin(0.5));
		/*
		 * 向上取整:static double ceil(double a) 返回大于或等于参数的最小(最接近负无穷大) double值,等于一个数学整数。  
  向下取整:static double floor(double a) 返回小于或等于参数的最大(最接近正无穷大) double值,等于一个数学整数。
  四舍五入:static long round(double a) 返回参数中最接近的 long ,其中 long四舍五入为。
		 * 
		 * */
		System.out.println("向上取整"+Math.ceil(a));
		System.out.println("向下取整"+Math.floor(a));
		System.out.println("四舍五入"+Math.round(a));
		
		System.out.println("随机数"+Math.random()*a);	

	}

}
结果:
0.5
0.5235987755982989
向上取整1.0
向下取整0.0
四舍五入1
随机数0.24348410780888774

三.字符串运算string类

package com.Itstar.demo02;
/**
 * String类 常用方法的基本使用
 * 
 * */
public class StringExample {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		String str=new String("abc123  789 ");
		//String str="abc123  789 ";
		/*
		 *  char charAt(int index) 返回 char指定索引处的值 
			boolean contains(CharSequence s) 当且仅当此字符串包含指定的char值序列时才返回true。 
		    boolean equals(Object anObject) 将此字符串与指定对象进行比较。
		    indexOf(String str) 返回指定子字符串第一次出现的字符串内的索引。
		    length() 返回此字符串的长度。---循环的中止条件 	
			boolean matches(String regex) 告诉这个字符串是否匹配给定的 regular expression 。
		    String replace(char oldChar, char newChar) 返回从替换所有出现的导致一个字符串 oldChar在此字符串 newChar 。 	
			String[] split(String regex) 将此字符串分割为给定的 regular expression的匹配。
		    String substring(int beginIndex) 返回一个字符串,该字符串是此字符串的子字符串。
		    String toLowerCase() 将所有在此字符 String使用默认语言环境的规则,以小写。  
			String toUpperCase() 将所有在此字符 String使用默认语言环境的规则大写。
		    String trim() 返回一个字符串,其值为此字符串,并删除任何前导和尾随空格。
		 * */
		System.out.println(str.charAt(0));
		System.out.println("是否含有123:"+str.contains("123"));
		if(str=="abc123  789 "/*str.equals()*/) {
			
			System.out.println("字符串相等");
			
		}else {
			
			System.out.println("字符串不相等");
		}
		System.out.println(str.indexOf("123"));
		System.out.println(str.length());
		for(int i=0;i<str.length();i++) {
			
			System.out.println(str.charAt(i));
			
		}
		//str 调用函数后 需要将返回值 重新赋给 str
		str=str.replace("123", "666");
		System.out.println(str);
		//子串开始于指定beginIndex并延伸到字符索引endIndex - 1 [beginIndex,endIndex)
	    System.out.println(str.substring(1, 3));
	    System.out.println(str.toUpperCase());
	   //用于从字符串的开始和结尾修剪空格
	    System.out.println(str.trim());

	}
}
结果:
a
是否含有123true
字符串不相等
3
12
a
b
c
1
2
3
 
 
7
8
9
 
abc666  789 
bc
ABC666  789 
abc666  789

四.字符串运算-大写字母 小写字母 数字出现次数

getCount(string s) AscII码对比 length 遍历

package com.Itstar.demo02;
/**
 * 统计 大写字母 小写字母 数字 出现的次数
 * 
 * */
public class StringDemo01 {
	
	public static void main(String[] args) {
		
		String s="ASDFQWERT123yuiopzxcvbnm7890%@";
		getCount(s);
		
		
	}
	public static void getCount(String s) {
		//记录大小写字母 数字出现次数
		int upper=0;
		int lower=0;
		int digit=0;
		//for 循环遍历--对字符串中的每一个字符进行比对
		
        for(int i=0;i<s.length();i++) {
			//定义一个char 用来存储我们每个字符串中字符
			char c=s.charAt(i);
			//通过 AscII 来判断  数字 48 大写字母 65  小写字母 97
			if(c>='0'&&c<='9') {
				
				digit++;
				
			}else if(c>='A'&&c<='Z') {
				
				upper++;
				
			}else if(c>='a'&&c<='z') {
				lower++;
				
			}
		}
        
        System.out.println("数字的个数为:"+digit);
        System.out.println("大写字母的个数为:"+upper);
        System.out.println("小写字母的个数为:"+lower);
        System.out.println("字符总长度"+s.length());
		
	}

}
结果:
数字的个数为:7
大写字母的个数为:9
小写字母的个数为:12
字符总长度30

五.字符串运算-查找父字符串中某一个子字符串出现的次数

indexof 循环遍历子字符串出现的字数 就需要 截取 substring 把已找到的部分截取 遍历后面的边界条件。

package com.Itstar.demo02;
/**
 * String类 常用方法的基本使用
 * 
 * */
public class StringExample {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		String str=new String("abc123  789 ");
		//String str="abc123  789 ";
		/*
		 *  char charAt(int index) 返回 char指定索引处的值 
			boolean contains(CharSequence s) 当且仅当此字符串包含指定的char值序列时才返回true。 
		    boolean equals(Object anObject) 将此字符串与指定对象进行比较。
		    indexOf(String str) 返回指定子字符串第一次出现的字符串内的索引。
		    length() 返回此字符串的长度。---循环的中止条件 	
			boolean matches(String regex) 告诉这个字符串是否匹配给定的 regular expression 。
		    String replace(char oldChar, char newChar) 返回从替换所有出现的导致一个字符串 oldChar在此字符串 newChar 。 	
			String[] split(String regex) 将此字符串分割为给定的 regular expression的匹配。
		    String substring(int beginIndex) 返回一个字符串,该字符串是此字符串的子字符串。
		    String toLowerCase() 将所有在此字符 String使用默认语言环境的规则,以小写。  
			String toUpperCase() 将所有在此字符 String使用默认语言环境的规则大写。
		    String trim() 返回一个字符串,其值为此字符串,并删除任何前导和尾随空格。
		 * */
		System.out.println(str.charAt(0));
		System.out.println("是否含有123:"+str.contains("123"));
		if(str=="abc123  789 "/*str.equals()*/) {
			
			System.out.println("字符串相等");
			
		}else {
			
			System.out.println("字符串不相等");
		}
		System.out.println(str.indexOf("123"));
		System.out.println(str.length());
		for(int i=0;i<str.length();i++) {
			
			System.out.println(str.charAt(i));
			
		}
		//str 调用函数后 需要将返回值 重新赋给 str
		str=str.replace("123", "666");
		System.out.println(str);
		//子串开始于指定beginIndex并延伸到字符索引endIndex - 1 [beginIndex,endIndex)
	    System.out.println(str.substring(1, 3));
	    System.out.println(str.toUpperCase());
	   //用于从字符串的开始和结尾修剪空格
	    System.out.println(str.trim());

	}

}
结果:
a
是否含有123true
字符串不相等
3
12
a
b
c
1
2
3
 
 
7
8
9
 
abc666  789 
bc
ABC666  789 
abc666  789
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值