网络安全JavaSE第四天

网络安全JavaSE第三天

5. 常用类

5.1 Math

Math类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。

自带常量

  • static double E :比任何其他值都更接近 e(即自然对数的底数)的 double 值。

  • static double PI : 比任何其他值都更接近 pi(即圆的周长与直径之比)的 double 值。

取整方法

  • static double ceil(double a) :返回最小的(最接近负无穷大)double 值,该值大于等于参数,并等于某个整数。

  • static double floor(double a) :返回最大的(最接近正无穷大)double 值,该值小于等于参数,并等于某个整数。

  • static double rint(double a) :返回最接近参数并等于某一整数的 double 值。

  • static long round(double a) :返回最接近参数的 long

三角函数方法

  • static double sin(double a) :返回角的三角正弦。

  • static double cos(double a) :返回角的三角余弦。

  • static double tan(double a) :返回角的三角正切。

  • static double toDegrees(double angrad) :将用弧度表示的角转换为近似相等的用角度表示的角。

  • static double toRadians(double angdeg) :将用角度表示的角转换为近似相等的用弧度表示的角。

  • static double asin(double a) :返回一个值的反正弦;返回的角度范围在 -pi/2 到 pi/2 之间。

  • static double acos(double a) :返回一个值的反余弦;返回的角度范围在 0.0 到 pi 之间。

  • static double atan(double a) :返回一个值的反正切;返回的角度范围在 -pi/2 到 pi/2 之间。

指数函数方法

  • static double exp(double a) :返回欧拉数 edouble 次幂的值。

  • static double log(double a) :返回 double 值的自然对数(底数是 e)。

  • static double log10(double a) :返回 double 值的底数为 10 的对数。

  • static double pow(double a, double b) :返回第一个参数的第二个参数次幂的值。

  • static double sqrt(double a) :返回正确舍入的 double 值的正平方根。

  • static double cbrt(double a) :返回 double 值的立方根。

其他方法

  • static double abs(double a) :返回 double 值的绝对值。

  • static double hypot(double x, double y) :返回 sqrt(x2 +y2),没有中间溢出或下溢。

  • static double max(double a, double b) :返回两个 double 值中较大的一个。

  • static double min(double a, double b) :返回两个 double 值中较小的一个。

  • static double random() :返回带正号的 double 值,该值大于等于 0.0 且小于 1.0

使用示例1:获取指定数据的最大值和最小值

public class MathDemo01 {
    public static void main(String[] args) {
        int a = 5;
        int b = 10;
        int max = Math.max(a, b);
        System.out.println("max = " + max);
​
        int min = Math.min(a, b);
        System.out.println("min = " + min);
    }
}

使用示例2:产生随机数

public class MathDemo02 {
    public static void main(String[] args) {
        double random = Math.random();
        System.out.println(random);
    }
}

得到的是 0 ~1 之间的小数,不会超过 1。

使用示例3:对小数相关操作。

package com.openlab;
​
/**
 * static double ceil(double a)  :返回最小的(最接近负无穷大)`double` 值,该值大于等于参数,并等于某个整数。
 * static double floor(double a)  :返回最大的(最接近正无穷大)`double` 值,该值小于等于参数,并等于某个整数。
 * static long round(double a)  :返回最接近参数的 `long`。
 */
public class MathDemo03 {
    public static void main(String[] args) {
        double d1 = 3.189;
        double d2 = 3.897;
        double d3 = -3.189;
        double d4 = -3.897;
        // ceil 方法是得到比给定值大的最小的整数
        double c1 = Math.ceil(d1); // 4.0
        double c2 = Math.ceil(d2); // 4.0
        double c3 = Math.ceil(d3); // -3
        double c4 = Math.ceil(d4); // -3
​
        System.out.println(c1 + "\t" + c2 + "\t" + c3 + "\t" + c4);
​
        // floor 得到给值小的最大的整数
        double f1 = Math.floor(d1); // 3
        double f2 = Math.floor(d2); // 3
        double f3 = Math.floor(d3); // -4
        double f4 = Math.floor(d4); // -4
        System.out.println(f1 + "\t" + f2 + "\t" + f3 + "\t" + f4);
​
​
        long r1 = Math.round(d1); // 3
        long r2 = Math.round(d2); // 4
        long r3 = Math.round(d3); // -3
        long r4 = Math.round(d4); // -4
        System.out.println(r1 + "\t" + r2 + "\t" + r3 + "\t" + r4);
    }
}

5.2 Scanner

一个可以使用正则表达式来解析基本类型和字符串的简单文本扫描器。

Scanner 使用分隔符模式将其输入分解为标记,默认情况下该分隔符模式与空白匹配。然后可以使用不同的 next 方法将得到的标记转换为不同类型的值。

package com;
​
import java.util.Scanner;
​
/**
 * @description: TODO
 * @company: 西安欧鹏
 * @author: Jock
 * @date: 2024-03-24
 * @version: 1.0
 */
public class ScannerDemo01 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        // 获取输入的一行数据
        //String line = sc.nextLine();
        //System.out.println("line = " + line);
​
        //int num = sc.nextInt();
        //System.out.println("num = " + num);
​
        while (sc.hasNext()) {
            String next = sc.next();
            System.out.println("next = " + next);
            if (next.equals("abc")) break;
        }
​
    }
}

5.3 Random

这个类是用于产生一个随机数的。

public class RandomDemo01 {
    public static void main(String[] args) {
        Random random = new Random();
​
        int i = random.nextInt(100);
        System.out.println("i = " + i);
​
        double v = random.nextDouble();
        System.out.println("v = " + v);
    }
}

5.4 String

String 类代表字符串。Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现。 字符串是常量;它们的值在创建之后不能更改,因为 String 对象是不可变的,所以可以共享。

获取相关

  • char charAt(int index) :返回指定索引处的 char 值。

  • int indexOf(int ch) :返回指定字符在此字符串中第一次出现处的索引。

  • int indexOf(int ch, int fromIndex) :返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。

  • int indexOf(String str) :返回指定子字符串在此字符串中第一次出现处的索引。

  • int indexOf(String str, int fromIndex) :返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。

  • int lastIndexOf(int ch) :返回指定字符在此字符串中最后一次出现处的索引。

  • int length() :返回此字符串的长度。

  • String[] split(String regex) : 根据给定正则表达式的匹配拆分此字符串。

  • String substring(int beginIndex) :返回一个新的字符串,它是此字符串的一个子字符串。

  • String substring(int beginIndex, int endIndex) :返回一个新字符串,它是此字符串的一个子字符串。

package com.openlab;
​
/**
 * - char charAt(int index)  :返回指定索引处的 `char` 值。
 * - int indexOf(int ch)  :返回指定字符在此字符串中第一次出现处的索引。
 * - int indexOf(int ch, int fromIndex)  :返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。
 * - int indexOf(String str)  :返回指定子字符串在此字符串中第一次出现处的索引。
 * - int indexOf(String str, int fromIndex)  :返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。
 * - int lastIndexOf(int ch)  :返回指定字符在此字符串中最后一次出现处的索引。
 * - int length()  :返回此字符串的长度。
 * - String[] split(String regex)  : 根据给定正则表达式的匹配拆分此字符串。
 * - String substring(int beginIndex)  :返回一个新的字符串,它是此字符串的一个子字符串。
 * - String substring(int beginIndex, int endIndex)  :返回一个新字符串,它是此字符串的一个子字符串。
 */
public class StringDemo01 {
    public static void main(String[] args) {
        String str = "hello world";
​
        // char charAt(int index),它是从 0 开始,也就是第一个字符的下标是0
        char c = str.charAt(4);
        System.out.println("c = " + c);
​
        // int indexOf(int ch):如果能找到就返回对应的下标,否则返回 -1
        int index = str.indexOf("W");
        System.out.println("index = " + index);
​
        //int indexOf(String str, int fromIndex)
        int index1 = str.indexOf("o", 6);
        System.out.println("index1 = " + index1);
​
        // int lastIndexOf(int ch) 从后往前找
        int index2 = str.lastIndexOf('o');
        System.out.println("index2 = " + index2);
​
        // int length():获取字符串长度
        int length = str.length();
        System.out.println("length = " + length);
​
        // String[] split(String regex)
        String[] s = str.split(" ");
        for (int i = 0; i < s.length; i++) {
            System.out.println(s[i]);
        }
​
        // String substring(int beginIndex, int endIndex)截取字符串
        String substring = str.substring(3, 9);
        System.out.println("substring = " + substring);
    }
}

判断相关

  • int compareTo(String anotherString) :按字典顺序比较两个字符串。

  • int compareToIgnoreCase(String str) :按字典顺序比较两个字符串,不考虑大小写。

  • boolean contains(CharSequence s) :当且仅当此字符串包含指定的 char 值序列时,返回 true。

  • boolean endsWith(String suffix) :测试此字符串是否以指定的后缀结束。

  • boolean equals(Object anObject) :将此字符串与指定的对象比较。

  • boolean equalsIgnoreCase(String anotherString) :将此 String 与另一个 String 比较,不考虑大小写。

  • boolean isEmpty() :当且仅当 length()0 时返回 true

  • boolean startsWith(String prefix) :测试此字符串是否以指定的前缀开始。

  • boolean startsWith(String prefix, int toffset) :测试此字符串从指定索引开始的子字符串是否以指定前缀开始。

package com.openlab;
public class StringDemo02 {
    public static void main(String[] args) {
        String s1 = "hello";
        String s2 = "Hello";
        String s3 = "hello";
        String s4 = new String("hello");
​
        System.out.println((int)'h');
        System.out.println((int)'H');
​
        //- int compareTo(String anotherString)  :按字典顺序比较两个字符串。它会返回正数、零、负数
        int i = s1.compareTo(s2);
        System.out.println("i = " + i);
​
        //- int compareToIgnoreCase(String str)  :按字典顺序比较两个字符串,不考虑大小写。
        int i1 = s1.compareToIgnoreCase(s2);
        System.out.println("i1 = " + i1);
​
        //- boolean contains(CharSequence s)  :当且仅当此字符串包含指定的 char 值序列时,返回 true。
        boolean llo = s1.contains("lloo");
        System.out.println("llo = " + llo);
​
        //- boolean endsWith(String suffix)  :测试此字符串是否以指定的后缀结束。
        boolean lo = s1.endsWith("Lo");
        System.out.println("lo = " + lo);
​
        //- boolean equals(Object anObject)  :将此字符串与指定的对象比较。
        boolean equals3 = s1.equals(s2);
        boolean equals = equals3;
        System.out.println("equals = " + equals);
        boolean equals1 = s1.equals(s3);
        System.out.println("equals1 = " + equals1);
        boolean equals2 = s3.equals(s4);
        System.out.println("equals2 = " + equals2);
​
        boolean flag = s1 == s3;
        System.out.println("flag = " + flag);
​
        flag = s1 == s4;
        System.out.println("flag = " + flag);
​
        // == 在 java 中比较对象的引用地址,而 equals 是比较对象的值
​
​
        //- boolean equalsIgnoreCase(String anotherString)  :将此 `String` 与另一个 `String` 比较,不考虑大小写。
        flag = s1.equalsIgnoreCase(s3);
        System.out.println("flag = " + flag);
​
        //- boolean isEmpty()  :当且仅当 [`length()`](../../java/lang/String.html#length()) 为  `0` 时返回 `true`。
        System.out.println("s1.isEmpty() = " + s1.isEmpty());
​
        //- boolean startsWith(String prefix)  :测试此字符串是否以指定的前缀开始。
        boolean h = s1.startsWith("H");
        System.out.println("h = " + h);
​
        //- boolean startsWith(String prefix, int toffset)  :测试此字符串从指定索引开始的子字符串是否以指定前缀开始。
    }
}

修改相关

  • String replace(char oldChar, char newChar) :返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。

  • String replace(CharSequence target, CharSequence replacement) :使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串。

  • String toLowerCase() :使用默认语言环境的规则将此 String 中的所有字符都转换为小写。

  • String toUpperCase() :使用默认语言环境的规则将此 String 中的所有字符都转换为大写。

  • String trim() :返回字符串的副本,忽略前导空白和尾部空白。

package com.openlab;
​
/**
 * - String replace(char oldChar, char newChar)  :返回一个新的字符串,它是通过用 `newChar` 替换此字符串中出现的所有 `oldChar` 得到的。
 * - String replace(CharSequence target, CharSequence replacement)  :使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串。
 * - String toLowerCase() :使用默认语言环境的规则将此 `String` 中的所有字符都转换为小写。
 * - String toUpperCase()  :使用默认语言环境的规则将此 `String` 中的所有字符都转换为大写。
 * - String trim() :返回字符串的副本,忽略前导空白和尾部空白。
 */
public class StringDemo03 {
    public static void main(String[] args) {
        String str = " good AFTERNOON  ";
​
        //- String replace(char oldChar, char newChar)  :返回一个新的字符串,它是通过用 `newChar` 替换此字符串中出现的所有 `oldChar` 得到的。
        //- String replace(CharSequence target, CharSequence replacement)  :使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串。
        String replace = str.replace("good", "Good");
        System.out.println("str = " + str);
        System.out.println("replace = " + replace);
​
        //- String toLowerCase() :使用默认语言环境的规则将此 `String` 中的所有字符都转换为小写。
        String lowerCase = str.toLowerCase();
        System.out.println("lowerCase = " + lowerCase);
​
        //- String toUpperCase()  :使用默认语言环境的规则将此 `String` 中的所有字符都转换为大写。
        String upperCase = str.toUpperCase();
        System.out.println("upperCase = " + upperCase);
​
        //- String trim() :返回字符串的副本,忽略前导空白和尾部空白。
        String trim = str.trim();
        System.out.println("trim = " + trim);
    }
}

6. 函数

6.1 函数概述

6.1.1 什么是函数

函数也叫方法,它是一个解决特定问题的代码块。这个代码块可以被反复使用。

6.1.2 为什么需要函数

为了让我们的程序更利于维护,也让我们程序更好复用。所以我们需要使用函数。

6.1.3 如何定义函数

在 Java 中函数定义需要有一个固定的格式:

访问修饰符 方法返回值类型 方法名称([参数列表]) [抛出异常] {
    方法体;
    [return 返回值;]
}

访问修饰符:
    public 这是公共,所有都可以访问
    private 这是私有的,只能自己访问
    protected 这是受保护的,只能它自己或它的子类访问
    
方法返回值类型:
    1. 基本数据类型
    2. 引用数据类型
    
方法名称:
    方法名称定义需要符合标识符的命名规则。

参数列表:
    1. 它在方法中是可以没的,如果有多个参数,则参数与参数之间使用英文逗号分隔
    2. 参数的声明格式为:参数类型 参数名称
    
抛出异常:
    1. 此部分是可以没有,如果有则需要使用 throws 来向外抛出
    2. 如果有多个异常,则使用逗号分隔
    
返回值:
    1. 如果方法执行后需要有返回值,则必须使用 return 关键字来返回
    2. 方法的返回值类型要与 return 后面的值的类型一致
    3. 如果方法返回返回值类型(如 void),那么方法中也可以没有 return 语句,也可以有。如果有 return 后不能有值。

例如,我们在 MethodDemo01中定义一个 show 方法和一个add方法。

public class MethodDemo01 {
    public static void main(String[] args) {
        show();
    }

    public static int add(int a, int b) {
        int result = a + b;
        return result;
    }
    public static void show() {
        System.out.println("大家下午好!");
        return;
    }
}

方法使用案例:计算最大值:

public class MethodDemo02 {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        //System.out.println(Math.max(a, b));
        int r = max(a, b);
        System.out.println(r);
    }

    public static int max(int a, int b) {
        return Math.max(a, b);
        //return a > b ? a : b;

        //if (a > b) {
        //    return a;
        //} else {
        //    return b;
        //}
    }
}

6.2 函数执行原理

每当调用一个方法时,系统会创建一个活动记录(也称为活动框架),用于保存方法中的参数和变量。活动记录置于一个内存去榆中,称为调用堆栈(call stack)。调用堆栈也称为执行堆栈、运行时堆栈,或者一个机器堆栈,常简称为“堆栈”。当一个方法调用另一个方法时,调用者的活动记录保持不动,一个新的活动记录被创建用于被调用的新方法。一个方法结束返回到调用者时,其相应的活动记录也被释放。

6.3 函数重载

6.3.1 什么叫函数重载

如果在一个类中有多个方法的名称相同,它的参数列表不同。那么这些方法就构成了重载方法。

6.3.2 重载方法的特点

如何判断方法是否是重载方法,可以根据它的特点来判断。重载方法具有以下特点:

  1. 方法名称必须相同

  2. 参数列表必须不同(包括参数类型、参数顺序、参数个数)

  3. 与方法的返回值无关

  4. 必须是在同一个类中

6.3.3 重载方法的使用

需求:实现两个数相加操作的功能。

public class MethodDemo03 {
    public static void main(String[] args) {
        System.out.println(add(2, 3.0f));
    }

    public static int add(int a, int b) {
        return a + b;
    }

    public static double add(double a, int b) {
        return a + b;
    }

    public static float add(int a, float b) {
        return a + b;
    }

    public static float add(float a , float b) {
        return a + b;
    }
}

6.4 函数的递归

所谓递归就是函数的内部调用其本身。在使用递归时,必须要有一个出口来退出递归的操作,否则需要产生栈溢出的错误。

假设我们需要计算 6! 的值,此时可以使用到递归。

1)非递归的实现:

public class MethodDemo04 {
    public static void main(String[] args) {
        int n = 6;
        System.out.println(recursion(n));
    }

    public static int recursion(int x) {
        int sum = 1;
        for (int i = x; i >= 1; i--) {
            sum *= i;
        }
        return sum;
    }
}

2)使用递归的实现

public class MethodDemo05 {
    public static void main(String[] args) {
         System.out.println(recursion(6));
    }
    public static int recursion(int x) {
        if (x == 1) return 1;
        else return x * recursion(x -1);
    }
}

示例学习:递归实现斐波那契数列

斐波那契数列的格式为:1 1 2 3 5 8 13 21 ....,从第三个数开始就是前两个数的和。

我们来计算 20 个数的斐波那契数列的和是多少:

public class MethodDemo6 {
    public static void main(String[] args) {
        int sum = 0;
        for (int i = 1; i <= 20; i++) {
            sum += fibola(i);
        }

        System.out.println("sum = " + sum);
    }
    public static int fibola(int num) {
        if (num == 1 || num == 2) {
            return 1;
        } else {
            return fibola(num-1) + fibola(num-2);
        }
    }
}

示例学习:汉诺塔问题

public class MethodDemo07 {
    public static void main(String[] args) {
        hanno(3, "A", "B", "C");
    }

    public static void hanno(int n, String  a, String b, String c) {
        if (n == 1) {
            System.out.println("a -> c");
        } else {
            hanno(n-1, a, c, b);
            System.out.println("a -> c");
            hanno(n-1, b, a, c);
        }
    }
}

7. 数组

7.1 数组概述

当我们需要存储大量的相同数据时,我们就可以考虑数组这种存储数据的结构来实现。在 Java 中数组具有如下的特点:

  1. 数组的长度固定,当数组定义好的,它的长度是不能发生变化的

  2. 数组中的元素类型是一致

  3. 数组中每个元素都有与之对应的下标,通过下标可以获取到数组中对应的元素

  4. 数组中的元素在内存是连续存放的

7.2 数组的定义

在 Java 中数组定义有以下几种方式:

// 第一种:
数据类型[] 数组名称 = new 数据类型[元素个数/长度];

// 第二种:
数据类型[] 数组名称 = new 数据类型[]{元素1,元素2,....,元素n};

// 第三种:
数据类型[] 数组名称 = {元素1,元素2,....,元素n};

使用示例:

public class ArrayDemo01 {
    public static void main(String[] args) {
        // 第一种方式:数据类型[] 数组名称 = new 数据类型[长度];
        int[] arr = new int[5];
        int tmp[] = new int[6];

        // 第二种方式:数据类型[] 数组名称 = new 数据类型[]{元素1,元素2,...,元素n};
        String[] strs = new String[]{"hello", "world", "java", "python"};

        // 第三种方式:数据类型[] 数组名称 = {元素1,元素2,...,元素n};
        String[] strs1 = {"hello", "world", "java", "python"};
    }
}

7.3 数组元素的获取

当数组定义好后,我们可以通过如下的几种方式来给数组进行赋值或获取元素。

7.3.1 数组赋值

public class ArrayDemo02 {
    public static void main(String[] args) {
        int[] nums = new int[5];
        // 1. 获取数组元素个数,通过数组对象的 length 属性
        System.out.println(nums.length);
        // 2. 获取第一个元素,它需要通过数组的下标,而下标是从 0 开始到数组长度 - 1。
        // 0 ,原因是当创建了数组后,没有给数组的元素赋值,它的默认是会根据数组类型来定。
        // 如果数组类型是 int ,那么它元素默认值为 0
        // 如果数组类型是 String,那么它的元素默认值为 null
        // 如果数组类型是 double ,那么它的元素默认值是 0.0
        // 如果数组类型是 char,那么它的元素默认是是 ''
        System.out.println(nums[0]);  // 0

        // 3. 给数组的元素赋值
        for (int i = 0; i < nums.length; i++) {
            nums[i] = (i + 10) % 7;
        }

        System.out.println("-------------");

        // 4. 显示所有元素
        for (int i = 0; i < nums.length; i++) {
            System.out.println(nums[i]);
        }

    }
}

  • 20
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值