实用类介绍

实用类介绍(一)

枚举类型

枚举指由一组固定的常量组成的类型。类型安全、易于输入、代码清晰

Java API

包装类

包装类:

  • 包装类把基本数据类型转换为对象

    • 每个基本数据类型java.lang包中都有一个相应的包装类

  • 包装类的作用

    • 提供一系列实用的方法

    • 集合不允许存放基本数据类型,存在数字时,要用包装类型

      包装类有:Integer、Boolean、Character、Long、Short、Double、Float、Byte
    • 注意String不是包装类

包装类的构造方法:

所有包装类都可以将与之对应的基本数据类型作为参数,来构造他们的实例(都能放字符串)

  • public Type (type value)

    • Integer i = new Integer(1);
  • 除Character(char )类外,其他包装类可将一个字符串作为参数构造他们的实例

  • public Type (String value):

    • Integer i=new Integer("123");
public static void main(String[] args){
    String str = new String("12");
    Intefer i = new Integer("18");
    Intefer ii = new Integer(1);
    Cnaracter cha = new Cnaracter('女');//只能放单个字符
    Boolean bool = new boolean("122");  //输出false,除了true(不区分大小写),其他都是false
    Long long1 = new Long(222L);
    Long lpng2 = new Long("111");  //Long l = new Long("111L");错误,L标识
    Byte b = 18;
    Byte byte1 = new Byte(b);
}

new包装类(字符串)即字符串------>包装类

new 包装类(基本数据类型)基本数据类型------->包装类

注意事项:

  • Boolean类构造方法参数为String类型时,若该字符串内容为true(不考虑大小写),则该Boolean对象表示true,否则表示false

  • 当Number包装类构造方法参数为String 类型时,字符串不能为null,且该字符串必须可解析为相应的基本数据类型的数据,否则编译不通过,运行时会抛出NumberFormatException异常

包装类的常用方法:

  • XXXValue(): 包装类转换成基本类型:

//示例:
Integer num = new Integer(25);
int intId = num.intValue();
  • toString(基本数据类型): 以字符串形式返回包装对象表示的基本类型数据(基本类型---->字符串):

String sex = Character.toString('男');
String id = Integer.toString(25);
//或者:
String sex = '男'+"";
String id = 25+"";
​
int num = 18;
String str = num +""; 或者 String str = Integer.toString(num);
  • parseXXX(字符串):把字符串转换为相应的基本数据类型数据(Character除外)(字符串--->基本类型)

int num = Integer.parseInt("36");
boolean bool = Boolean.parseBoolean("false");
​
String str = "18";
Double.parseDouble(str);
Long.parseLong(str);
  • valueOf(基本数据类型)

    • 所有包装类都有如下方法(基本类型--->包装类)public static Type valueOf(type value)

      Integer intValue = Integer.valueOf(21);
      //演示:
      int num1 = 18;
      Integer aa = new Integer(num1);或者 Integer integer1 = Integer.valueOf(num1); 
      //直接转:
      int n1 = 18;
      Integer integer = n1;
      int n2 = integer;

    • 除Character类外,其他包装类都有如下方法(字符串>包装类)public static Type valueOf(String s)

      Integer intValue = Integer.ValueOf("21");

装箱和插箱:

基本类型和包装类的自动转换

Integer intObject = 5;
int intValue = intObject;
  • 装箱:基本类型转换为包装类的对象

  • 拆箱:包装类对象转换为基本类型的值

包装类的特点:

  • JDK1.5后,允许基本数据类型和包装类型进行混合数学运算(加减乘除)

  • 包装类并不是用来取代基本数据类型的(在基本数据类型需要用对象表示时使用)

Math类

java.lang.Math类提供了常用的数学运算方法和两个静态常量E(自然对数的底数) 和PI(圆周率)

Math.abs(-3.5);   //abs返回绝对值:3.5

Math.max(2.5,90.5);  //返回90.5
int random = (int)(Math.random()*10);  //生成一个0~9之间的随机数;(Math.random()*10)括号

double d = Math.random();//输出0~1得随机数(小数)

获取指定范围随机数

公式[a,b]:(int)(Math.random() * (b - a + 1) + a)

例:生成[10,99]之间的随机数

首先将原始方法范围整体扩大99 - 10 + 1 = 90倍,获取的区间为[0.0 , 90.0)

再加a = 10获取的区间为[10.0 , 100.0),右侧为开区间,将其类型强制转换为int型后,区间即可看做[10.0 , 99.0]

实用类介绍(二)

Random类

  • 生成随机数的其他方式

    • java.util.Random类

      Random random = new Random();  //创建一个Random对象
      for(int i = 0; i<20; i++){
          int num = random.nextInt(10);  //返回下一个伪随机数,整型的
          System.out.println("第"+(i+1)+"个随机数是:"+num);
      }

用同一个种子值来初始化两个Random 对象,然后用每个对象调用相同的方法,得到的随机数也是相同的

String类

String:

  • 生活中的字符串 "欢迎进入"、"Hello World"、"www.baidu.cn"

  • 使用String对象存储字符串

    String s = "Hello World";
    String s = new String();
    String s = new String("Hello World");
    
  • String类位于java.lang包中,具有丰富的方法

    计算字符串的长度、比较字符串、连接字符串、提取字符串

字符串长度length()

比较字符串equals()等:

  • equals()方法:

    • ==:判断两个字符串在内存中的地址,即判断是否是同一个字符串对象

    • equals():检查组成字符串内容的字符是否完全一致

  • 使用equalsIgnoreCase() 忽略大小写

  • 使用toLowerCase() 小写

  • 使用toUpperCase() 大写

字符串连接:

  • 方法一:使用“+”,每“+”一次,开辟一次空间

  • 方法二:使用String类的concat() 方法,开辟一次空间

    String s = "我爱你";
    String str = s.concat("中国");
    System.out.println(str);	
    //输出:我爱你中国

字符串常用提取方法:

方法名说明
public int indexOf(int ch)搜索第一个出现的字符ch(或字符串value),如果没有找到,返回-1
public int indexOf(String value)
public int lastIndexOf(int ch)搜索最后一个出现的字符ch(或字符串value),如果没有找到,返回-1
public int lastIndexOf(String value)
public String substring(int index)提取从位置索引开始的字符串部分
public String substring(int beginindex, int endindex)提取beginindex和endindex之间的字符串部分区间左闭右开
public String trim()返回一个前后不含任何空格的调用字符串的副本

替换:

replace和replaceAll:

String str = "  我 的 博 客  ";
str = str.trim();  		//只清空前后空格

String rr = str.replaceAll(" ","");     //将空格都删除
String rr = str.replaceAll("\\d","");   //可以用正则表达式表示

String ss = str.replace(" ","");

判断是否是指定字符结尾:

String str = "我爱你中国,我爱你故乡.com";
System.out.println(str.endsWith("com")); //判断endsWith是否以com结尾

//将字符串转换为字节数组:

String str = "我爱你中国,我爱你故乡.com";
System.out.println(Arrays.toString(str.getBytes()));
//输出:[-50, -46, -80, -82, -60, -29, -42, -48, -71, -6, -93, -84, -50, -46, -80, -82, -60, -29, -71, -54, -49, -25, 46, 99, 111, 109]

字符串拆分:

  1. 用split()方法进行分隔,分隔开的子字符串放入数组,然后进行处理

    public static void main(String[] args) {
    		//1. 用split()方法进行分隔,分隔开的子字符串放入数组,然后进行处理
    		String str = "I love 博客";
    		String[] array = new String[10];
    		array = str.split(" ");
    		for(String s : array){
    			System.out.println(s);
    		}
    }
    //个别符号需要转义符
    String str = "我的.博客";
    String strs = str.split("\\.");
    System.out.println(Arrays.toString(strs));

  2. 用StringTokenizer类进行操作

    public static void main(String[] args) {
    		// 2. 用StringTokenizer类进行操作
    		String str = new String("Human branches,their own shade");
    		StringTokenizer tt = new StringTokenizer(str," ,"); //按照空格和逗号截取
    		String[] array = new String[10];  //定义一个字符串数组
    		
    		int i = 0;
    		while(tt.hasMoreTokens()){
    			array[i] = tt.nextToken();  //将分割开的字符串放入数组中
    			i++;
    		}
    		
    		for(int j = 0; j<array.length;j++){
    			System.out.println(array[j]+" ");
    		}
    		
    	}

  3. 用indexOf()方法进行定位,然后用substring()进行截取,然后操作

    public static void main(String[] args) {
    		// 3. 用indexOf()方法进行定位,然后用substring()进行截取,然后操作
    		String str = new String("Human branches , their own shade");
    		String[] array = new String[10];
    		String temp = str;  //定义一个字符串变量,把str赋给它,保持str字符串不变
    		
    		for(int i = 0; i<array.length; i++){
    			int index = temp.indexOf(" ");  //寻找空格的位置
    			System.out.print("前一个单词"+index+"位后剩下------> ");  //找到空格处,几个长度后分割
    			if(index == -1){  //搜索第一个出现的字符ch(或字符串value),如果没有找到,返回-1
    				array[i] = temp;  //找不到空格说明剩下最后一个字符串,不用分割,赋给array后跳出
    				break;
    				
    			}
    			array[i] = temp.substring(0, index);
    			temp = temp.substring(index+1);  //剩下的赋给temp
    			System.out.println("temp="+temp);
    		}
    		
    		System.out.println("\n-----array里面的存储-----");			
    		for(String s : array){
    			System.out.println(s+" ");
    		}
    		
    		System.out.println("-----输出不为空的数组-----");		
    		for(int j = 0; j<array.length; j++){
    			if(("").equals(array[j]) || null== array[j]){
    				break;
    			}
    			System.out.println(array[j]+" ");
    		}			
    	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

朱尔斯Jules

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值