JAVA几个重要常用类(String类、Math类、Arrays类、Random类、包装类)

(一)String类

(1.1)String类的常用属性和方法

1、 length() 数组的长度
2、 charAt(index) 根据下标找字符
3、 indexOf(Str) 根据字符找下标(从左到右找到该字符第一次出现的下 标)
4、 indexOf(String str,int fromIndex) 从指定的位置开始查找该字符第一次出现的位置
5、 lastIndexOf(String str) 根据字符找下标(从右往左查找该字符第一次出现的下标)
6、 lastIndexOf(String str ,fromIndex) 根据字符找下标(从指定的位置开始从右往左查找该字符第一次出现的
下标)
7、 replace(String oldStr , String newStr) 字符串替换
8、 split(String str) 使用指定的字符,分割字符串,得到相应的字符串数组
9、 concat(String str) 字符串拼接
10、 contains(String str) 字符串是否包含指定字符(结果为boolean类型的)
11、 equals(“”) 比较两个字符串是否相等,得到的结果是boolean类型
12、 subString(int beginIndex) 从指定下标开始截取,直到字符串结束
13、 subString(int beginIndex,endIndex)从指定下标开始截取到指定位置
14、 toLowerCase() 将大写转化为小写
15、 toUpperCase() 将小写转化为大写

(1.2)代码实现

public class text1
{
 public static void main(String[] args) 
 {
  String s=" ";
  String s1="How are you";
  System.out.println(s1.charAt(2));//提取下标为2的字符
  System.out.println(s1.length());//字符串长度
     System.out.println(s1.startsWith("How"));//是否以How开头
     System.out.println(s1.endsWith("you"));//是否以you结尾
  s=s1.substring(4);//提取字符串;从下标为4的开始到字符串结尾为止
  System.out.println(s);
  s=s1.substring(4,7);//提取字符串;下标为[4,7)不包括7
   System.out.println(s);
   s=s1.toLowerCase();//转小写
   System.out.println(s);
   s=s1.toUpperCase();//转大写
   System.out.println(s);
   String s2=" How old are you!!! ";
   System.out.println(s1.equals(s2));//比较两个字符串是否相等
   System.out.println(s1.indexOf("you"));//字符串是s是否包含you
   String s3=s1.replace(" ", "&");
   System.out.println("结果是"+s3);
   s=s2.trim();//去除字符串首尾空格。中间空格不能取
   System.out.println(s);
   System.out.println(s2);//String为不可变字符串,s2不变
 }
}

结果展示
在这里插入图片描述

(1.3) 不可变和可变字符序列使用陷阱

String一经初始化后,就不会再改变其内容了。对String字符串的操作实际上是对其副本(原始拷贝)的操作,原来的字符串一点都没有改变。
StringBuilder和StringBuffer类是对原字符串本身操作的,可以对字符串进行修改而不产生副本拷贝或者产生少量的副本。因此可以在循环中使用。

(二)Math类

(2.1)Math类的常用方法

  1. abs(); 绝对值
  2. round() 四舍五入
  3. ceil() 上取整
  4. floor() 下取整
  5. random() 随机数
  6. pow(a,b);a的b次幂
  7. sqrt();开平方

(2.2)代码实现

public class text1
{
 public static void main(String[] args)
 {
  //取整相关操作
  System.out.println(Math.ceil(3.2));//向上取整
  System.out.println(Math.floor(3.2));//向下取整
  System.out.println(Math.round(3.2));//四舍五入
  System.out.println(Math.round(3.8));//四舍五入
  //绝对值、开方、a的b次幂操作
  System.out.println(Math.abs(-45));//绝对值
  System.out.println(Math.sqrt(64));//开平方
  System.out.println(Math.pow(5, 2));//a的b次幂
  System.out.println(Math.pow(2, 5));
  //Math类中常用的常量
  System.out.println(Math.PI);
  System.out.println(Math.E);
  //随机数
  System.out.println(Math.random());//[0,1)
 }
}

结果展示在这里插入图片描述

(三)Arrays类

(3.1)Arrays类的常用方法

1.toString(); 返回指定数组内容的字符串表示形式。
2.sort(); 对指定的类型数组按数字升序排序。
3.binarySearch();使用二分法来搜索指定类型数组,以获得指定的值。
4.fill(); 将指定类型分配给指定类型数组的每个元素。

(3.2)代码实现

import java.util.Arrays;
public class text1
{
 public static void main(String[] args) 
 {
  int arr[]= {1,5,4,8,9,7,3};
  System.out.println("以字符串形式输出数组:"+Arrays.toString(arr));
  Arrays.sort(arr);//给数组排序
  System.out.println("排序后的数组:"+Arrays.toString(arr));
  System.out.println(Arrays.binarySearch(arr, 9));
  Arrays.fill(arr, 0);
  System.out.println("填充后的字符串:"+Arrays.toString(arr));
 }
}

结果展示
在这里插入图片描述

(四)Random类

(4.1)代码实现

import java.util.Random;
public class text1
{
 public static void main(String[] args) 
 {
  Random rand=new Random();
  //随机生成[0,1)之间的double类型的数据
  System.out.println(rand.nextDouble());
  //随机生成int类型允许范围之内的整型数据
  System.out.println(rand.nextInt());
  //随机生成[0,1)之间的float类型的数据
  System.out.println(rand.nextFloat());
  //随机生成false或者true
  System.out.println(rand.nextBoolean());
  //随机生成[0,10)之间的int类型的数据
  System.out.println(rand.nextInt(10));
  //随机生成[20,30)之间的int类型的数据
  System.out.println(20+rand.nextInt(10));
  //随机生成[20,30)之间的int类型的数据  这个方法计算比较复杂
  System.out.println(20+rand.nextDouble()*10); 
 }
}

结果展示
在这里插入图片描述

(五)包装类

(5.1)包装类的好处

将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据,进行十六进制的操作。常用的操作之一:用于基本数据类型与字符串之间的转换。

(5.2)基本数据类型和包装类的对应

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

(5.3)自动装箱和自动拆箱

自动装箱自动将基本数据类型转换为包装器类型

Integer i=100;//自动装箱

自动拆箱自动将包装器类型装换为基本数据类型

Integer i=100;
int j=i;

在装箱的时候自动调用的是Integer的valueOf(int)方法。而在拆箱的时候自动调用的是Integer的intValue方法。

(5.4)代码实现

public class text1
{
 public static void main(String[] args) 
 {
  //把字符串包装成包装类对象
  Integer e=new Integer("9999");
  Integer f=Integer.parseInt("99988");
  //把包装类对象转成字符串
  String str=f.toString();
  //常见的常量
  System.out.println("int类的最大整数:"+Integer.MAX_VALUE);
  System.out.println(Integer.toBinaryString(60));   
  System.out.println(Integer.toOctalString(60));    
  System.out.println(Integer.toHexString(60));
  
 }
}

结果展示
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/aabb12345687/article/details/106749097