当前位置:网站首页>Dark horse notes -- wrapper class, regular expression, arrays class
Dark horse notes -- wrapper class, regular expression, arrays class
2022-06-30 12:47:00 【Xiaofu knocks the code】
Catalog
2.1 Overview of regular expressions 、 First experience
*2.2 The use of regular expressions
2.3 Common cases of regular expressions
2.4 Application of regular expression in method
*2.5 Regular expression crawling information
3.1Arrays Class Overview , Demonstration of common functions
*3.2Arrays Class for Comparator Comparator support
1. Packaging
Packaging : In fact, that is 8 Reference types corresponding to basic data types .

Why provide packaging ?
Java In order to realize that everything is an object , by 8 Two basic types provide corresponding reference types .
Later collections and generics can only support wrapper types , Basic data types are not supported .
Automatic boxing : The data and variables of the basic type can be directly assigned to the variables of the packaging type .
Automatic dismantling : Variables of packing type can be directly assigned to variables of basic data type .
Unique functions of packaging class
The default value of the variable of the wrapper class can be null, Higher fault tolerance .
You can convert basic types of data into string types ( Not very useful ).
You can convert a numeric value of string type into a real data type ( It's really useful ), Specific use Packaging type .valueOf( String variable ) Convert to basic data type .
/**
The goal is : Understand the concept of packaging class , And use .
*/
public class Test {
public static void main(String[] args) {
int a = 10;
Integer a1 = 11;
Integer a2 = a; // Automatic boxing
System.out.println(a);
System.out.println(a1);
Integer it = 100;
int it1 = it; // Automatic dismantling
System.out.println(it1);
double db = 99.5;
Double db2 = db; // It's packed automatically
double db3 = db2; // Automatic dismantling
System.out.println(db3);
// int age = null; // Wrong report !
Integer age1 = null;
Integer age2 = 0;
System.out.println("-----------------");
// 1、 Wrapper classes can convert basic types of data into string form .( No dice )
Integer i3 = 23;
String rs = i3.toString();
System.out.println(rs + 1);
String rs1 = Integer.toString(i3);
System.out.println(rs1 + 1);
// Can directly + String gets the string type
String rs2 = i3 + "";
System.out.println(rs2 + 1);
System.out.println("-----------------");
String number = "23";
// Convert to integer
// int age = Integer.parseInt(number);
int age = Integer.valueOf(number);
System.out.println(age + 1);
String number1 = "99.9";
// Convert to decimal
// double score = Double.parseDouble(number1);
double score = Double.valueOf(number1);
System.out.println(score + 0.1);
}
}
2. Regular expressions
2.1 Overview of regular expressions 、 First experience
demand : If it is required to check one now qq Is the number correct ,6 Bit and 20 Within the seat , Must be all numbers .
public class RegexDemo1 {
public static void main(String[] args) {
// demand : check qq number , All numbers must be 6 - 20 position
System.out.println(checkQQ("251425998"));
System.out.println(checkQQ("2514259a98"));
System.out.println(checkQQ(null));
System.out.println(checkQQ("2344"));
System.out.println("-------------------------");
// The first experience of regular expressions :
System.out.println(checkQQ2("251425998"));
System.out.println(checkQQ2("2514259a98"));
System.out.println(checkQQ2(null));
System.out.println(checkQQ2("2344"));
}
public static boolean checkQQ2(String qq){
return qq != null && qq.matches("\\d{6,20}");
}
public static boolean checkQQ(String qq){
// 1、 Judge qq Whether the length of the number meets the requirements
if(qq == null || qq.length() < 6 || qq.length() > 20 ) {
return false;
}
// 2、 Judge qq Are all numbers in , Not return false
// 251425a87
for (int i = 0; i < qq.length(); i++) {
// Get each character
char ch = qq.charAt(i);
// Determine whether this character is not a number , It's not a number that returns directly false
if(ch < '0' || ch > '9') {
return false;
}
}
return true; // It must be legal !
}
}
*2.2 The use of regular expressions
String objects provide methods to match regular expressions :
public boolean matches(String regex): Determine whether regular expressions match , Match return true, Mismatch returned false.

/**
The goal is : comprehensive 、 Learn more about the rules of regular expressions
*/
public class RegexDemo02 {
public static void main(String[] args) {
//public boolean matches(String regex): Determine whether it matches the regular expression , Match return true
// Can only be a b c
System.out.println("a".matches("[abc]")); // true
System.out.println("z".matches("[abc]")); // false
// Cannot appear a b c
System.out.println("a".matches("[^abc]")); // false
System.out.println("z".matches("[^abc]")); // true
System.out.println("a".matches("\\d")); // false
System.out.println("3".matches("\\d")); // true
System.out.println("333".matches("\\d")); // false
System.out.println("z".matches("\\w")); // true
System.out.println("2".matches("\\w")); // true
System.out.println("21".matches("\\w")); // false
System.out.println(" you ".matches("\\w")); //false
System.out.println(" you ".matches("\\W")); // true
System.out.println("---------------------------------");
// The above regular matching can only check a single character .
// Check the password
// It has to be numbers Letter Underline At least 6 position
System.out.println("2442fsfsf".matches("\\w{6,}"));
System.out.println("244f".matches("\\w{6,}"));
// Verification Code Must be numbers and characters Must be 4 position
System.out.println("23dF".matches("[a-zA-Z0-9]{4}"));
System.out.println("23_F".matches("[a-zA-Z0-9]{4}"));
System.out.println("23dF".matches("[\\w&&[^_]]{4}"));
System.out.println("23_F".matches("[\\w&&[^_]]{4}"));
}
}
2.3 Common cases of regular expressions
demand :
Please write a program to simulate the user to input the mobile phone number , Email number , Verify that the format is correct , And give tips , Until the format is entered correctly .
analysis :
Define methods , Receiving data from users , Use regular expressions to complete the verification , And give tips .
import java.util.Arrays;
import java.util.Scanner;
public class RegexTest3 {
public static void main(String[] args) {
// The goal is : check Phone number mailbox Phone number
// checkPhone();
// checkEmail();
// checkTel();
int[] arr = {10, 4, 5,3, 4,6, 2};
System.out.println(Arrays.binarySearch(arr, 2));
}
public static void checkTel(){
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println(" Please enter your telephone number :");
String tel = sc.next();
// Determine whether the email format is correct 027-3572457 0273572457
if(tel.matches("0\\d{2,6}-?\\d{5,20}")){
System.out.println(" The format is correct , Registration completed !");
break;
}else {
System.out.println(" Wrong format !");
}
}
}
public static void checkEmail(){
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println(" Please enter your registered email address :");
String email = sc.next();
// Determine whether the email format is correct [email protected]
// Determine whether the email format is correct [email protected]
// Determine whether the email format is correct [email protected]
if(email.matches("\\w{1,30}@[a-zA-Z0-9]{2,20}(\\.[a-zA-Z0-9]{2,20}){1,2}")){
System.out.println(" The email format is correct , Registration completed !");
break;
}else {
System.out.println(" Wrong format !");
}
}
}
public static void checkPhone(){
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println(" Please enter your registered mobile phone number :");
String phone = sc.next();
// Determine whether the format of the mobile phone number is correct
if(phone.matches("1[3-9]\\d{9}")){
System.out.println(" The mobile phone number format is correct , Registration completed !");
break;
}else {
System.out.println(" Wrong format !");
}
}
}
}
2.4 Application of regular expression in method
Use of regular expressions in string methods

/**
The goal is : Application of regular expression in method .
public String[] split(String regex):
-- Split the string according to the content matched by the regular expression , Returns an array of strings .
public String replaceAll(String regex,String newStr)
-- Replace according to the matching content of the regular expression
*/
public class RegexDemo04 {
public static void main(String[] args) {
String names = " path dhdfhdf342 Rong'er 43fdffdfbjdfaf Xiao He ";
String[] arrs = names.split("\\w+");
for (int i = 0; i < arrs.length; i++) {
System.out.println(arrs[i]);
}
String names2 = names.replaceAll("\\w+", " ");
System.out.println(names2);
}
}*2.5 Regular expression crawling information
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
expand : Regular expression crawls the contents of the information .( understand )
*/
public class RegexDemo05 {
public static void main(String[] args) {
String rs = " Come to the dark horse program to learn Java, Telephone 020-43422424, Or contact email " +
"[email protected], Telephone 18762832633,0203232323" +
" mailbox [email protected],400-100-3233 ,4001003232";
// demand : Climb out of the above content Phone number and email address .
// 1、 Define crawling rules , String form
String regex = "(\\w{1,30}@[a-zA-Z0-9]{2,20}(\\.[a-zA-Z0-9]{2,20}){1,2})|(1[3-9]\\d{9})" +
"|(0\\d{2,6}-?\\d{5,20})|(400-?\\d{3,9}-?\\d{3,9})";
// 2、 Compile this crawling rule into a matching object .
Pattern pattern = Pattern.compile(regex);
// 3、 Get a content matcher object
Matcher matcher = pattern.matcher(rs);
// 4、 Started looking for
while (matcher.find()) {
String rs1 = matcher.group();
System.out.println(rs1);
}
}
}3.Arrays class
3.1Arrays Class Overview , Demonstration of common functions
Arrays Class Overview : Array operation tool class , Special for manipulating array elements .
Arrays Class API

import java.util.Arrays;
public class ArraysDemo1 {
public static void main(String[] args) {
// The goal is : Learn how to use Arrays Class API , And understand its principle
int[] arr = {10, 2, 55, 23, 24, 100};
System.out.println(arr);
// 1、 Returns the contents of the array toString( Array )
// String rs = Arrays.toString(arr);
// System.out.println(rs);
System.out.println(Arrays.toString(arr));
// 2、 Sort of API( By default, the array elements are automatically sorted in ascending order )
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
// 3、 Binary search technology ( The prerequisite array must be arranged in order to support , Otherwise, there will be bug)
int index = Arrays.binarySearch(arr, 55);
System.out.println(index);
// Returns the rule that no element exists : - ( The location index that should be inserted + 1)
int index2 = Arrays.binarySearch(arr, 22);
System.out.println(index2);
// Be careful : If the array is not well ordered , The existing element may not be found , So that bug!!
int[] arr2 = {12, 36, 34, 25 , 13, 24, 234, 100};
System.out.println(Arrays.binarySearch(arr2 , 36));
}
}
*3.2Arrays Class for Comparator Comparator support
Arrays Sort method of class

Custom collation
Set up Comparator The comparator object corresponding to the interface , To customize the comparison rules .

import java.util.Arrays;
import java.util.Comparator;
public class ArraysDemo2 {
public static void main(String[] args) {
// The goal is : Custom array collation :Comparator Comparator object .
// 1、Arrays Of sort Method is the default ascending sort for arrays with value properties
int[] ages = {34, 12, 42, 23};
Arrays.sort(ages);
System.out.println(Arrays.toString(ages));
// 2、 demand : null !( Custom comparator object , Only sorting of reference types is supported !!)
Integer[] ages1 = {34, 12, 42, 23};
/**
Parameter one : Sorted array Must be an element of reference type
Parameter two : Anonymous inner class object , Represents a comparator object .
*/
Arrays.sort(ages1, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
// Specify comparison rules .
// if(o1 > o2){
// return 1;
// }else if(o1 < o2){
// return -1;
// }
// return 0;
// return o1 - o2; // Default ascending order
return o2 - o1; // Descending
}
});
System.out.println(Arrays.toString(ages1));
System.out.println("-------------------------");
Student[] students = new Student[3];
students[0] = new Student(" Wu lei ",23 , 175.5);
students[1] = new Student(" Xie Xin ",18 , 185.5);
students[2] = new Student(" Wang Liang ",20 , 195.5);
System.out.println(Arrays.toString(students));
// Arrays.sort(students); // Run directly
Arrays.sort(students, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
// Specify your own comparison rules
// return o1.getAge() - o2.getAge(); // Sort by age in ascending order !
// return o2.getAge() - o1.getAge(); // Sort by age !!
// return Double.compare(o1.getHeight(), o2.getHeight()); // Comparing floating-point types can be written like this Ascending
return Double.compare(o2.getHeight(), o1.getHeight()); // Comparing floating-point types can be written like this Descending
}
});
System.out.println(Arrays.toString(students));
}
}
边栏推荐
- Tronapi-波场接口-PHP版本--附接口文档-基于ThinkPHP5封装-源码无加密-可二开-作者详细指导-2022年6月28日11:49:56
- Joplin implements style changes
- Substrate 源码追新导读: 修复BEEFY的gossip引擎内存泄漏问题, 智能合约删除队列优化
- 【驚了】迅雷下載速度竟然比不上虛擬機中的下載速度
- [learn awk in one day] operator
- 市值蒸发650亿后,“口罩大王”稳健医疗,盯上了安全套
- [MySQL] MySQL installation and configuration
- 21. Notes on WPF binding
- MySQL中变量的定义和变量的赋值使用
- SQLSERVER 查询编码是 936 简体中文GBK,那我是写936 还是写GBK?
猜你喜欢

60 个神级 VS Code 插件!!

【OpenGL】OpenGL Examples

rpm2rpm 打包步骤
![[300+ continuous sharing of selected interview questions from large manufacturers] column on interview questions of big data operation and maintenance (II)](/img/cf/44b3983dd5d5f7b92d90d918215908.png)
[300+ continuous sharing of selected interview questions from large manufacturers] column on interview questions of big data operation and maintenance (II)

Android development interview real question advanced version (with answer analysis)

Questionnaire star questionnaire packet capturing analysis
![[target tracking] |pytracking configuring win to compile prroi_ pool. pyd](/img/ac/1e443164e57c4f34ddd1078de9f0d2.png)
[target tracking] |pytracking configuring win to compile prroi_ pool. pyd

【一天学awk】运算符

After the market value evaporated by 65billion yuan, the "mask king" made steady medical treatment and focused on condoms

MySQL built-in functions
随机推荐
Basic interview questions for Software Test Engineers (required for fresh students and test dishes) the most basic interview questions
Commands for redis basic operations
Introduction to the novelty of substrat source code: indexing of call calls and fully completing the materialization of storage layer
黑马笔记---包装类,正则表达式,Arrays类
Redis - problèmes de cache
JMeter's performance test process and performance test focus
【 surprise】 la vitesse de téléchargement de Thunderbolt n'est pas aussi rapide que celle de la machine virtuelle
市值蒸发650亿后,“口罩大王”稳健医疗,盯上了安全套
Apple executives openly "open the connection": Samsung copied the iPhone and only added a large screen
Analysis of the whole process of common tilt data processing in SuperMap idesktop
两批次纯牛奶不合格?麦趣尔回应:正对产品大批量排查抽检
New function of SuperMap iserver11i -- release and use of legend
Visual studio configures QT and implements project packaging through NSIS
数据仓库建设之确定主题域
Videos are stored in a folder every 100 frames, and pictures are transferred to videos after processing
MySQL中变量的定义和变量的赋值使用
JMeter性能测试之相关术语及性能测试通过标准
黑马笔记---集合(Collection的常用方法与遍历方式)
Shell编程概述
Introduction to new features of ES6