System&Math&包装类&文本扫描器&String总结

发布时间 2023-12-28 20:48:43作者: ZWJAS

总结

System类

1、注意设计类的时候,不要将类名设计为和jdk提供的类名一样
掌握属性和方法
属性:
err:获取标准的输出流(打印错误信息)
out:标准的输出流,打印任意信息
in:获取标准的输入流,可以获取控制台输入的信息

方法:(全部是类方法)
1、currentTimeMillis() :获取当前时间,以毫秒来表示,1000毫秒 == 1秒
作用:测试代码的运行时间

 long start = System.currentTimeMillis();
 int i = 0;
do{
System.out.println(i);
 i++;
}while(i < 10000);
long end = System.currentTimeMillis();
System.out.println("程序运行时间:" + (end - start) + "毫秒");

2、exit(0) 终止当前运行的Java虚拟机。

System.exit(0); // 结束JVM

3、arraycopy(Object src, int srcPos, Object dest, int destPos, int length)

//删除一个数组中指定下标的元素
int[] nums = {1, 3, 5, 7, 9};
int[] dest = new int[nums.length - 1];
//删除下标为2的元素
System.arraycopy(nums,0,dest,0,2);
System.arraycopy(nums,3,dest,2,2);
System.out.println(Arrays.toString(dest));
//设计一个方法删除一个数组中指定范围的元素
int[] arrs = SystemDemo.removeElement(nums, 1, 3);
System.out.println(Arrays.toString(arrs));

public static int[] removeElement(int[] nums,int start,int end) {
 // 判断参数
int[] arrs = new int[nums.length - (end - start)];
// 赋值数组
System.arraycopy(nums,0,arrs,0,start);
System.arraycopy(nums,end,arrs,start,nums.length - end);

// 返回结果
return arrs;
    }

Math常用类

Math:封装一些数学常用的方法
属性(常量)
E:表示2.71
PI:3.14
方法
1、random():产生1个范围在0,1之间的随机数,数据类型是double

// 产生1个随机数
  double random = Math.random();
  System.out.println(random);
// 改变随机数的整数部分的范围,整数部分要变多大,就乘以那个数
  double v = Math.random() * 100;// 整数部分的范围是0-99
  System.out.println(v);

//  将这个随机数变为整数:使用数据类型向下转换
  int num = (int)(Math.random() * 5 + 1);
  System.out.println(num);

// 产生5个1-100内的随机数
  for (int i = 0; i < 5; i++) {
      int num = (int)(Math.random() * 100 + 1);
      System.out.println(num);
  }
  int max = Math.max(10, 34);

2、max(),min():获取两个数中最大或最小

int max = Math.max(10, 34);
System.out.println(max);
int min = Math.min(10, 34);
System.out.println(min);

3、ceil():向上取值(天花板)

 double ceil = Math.ceil(3.00000000);
 System.out.println(ceil);

4、floor():向下取值(地板)

double floor = Math.floor(3.9999999999);
System.out.println(floor);

5、round():四舍五入

long round = Math.round(3.413);
System.out.println(round);

BigInteger

使用整数的范围超过long,就是BigInteger
Number是数值类型的父类
Comparable:自然比较器的接口:对对象进行比较大小

1、构造方法
BigInteger(String val) 将BigInteger的十进制字符串表示形式转换为BigInteger。
val必须是纯数字的字符串

2、常用的方法:加 减 乘 除
add(BigInteger a):做加法
subtract(BigInteger val) :做减法
multiply(BigInteger val) :做乘法
divide(BigInteger val):做除法

 // 创建BigInteger对象
 BigInteger bi = new BigInteger("1214321453254356465467576576876876876876876867876876");
 BigInteger one = BigInteger.TEN;
 // 做加法
 BigInteger sum = bi.add(one);
 System.out.println(sum);

 // 做减法
 BigInteger subtract = bi.subtract(one);
 System.out.println(subtract);

 // 做乘法
 BigInteger multiply = bi.multiply(one);
 System.out.println(multiply);

 // 做除法
 BigInteger divide = bi.divide(new BigInteger("2"));
 System.out.println(divide);

BigDecimal

BigDecimal表示小数位可以精确到你想要的位数
只要与钱相关的属性,一般使用
属性:

构造方法:
BigDecimal(String val) 将BigDecimal的字符串表示 BigDecimal转换为 BigDecimal
val是纯数字,带小数点

 BigDecimal bd1 = new BigDecimal("3547546435325235.11423532456436546754765765786574643563253535");
        BigDecimal bd2 = new BigDecimal("354.11423532456436546754765765786574643563253535");
        BigDecimal one = BigDecimal.ONE;
        BigDecimal sum = bd1.add(one);// 做加法
        System.out.println(sum);
        BigDecimal subtract = bd1.subtract(one);// 做减法
        System.out.println(subtract);

        BigDecimal multiply = bd1.multiply(BigDecimal.TEN); // 做乘法
        System.out.println(multiply);

        // 做除法
        BigDecimal divide = bd1.divide(bd2,30,BigDecimal.ROUND_HALF_UP); // ArithmeticException:算术错误,小数位不确定
        // 做除法一定要指定小数位
        // 还是指定四舍五入的模式:
        System.out.println(divide);

包装类

包装类:将基本数据类型,包装为一个类
每一个基本数据类型对应的类就是这个类型包装类
java基本类型:
byte short int long
float double
char
boolean
java中有8个包装类
byte的包装类是:Byte
short的包装类是:Short
int的包装类是:Integer
long的包装类是:Long
float的包装类是:Float
double的包装类是:Double
char的包装类是:Character
boolean的包装类:Boolean

包装类作用:
1)、使用包装,默认都是null
2)、将字符串类型数据转换为对应基本数据类型

装箱和拆箱
装箱:将基本类型变为包装类
拆箱:将包装类装换为基本类型
jdk1.5后支持自动装箱和自动拆箱

Integer i = new Integer(123); // 手动将int类型数字装箱为Integer类型
Integer i2 = new Integer("120"); // 将字符串120装换为Integer
Integer i = 123; // 自动装箱
int num = i.intValue(); // 将包装类手动拆箱
int num2 = i; // 自动拆箱

讲解Integer
1、常量
2、构造方法
Integer(int value)
构造一个新分配的 Integer对象,该对象表示指定的 int值。

Integer i = new Integer(123); // 手动将int类型数字装箱为Integer类型
Integer i = 123; // 自动装箱

Integer(String s)
构造一个新分配 Integer对象,表示 int由指示值 String参数。

Integer i2 = new Integer("120"); // 将字符串120装换为Integer

3、方法:
parseInt(String s) :将字符串转换int

   int num3 = Integer.parseInt("123");

   String[] strs = {"1", "3", "5", "7", "9"};
 // 计算strs数组所有元素的和
   int sum = 0; // 和
   for (String s : strs) {
       // 将字符串转换int
      sum += Integer.parseInt(s);
   }
   System.out.println(sum);

Character
isDigit(char ch) 确定指定的字符是否是数字。
isLetter(char ch) 确定指定的字符是否是一个字母

   char[] chars = {'a','3','D','6','t','7','8','9'};

 // 统计chars数组中数字的个数
   int count = 0;
   for (char c : chars) {
       if (Character.isDigit(c)) { // 判断c字符是否为数字
           count++;
       }
   }
   System.out.println(count);
 Integer integer = new Integer(128);
   Integer integer1 = new Integer(120);
   System.out.println(integer == integer1);
   System.out.println(integer.equals(integer1));

 Integer in1 = 128;
 Integer in2 = 128;

 System.out.println(in1 == integer);

// 享元模式:整型的包装
//jvm在常量池创建对象,每个整型都有256个对象(-128-127)
// 只用你使用自动装箱,并且你的数值在-128-127内,就会使用享元模式

文本扫描器

Scanner:获取在控台输入文本信息

 // 1、创建文本扫描器
        Scanner sc = new Scanner(System.in);
        // 2、提示输入
//        System.out.println("请输入你的年龄:");
        // 3、获取输入的内容,获取的是int类型的数字,nextInt()
        int age = sc.nextInt();
        System.out.println("你的年龄是:" + age);
        System.out.println("请输入你的分数:");
        double score = sc.nextDouble();
        System.out.println("你的分数:" + score);
        System.out.println("请输入你的名字:");
        String name = sc.nextLine();// next():获取单个字符串 nextLine():获取多个字符串
        System.out.println(name);

String:字符串的类

String:底层实现使用final修饰的字节数组来实现的
新建一个String的对象,这个对象里面就有一个final修饰字节数组,这个数组用装字符串的值
String s = "abc";
在常量池创建一个数字组:final byte[] bytes = {a,b,c}
String s2 = "abc";
s == s2地址相等
// 修改s的值
s = "abcd";
String的值是不可修改的

public class StringDemo {
    public static void main(String[] args) {
        String s1 = new String("abc"); // "abc"在常量池,然后把地址给在堆内存的对象,对象又把对象的地址给变量s1
        String s2 = "abc"; // 地址就是常量池的地址
        System.out.println(s1 == s2); // 所以不相等
        s2 = "abcd";
        String s3 = "ab"; // 地址就是常量池的地址
        String s4 = "a" + "b"; // 先拼接常量,"ab",再去常量池找,所以就等于“ab”
        System.out.println(s3 == s4);
        String s4 = s3 + "c"; // 使用变量拼接,会调用stringBuilder方法,会创建数组,就和s1类似,所以和s5的地址不相等
        String s5 = "abc"; // 地址就是常量池的地址
        System.out.println(s4 == s5);
    }
}

String类常用的方法

        // 常用方法:
        // 1.char charAt(int index) 返回 char指定索引处的字符。 不能超过下标范围
        String str = "abc";
        char c = str.charAt(2);
        System.out.println(c);
        // 2.String concat(String str) 将指定的字符串连接到该字符串的末尾。 相当于:+
        str = str.concat("123"); // 将123拼接到str尾部
        System.out.println(str);

        /*
         * 3.boolean contains(CharSequence s) 判断当前对象是否包含连续的参数s。s可以是以下三个子类的对象
         * CharSequence接口,包含下面子类:String、StringBuffer、StringBuilder
         */
        boolean temp = str.contains("bcd");// 在str里面有没有bc
        System.out.println(temp);

        // 4.boolean endsWith(String suffix) 判断当前字符串是否以指定的后缀suffix结尾。(参数必须连续)
        String name = "蘋蘋和小苍老师.mp4";
        boolean b = name.endsWith(".avi");
        System.out.println(b);
        // 5.boolean startsWith(String prefix) 判断当前字符串是否以指定的前缀prefix开头。  (参数必须连续)
        System.out.println(name.startsWith("蘋蘋"));

        /*
         * 8.void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
         * srcBegin:字符串开始下标
         * srcEnd:字符串结束下标(不包含)
         * dstBegin:数组dst的下标
         * 将此字符串中的字符复制到目标字符数组dst中。  (含头不含尾)[srcBegin,srcEnd)
         */
        char[] chars = new char[10];
        String s = "abcdefghijk";
        s.getChars(1,5,chars,0);
        System.out.println(Arrays.toString(chars));

        // 9.int indexOf(String s) 返回s第一次在当前对象str中出现的索引 ,-1表示没有找到(注意:这里s必须也是匹配连续的字符串)
        String s2 = "abc23432abc23423bc";
        int bc = s2.indexOf("bc",5);
        System.out.println(bc);

        // 10.int indexOf(String s, int fromIndex) 返回s在当前对象指定下标fromIndex后第一次出现的索引  (str是作为一个整体匹配下标的)


        // 11.int lastIndexOf(String str) 返回指定子字符串最后一次出现的字符串中的索引。
        // 12.int lastIndexOf(String str, int fromIndex) 返回指定子字符串的最后一次出现的字符串中的索引,从指定索引开始向前搜索。

        // 13.boolean isEmpty() 当 length()为 0时,返回 true 。
        String str2 = null; // 空对象,NullPointerException:
        str2.isEmpty();
        String str2 = "";
        System.out.println(str2.isEmpty());

        // 14.int length() 返回此字符串的长度。
        String str3 = "abcdef";
        System.out.println(str3.length());

        /*
         * 16.String replace(CharSequence target, CharSequence replacement)   将当前字符串中的target替换为replacement,通常用来和谐用语
         * 父接口:CharSequence
         *			该接口的实现类:String、StringBuffer、StringBuilder
         */
        String  str4 = "abc342354dsfsdf436546";
        String str6 = str4.replace('5', '五');
        String str6 = str4.replace("54","五四");
        System.out.println(str6);

        // 19.String[] split(String regex)  将当前字符串对象按照给定的正则表达式regex分割为String[]
        String str =       "172.168.11.65;192.168.11.66;172.168.11.69;169.168.11.169;"
        String[] split = str.split(";");// 通过;将str分割成几个字符串
        for (String s : split) {
            System.out.println(s);
        }
// 请将当前str中所有的ip地址打印出来


        // 20.CharSequence subSequence(int beginIndex, int endIndex)  从当前字符串中beginIndex和endIndex之间截取子串(含头不含尾)
        String str7 = "123456789";
        CharSequence charSequence = str7.subSequence(1, 4);
        System.out.println(charSequence);
        // 21.String substring(int beginIndex)   从当前字符串中截取beginIndex到最后的子串
        // 22.String substring(int beginIndex, int endIndex)  从当前字符串中beginIndex和endIndex之间截取子串(含头不含尾)
        String str8 = "123456789";
        String substring = str8.substring(2, 6);
        System.out.println(substring);
//        str = "172.168.11.65;192.168.11.66;172.168.11.69;169.168.11.169;";
        // 请将字符串中ip是172开头的 最后一段打印出来
        /*
         * 思路:
         * 1. 将字符串str先进行按照;分割
         * 2. 遍历字符串数组,判断字符串以172开头的
         * 3. 在以172的字符串中,再截取最后一个.后面的字符,打印
         */

        // 23.char[] toCharArray() 将此字符串转换为新的字符数组。  方便遍历当前所有的字符
        String str9 = "sdafds32432gretrty";
        char[] chs = str9.toCharArray();
        for (char ch : chs) {
            System.out.println(ch);
        }

        // 24.String toUpperCase() 将此字符转换为全大写	一般用于验证码
        // 25.String toLowerCase() 将此字符转换为全小写	一般用于验证码
        String str10 = "abc";
        System.out.println(str10.toUpperCase());
        System.out.println("ABC".toLowerCase());

        // 26.String trim() 将当前字符串对象中字符前面和后面的空格去除	用了避免用户的一些不当输入
        String str = "		asdlkjfal		jfdaklfj 		dsalkj		";
        System.out.println(str.trim());

        // 27.static String valueOf(Object obj) 将其他类型转换为String对象
        String s = String.valueOf(10);