包装类
package com.api.integer;
//包装类
public class Test {
public static void main(String[] args) {
int a = 10;
Integer b = 11;
System.out.println(a);
System.out.println(b);
System.out.println("====================");
Integer c = a;
Integer d = 100;
int e = d;
System.out.println(c);
System.out.println(e);
double f = 99.5;
Double g = f;
double h = g;
System.out.println(h);
System.out.println("===========================================");
//int a1 = null; null is for reference type
Integer a2 = null;
Integer a3 = 0;
System.out.println(a2);
System.out.println("==========================================");
Integer b1 = 23;
String s = b1.toString();
System.out.println(s + 1);
String s1 = Integer.toString(b1);
System.out.println(s1 + 1);
//可以直接+字符串得到字符串类型
String s2 = b1 + "";
System.out.println(s2 + 1);
System.out.println("====================================================");
String m = "23";
int n = Integer.parseInt(m);
System.out.println(n+1);
int n2 = Integer.valueOf(m);
System.out.println(n2+1);
String m1 = "99.9";
double n1 = Double.parseDouble(m1);
System.out.println(n1 + 0.1);
double n3 = Double.valueOf(m1);
System.out.println(n3+0.1);
}
}
正则表达式
package com.api.regex;
//a sharp contrast
public class RegexDemo01 {
public static void main(String[] args) {
System.out.println(checkQQ("766845135"));
System.out.println(checkQQ("123456"));
System.out.println(checkQQ("a1231233"));
System.out.println(checkQQ(null));
System.out.println(checkQQ("12345"));
System.out.println(check2("766845135"));
System.out.println(check2("123456"));
System.out.println(check2("a1231233"));
System.out.println(check2(null));
System.out.println(check2("12345"));
}
public static boolean check2(String qq) {
return qq != null && qq.matches("\\d{6,20}");
}
public static boolean checkQQ(String qq){
if (qq == null || qq.length() < 6 || qq.length() > 20 ){
return false;
}
for (int i = 0; i < qq.length(); i++) {
char a = qq.charAt(i);
if (a < '0' || a > '9'){
return false;
}
}
return true;
}
}
![]()
package com.api.regex;
//regex rule
public class RegexDemo02 {
public static void main(String[] args) {
//java.util.regex.Pattern
System.out.println("z".matches("[abc]"));
System.out.println("a".matches("[abc]"));
System.out.println("z".matches("[^abc]"));
System.out.println("a".matches("[^abc]"));
System.out.println("aa".matches("[abc]"));
System.out.println("aa".matches("[abc]+"));
System.out.println("=============================================");
System.out.println("2".matches("\\d"));
System.out.println("a".matches("\\d"));
System.out.println("222".matches("\\d"));
System.out.println("2".matches("\\D"));
System.out.println("a".matches("\\D"));
System.out.println("===============================================");
System.out.println("a".matches("\\w"));
System.out.println("2".matches("\\w"));
System.out.println("_".matches("\\w"));
System.out.println("aa".matches("\\w"));
System.out.println("你".matches("\\w"));
System.out.println("你".matches("\\W"));
System.out.println("==============================================");
System.out.println("2442fsfs".matches("\\w{6,}"));
System.out.println("2412f".matches("\\w{6,}"));
System.out.println("===============================================");
System.out.println("35as".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,}"));
}
}
package com.api.regex;
//案例:校验输入的信息
import java.util.Scanner;
public class RegexTest3 {
public static void main(String[] args) {
//checkPhone();
checkMoney();
}
public static void checkMoney() {
Scanner scan = new Scanner(System.in);
while (true) {
System.out.println("请您输入您的金额:");
String phone = scan.next();
//[](只匹配一个字符) 和 ()的区别 && 取交集
if (phone != null && phone.matches("\\d+(\\.\\d+)?")){
System.out.println("输入正确");
break;
}else {
System.out.println("输入错误");
}
}
scan.close();
}
public static void checkTel() {
Scanner scan = new Scanner(System.in);
while (true) {
System.out.println("请您输入您的注册tel号码:");
String phone = scan.next();
//027-3572457 0273572457
if (phone != null && phone.matches("0\\d{2,6}-?\\d{5,20}")){
System.out.println("输入正确");
break;
}else {
System.out.println("输入错误");
}
}
scan.close();
}
public static void checkPhone() {
Scanner scan = new Scanner(System.in);
while (true) {
System.out.println("请您输入您的注册手机号码:");
String phone = scan.next();
if (phone != null && phone.matches("1[3-9]\\d{9}")){
System.out.println("输入正确");
break;
}else {
System.out.println("输入错误");
}
}
scan.close();
}
public static void checkEmail() {
Scanner scan = new Scanner(System.in);
while (true) {
//3268847878@qq.com
//3268847dsda878@163.com
//3268847dsda878@pci.com.cn
System.out.println("请您输入您的注册Email:");
String phone = scan.next();
if (phone != null && phone.matches("\\w{1,30}@[a-zA-Z1-9]{2,20}" +
"(\\.[a-zA-Z1-9]{2,20}){1,2}")){
System.out.println("输入正确");
break;
}else {
System.out.println("输入错误");
}
}
scan.close();
}
}
package com.api.regex;
//正则表达式在字符串方法中的使用
import java.util.Arrays;
public class RegexDemo04 {
public static void main(String[] args) {
String names = "小明asdasd3132131小张asdddd1115小林";
String[] s1 = names.split("\\w+");
for (int i = 0; i < s1.length; i++) {
System.out.println(s1[i]);
}
System.out.println(Arrays.toString(s1));
String s2 = names.replaceAll("\\w+", " ");
System.out.println(s2);
}
}
package com.api.regex;
//正则表达式爬取信息
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexDemo05 {
public static void main(String[] args) {
String s = "来黑马程序学习Java,电话020-43422424,或者联系邮箱" +
"itcast@itcast.cn,电话18762832633,0203232323 " +
"邮箱bozai@itcast.cn, 400-100-3233, 4001003232";
String regex = "(0\\d{2,6}-?\\d{5,20})|(1[3-9]\\d{9})" +
"|(\\w{1,30}@[a-zA-Z1-9]{2,20}(\\.[a-zA-Z1-9]{2,20}){1,2})" +
"|(400-?\\d{3,9}-?\\d{3,9})";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(s);
while (matcher.find()){
System.out.println(matcher.group());
}
}
}
lambda
package com.api.lambda;
//lambda
public class LambdaDemo01 {
public static void main(String[] args) {
/*Swimming s = new Swimming(){
@Override
public void swim(){
System.out.println("the teacher swims well");
}
};*/
/*Swimming s = () -> {
System.out.println("the teacher swims well")
;
};*/
Swimming s = () -> System.out.println("the teacher swims well");
go(s);
System.out.println("===========================================");
/*go(() -> {
System.out.println("the student swims happily");
});*/
go(() -> System.out.println("the student swims happily"));
}
public static void go(Swimming swimming) {
System.out.println("begin!");
swimming.swim();
System.out.println("end!");
}
}
interface Swimming{
void swim();
}
package com.api.lambda;
//lambda rules
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.Comparator;
public class LambdaDemo02 {
public static void main(String[] args) {
Integer[] ages = {34,12,42,23};
/*Arrays.sort(ages,new Comparator<Integer>(){
@Override
public int compare(Integer o1,Integer o2){
return o2-o1;
}
});*/
/*Arrays.sort(ages,(Integer o1,Integer o2) -> {
return o2-o1;
}
);*/
/*Arrays.sort(ages,(o1, o2) -> {
return o2-o1;
}
);*/
Arrays.sort(ages, (o1, o2) -> o2-o1 );
System.out.println(Arrays.toString(ages));
JFrame win = new JFrame ( "登录界面");
JButton btn = new JButton("我是一个很大的按钮");
/*btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("有人点我,点我,点我!! ");
}
});*/
/*btn.addActionListener((ActionEvent e) -> {
System.out.println("有人点我,点我,点我!! ");
}
);*/
/*btn.addActionListener(e -> {
System.out.println("有人点我,点我,点我!! ");
}
);*/
btn.addActionListener(e -> System.out.println("有人点我,点我,点我!! "));
win.add(btn);
win.setSize(400,300);
win.setVisible(true) ;
}
}
Arrays补充
package com.api.arrays;
//Arrays 对于 Comparator 比较器的支持
import java.util.Arrays;
import java.util.Comparator;
public class ArraysDemo01 {
public static void main(String[] args) {
int[] ages = {34,12,42,23};
Arrays.sort(ages);
System.out.println(Arrays.toString(ages));
Integer[] ages1 = {34,12,42,23};
Arrays.sort(ages1, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
/*if (o1>o2){
return 1;
}
if (o1<o2){
return -1;
}
return 0;*/
return o2-o1;
}
});
System.out.println(Arrays.toString(ages1));
System.out.println("================================");
Student[] s = new Student[3];
s[0] = new Student("WangLiang",23,175.5);
s[1] = new Student("ZhangShuai",18,185.4);
s[2] = new Student("LiChao",20,180.2);
System.out.println(Arrays.toString(s));
Arrays.sort(s, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
//return o2.getAge() - o1.getAge();
//return Double.compare(o1.getHeight(), o2.getHeight());
return Double.compare(o2.getHeight(), o1.getHeight());
}
});
System.out.println(Arrays.toString(s));
}
}
package com.api.arrays;
//Arrays 对于 Comparator 比较器的支持
public class Student {
private String name;
private int age;
private double height;
public String getName() {
return name;
}
public int getAge() {
return age;
}
public double getHeight() {
return height;
}
public Student() {
}
public Student(String name, int age, double height) {
this.name = name;
this.age = age;
this.height = height;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", height=" + height +
'}';
}
}
package com.api.arrays;
//binarySearch
public class ArraysDemo02 {
public static void main(String[] args) {
int[] arr = {10,14,16,25,28,30,35,88,100};
System.out.println(binarySearch(arr,1));
}
public static int binarySearch(int[] arr,int key){
int low = 0;
int high = arr.length -1;
while(low <= high){
int mid = (low + high) >> 1 ;
if (key < arr[mid]) {
high = mid - 1;
} else if (key > arr[mid]) {
low = mid +1;
}else {
return mid;
}
}
return -(low+1);
}
}