day14

作者: 姗婷 | 来源:发表于2020-11-28 23:22 被阅读0次
import java.util.Arrays;

/*
如下字符串:91 27 46 38 50
最终输出结果:50 38 46 27 91
 */
public class RegexTest1 {
    public static void main(String[] args) {
//定义字符串
        String s = "91 27 46 38 50";
//对字符串进行切割,得到一个字符串数组
        String[] strArr = s.split(" ");
//把字符串数组变成一个int数组
        int[] intArr = new int[strArr.length];
        for (int i = 0; i < strArr.length ; i++) {
            intArr[i] = Integer.parseInt(strArr[i]);
        }

        Arrays.sort(intArr);
//把排序后的int数组在组装成一个字符串
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i <intArr.length ; i++) {
            sb.append(intArr[i]).append(" ");
        }
//转为字符串,去最后一个空格
        String s1 = sb.toString().trim();
        System.out.println(s1);
    }
}

1:正则表达式(理解)

(1)就是符合一定规则的字符串
(2)常见规则
A:字符
x 字符 x。举例:'a'表示字符a
\ 反斜线字符。
\n 新行(换行)符 ('\u000A')
\r 回车符 ('\u000D')
B:字符类
[abc] a、b 或 c(简单类)
[^abc] 任何字符,除了 a、b 或 c(否定)
[a-zA-Z] a到 z 或 A到 Z,两头的字母包括在内(范围)
[0-9] 0到9的字符都包括
C:预定义字符类
. 任何字符。我的就是.字符本身,怎么表示呢? .
\d 数字:[0-9]
\w 单词字符:[a-zA-Z_0-9]
在正则表达式里面组成单词的东西必须有这些东西组成
D:边界匹配器
^ 行的开头
$ 行的结尾
\b 单词边界
就是不是单词字符的地方。
举例:hello world?haha;xixi
E:Greedy 数量词
X? X,一次或一次也没有
X* X,零次或多次
X+ X,一次或多次
X{n} X,恰好 n 次
X{n,} X,至少 n 次
X{n,m} X,至少 n 次,但是不超过 m 次

arr[x] = Integer.parseInt(strArray[x]);
StringBuilder sb = new StringBuilder();
        for (int x = 0; x < arr.length; x++) {
            sb.append(arr[x]).append(" ");
        }

(3)常见功能:(分别用的是谁呢?)
A:判断功能
String类的public boolean matches(String regex)
B:分割功能
String类的public String[] split(String regex)
C:替换功能
String类的public String replaceAll(String regex,String replacement)
D:获取功能
Pattern和Matcher
Pattern p = Pattern.compile("a*b");
Matcher m = p.matcher("aaaaab");
find():查找存不存在
group():获取刚才查找过的数据
(4)案例
A:判断电话号码和邮箱
String regex = "\w+@\w{2,6}(\.\w{2,3})+";
B:按照不同的规则分割数据

              //硬盘上的路径,我们应该用\\替代\
        String s4 = "E:\\JavaSE\\day14\\avi";
        String[] str4Array = s4.split("\\\\");

C:把论坛中的数字替换为*

        String regex = "\\d+";
        String ss = "*";
        String result = s.replaceAll(regex, ss);

D:获取字符串中由3个字符组成的单词

String s = "da jia ting wo shuo,jin tian yao xia yu,bu shang wan zi xi,gao xing bu?";
        // 规则,\b边界
        String regex = "\\b\\w{3}\\b";
        // 把规则编译成模式对象
        Pattern p = Pattern.compile(regex);
        // 通过模式对象得到匹配器对象
        Matcher m = p.matcher(s);
              // 注意:一定要先find(),然后才能group()
                while (m.find()) {
            System.out.println(m.group());
        }       

2:Math(掌握)

(1)针对数学运算进行操作的类
(2)常见方法(自己补齐)
A:绝对值
public static int abs(int a)
B:向上取整
public static double ceil(double a)
C:向下取整
public static double floor(double a)
D:两个数据中的大值
public static int max(int a,int b):最大值 (min自学)
E:a的b次幂
public static double pow(double a,double b)
F:随机数
public static double random():随机数 [0.0,1.0)
G:四舍五入
public static int round(float a)
H:正平方根
public static double sqrt(double a)
(3)案例:
A:猜数字小游戏
B:获取任意范围的随机数

3:Random(理解)

(1)用于产生随机数的类
(2)构造方法:
A:Random() 默认种子,每次产生的随机数不同
B:Random(long seed) 指定种子,每次种子相同,随机数就相同
(3)成员方法:
A:int nextInt() 返回int范围内的随机数
B:int nextInt(int n) 返回[0,n)范围内的随机数

4:System(掌握)

(1)系统类,提供了一些有用的字段和方法
(2)成员方法(自己补齐)
A:运行垃圾回收器
public static void gc()
B:退出jvm
public static void exit(int status):终止当前正在运行的 Java 虚拟机。参数用作状态码;根据惯例,非 0 的状态码表示异常终止。
C:获取当前时间的毫秒值
public static long currentTimeMillis()
D:数组复制
public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束。

5:BigInteger(理解)

(1)针对大整数的运算
(2)构造方法
A:BigInteger(String s)
(3)成员方法(自己补齐)

  • public BigInteger add(BigInteger val):加
  • public BigInteger subtract(BigInteger val):减
  • public BigInteger multiply(BigInteger val):乘
  • public BigInteger divide(BigInteger val):除
  • public BigInteger[] divideAndRemainder(BigInteger val):返回商和余数的数组

6:BigDecimal(理解)

(1)浮点数据做运算,会丢失精度。所以,针对浮点数据的操作建议采用BigDecimal。(金融相关的项目)
(2)构造方法
A:BigDecimal(String s)
(3)成员方法:

  • public BigDecimal add(BigDecimal augend)
  • public BigDecimal subtract(BigDecimal subtrahend)
  • public BigDecimal multiply(BigDecimal multiplicand)
  • public BigDecimal divide(BigDecimal divisor)
  • public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode):商,几位小数,如何舍取

7:Date/DateFormat(掌握)

(1)Date是日期类,可以精确到毫秒。
A:构造方法

  • Date():根据当前的默认毫秒值创建日期对象
  • Date(long date):根据给定的毫秒值创建日期对象
    B:成员方法
  • public long getTime():获取时间,以毫秒为单位
  • public void setTime(long time):设置时间
    C:日期和毫秒值的相互转换
    案例:你来到这个世界多少天了?
    (2)DateFormat针对日期进行格式化和针对字符串进行解析的类,但是是抽象类,所以使用其子类SimpleDateFormat
    A:SimpleDateFormat(String pattern) 给定模式
    yyyy-MM-dd HH:mm:ss
    B:日期和字符串的转换
    a:Date -- String
    format()
// 创建日期对象
        Date d = new Date();
        // 创建格式化对象
        // SimpleDateFormat sdf = new SimpleDateFormat();
        // 给定模式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        // public final String format(Date date)
        String s = sdf.format(d);
        System.out.println(s);

b:String -- Date
parse()

    String str = "2008-08-08 12:12:12";
    //在把一个字符串解析为日期的时候,请注意格式必须和给定的字符串格式匹配
    SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date dd = sdf2.parse(str);
    System.out.println(dd);

C:案例:
制作了一个针对日期操作的工具类。

8:Calendar(掌握)

(1)日历类,封装了所有的日历字段值,通过统一的方法根据传入不同的日历字段可以获取值。
(2)如何得到一个日历对象呢?
Calendar rightNow = Calendar.getInstance();
本质返回的是子类对象
(3)成员方法
A:根据日历字段得到对应的值
B:根据日历字段和一个正负数确定是添加还是减去对应日历字段的值
C:设置日历对象的年月日
(4)案例:
计算任意一年的2月份有多少天?

//        键盘录入
        Scanner sc = new Scanner(System.in);
        System.out.println("输入你想要计算的年份");
        int i = sc.nextInt();
//设置日历对象的年月日
        Calendar c = Calendar.getInstance();
//      这一年的3月1日
        c.set(i,2,1);
//        把时间往前推一天,就是2月的最后一天
        c.add(Calendar.DATE,-1);
//         获取这一天输出即可
        System.out.println(c.get(Calendar.DATE));

相关文章

网友评论

      本文标题:day14

      本文链接:https://www.haomeiwen.com/subject/skwoiktx.html