Day15
一,字符串类
1,String的使用
public class Test01 {
public static void main ( String [ ] args) {
String str = "123abc" ;
str = str. concat ( "DEF123" ) ;
str = str. substring ( 3 ) ;
str = str. substring ( 1 , 6 ) ;
str = str. toUpperCase ( ) ;
str = str. toLowerCase ( ) ;
str = " 123 abcDE F12 3 " ;
str = str. trim ( ) ;
str = str. replace ( '2' , 'x' ) ;
str = str. replaceAll ( "1x" , "aaa" ) ;
str = str. replaceAll ( " " , "" ) ;
str = "123abcDEF1234" ;
System . out. println ( str) ;
System . out. println ( "判断字符串是否以某个字符串开头:" + str. startsWith ( "123" ) ) ;
System . out. println ( "判断字符串是否以某个字符串结尾:" + str. endsWith ( "1234" ) ) ;
System . out. println ( "获取子字符串在此字符串中第一次出现的下标:" + str. indexOf ( "12" ) ) ;
System . out. println ( "获取子字符串在此字符串中最后一次出现的下标:" + str. lastIndexOf ( "12" ) ) ;
System . out. println ( "获取指定下标上的字符:" + str. charAt ( 6 ) ) ;
System . out. println ( "----------------------------" ) ;
System . out. println ( String . valueOf ( 100 ) ) ;
System . out. println ( String . valueOf ( 123.123 ) ) ;
System . out. println ( String . valueOf ( true ) ) ;
System . out. println ( String . valueOf ( 'a' ) ) ;
System . out. println ( 100 + "" ) ;
System . out. println ( 123.123 + "" ) ;
System . out. println ( true + "" ) ;
System . out. println ( 'a' + "" ) ;
}
}
2,StringBuffer的使用
public class Test02 {
public static void main ( String [ ] args) {
StringBuffer sb = new StringBuffer ( ) ;
sb. append ( "123abc" ) ;
sb. append ( "DEF123" ) ;
sb. insert ( 6 , "xyz" ) ;
sb. deleteCharAt ( 5 ) ;
sb. delete ( 3 , 11 ) ;
sb. reverse ( ) ;
sb. replace ( 1 , 4 , "用良心做教育" ) ;
System . out. println ( sb) ;
}
}
3,StringBuilder的使用
public class Test03 {
public static void main ( String [ ] args) {
StringBuilder sb = new StringBuilder ( ) ;
sb. append ( "123abc" ) ;
sb. append ( "DEF123" ) ;
sb. insert ( 6 , "xyz" ) ;
sb. deleteCharAt ( 5 ) ;
sb. delete ( 3 , 11 ) ;
sb. reverse ( ) ;
sb. replace ( 1 , 4 , "用良心做教育" ) ;
System . out. println ( sb) ;
}
}
4,String的深入
public class Test04 {
public static void main ( String [ ] args) {
}
}
public class Test05 {
public static void main ( String [ ] args) {
String str1 = "abc" ;
String str2 = "abc" ;
System . out. println ( str1 == str2) ;
String str3 = "ab" + "c" ;
System . out. println ( str1 == str3) ;
final String s1 = "ab" ;
final String s2 = "c" ;
String str4 = s1 + s2;
System . out. println ( str1 == str4) ;
String s3 = "ab" ;
String s4 = "c" ;
String str5 = s3+ s4;
System . out. println ( str1 == str5) ;
}
}
5,StringBuffer和StringBuilder的深入
public class Test06 {
public static void main ( String [ ] args) {
}
}
public class Test07 {
public static void main ( String [ ] args) {
StringBuilder sb1 = new StringBuilder ( ) ;
sb1. append ( "abc" ) ;
System . out. println ( sb1) ;
StringBuffer sb2 = new StringBuffer ( ) ;
sb2. append ( "abc" ) ;
System . out. println ( sb2) ;
System . out. println ( "-----------------------------" ) ;
MyStringBuilder my1 = new MyStringBuilder ( ) ;
my1. append ( "abc" ) ;
System . out. println ( my1) ;
MyStringBuffer my2 = new MyStringBuffer ( ) ;
my2. append ( "abc" ) ;
System . out. println ( my2) ;
}
import java. util. Arrays ;
abstract class MyAbstractStringBuilder {
char [ ] value;
int count;
MyAbstractStringBuilder ( ) {
}
MyAbstractStringBuilder ( int capacity) {
value = new char [ capacity] ;
}
public int length ( ) {
return count;
}
public int capacity ( ) {
return value. length;
}
public MyAbstractStringBuilder append ( String str) {
if ( str == null )
return appendNull ( ) ;
int len = str. length ( ) ;
ensureCapacityInternal ( count + len) ;
str. getChars ( 0 , len, value, count) ;
count += len;
return this ;
}
private MyAbstractStringBuilder appendNull ( ) {
int c = count;
ensureCapacityInternal ( c + 4 ) ;
final char [ ] value = this . value;
value[ c++ ] = 'n' ;
value[ c++ ] = 'u' ;
value[ c++ ] = 'l' ;
value[ c++ ] = 'l' ;
count = c;
return this ;
}
private void ensureCapacityInternal ( int minimumCapacity) {
if ( minimumCapacity - value. length > 0 ) {
value = Arrays . copyOf ( value, newCapacity ( minimumCapacity) ) ;
}
}
private static final int MAX_ARRAY_SIZE = Integer . MAX_VALUE - 8 ;
private int newCapacity ( int minCapacity) {
int newCapacity = ( value. length << 1 ) + 2 ;
if ( newCapacity - minCapacity < 0 ) {
newCapacity = minCapacity;
}
return ( newCapacity <= 0 || MAX_ARRAY_SIZE - newCapacity < 0 )
? hugeCapacity ( minCapacity)
: newCapacity;
}
private int hugeCapacity ( int minCapacity) {
if ( Integer . MAX_VALUE - minCapacity < 0 ) {
throw new OutOfMemoryError ( ) ;
}
return ( minCapacity > MAX_ARRAY_SIZE )
? minCapacity : MAX_ARRAY_SIZE ;
}
@Override
public String toString ( ) {
return String . valueOf ( value) ;
}
}
public class MyStringBuffer extends MyAbstractStringBuilder {
public MyStringBuffer ( ) {
super ( 16 ) ;
}
public MyStringBuffer ( int capacity) {
super ( capacity) ;
}
public MyStringBuffer ( String str) {
super ( str. length ( ) + 16 ) ;
this . append ( str) ;
}
@Override
public synchronized MyStringBuffer append ( String str) {
super . append ( str) ;
return this ;
}
}
public class MyStringBuilder extends MyAbstractStringBuilder {
public MyStringBuilder ( ) {
super ( 16 ) ;
}
public MyStringBuilder ( int capacity) {
super ( capacity) ;
}
public MyStringBuilder ( String str) {
super ( str. length ( ) + 16 ) ;
this . append ( str) ;
}
@Override
public MyStringBuilder append ( String str) {
super . append ( str) ;
return this ;
}
}
二,关于时间日期的类
1,Date类–日期类
import java. util. Date ;
public class Test01 {
public static void main ( String [ ] args) {
Date date = new Date ( 1000 ) ;
System . out. println ( date) ;
}
}
2,SimpleDateForma–格式化日期类
import java. text. ParseException ;
import java. text. SimpleDateFormat ;
import java. util. Date ;
public class Test02 {
public static void main ( String [ ] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat ( "yyyy年MM月dd日 HH:mm:ss" ) ;
String datetime = sdf. format ( new Date ( ) ) ;
System . out. println ( datetime) ;
Date date = sdf. parse ( "2023年12月15日 14:65:57" ) ;
System . out. println ( date) ;
}
}
3,Calendar–日历类
import java. util. Calendar ;
public class Test03 {
public static void main ( String [ ] args) {
Calendar c = Calendar . getInstance ( ) ;
int year = c. get ( Calendar . YEAR ) ;
int month = c. get ( Calendar . MONTH ) + 1 ;
int day = c. get ( Calendar . DAY_OF_MONTH ) ;
int hour = c. get ( Calendar . HOUR ) ;
int minute = c. get ( Calendar . MINUTE ) ;
int second = c. get ( Calendar . SECOND ) ;
System . out. println ( year) ;
System . out. println ( month) ;
System . out. println ( day) ;
System . out. println ( hour) ;
System . out. println ( minute) ;
System . out. println ( second) ;
}
}
三,math类–数学类
public class Test01 {
public static void main ( String [ ] args) {
System . out. println ( "求次方:" + Math . pow ( 3 , 2 ) ) ;
System . out. println ( "求平方根:" + Math . sqrt ( 9 ) ) ;
System . out. println ( "求绝对值:" + Math . abs ( - 100 ) ) ;
System . out. println ( "向上取整(天花板):" + Math . ceil ( 1.1 ) ) ;
System . out. println ( "向下取整(地板):" + Math . floor ( 1.9 ) ) ;
System . out. println ( "四舍五入:" + Math . round ( 1.5 ) ) ;
System . out. println ( "最大值:" + Math . max ( 10 , 20 ) ) ;
System . out. println ( "最小值:" + Math . min ( 10 , 20 ) ) ;
System . out. println ( "随机数(0包含~1排他):" + Math . random ( ) ) ;
int num = ( int ) ( Math . random ( ) * 100 ) + 1 ;
System . out. println ( num) ;
}
}
public class Test02 {
public static void main ( String [ ] args) {
System . out. println ( Math . abs ( - 100 ) ) ;
System . out. println ( Integer . MAX_VALUE ) ;
System . out. println ( Integer . MIN_VALUE ) ;
System . out. println ( Math . abs ( Integer . MIN_VALUE ) ) ;
System . out. println ( Math . abs ( Integer . MAX_VALUE + 1 ) ) ;
}
}
四,Random–随机类
import java. util. Random ;
public class Test01 {
public static void main ( String [ ] args) {
Random ran = new Random ( ) ;
System . out. println ( "随机出int类型取值范围里的数据:" + ran. nextInt ( ) ) ;
System . out. println ( "随机出float类型取值范围里的数据:" + ran. nextFloat ( ) ) ;
System . out. println ( "随机出double类型取值范围里的数据:" + ran. nextDouble ( ) ) ;
System . out. println ( "随机出boolean类型取值范围里的数据:" + ran. nextBoolean ( ) ) ;
System . out. println ( "随机出0~9的int数据:" + ran. nextInt ( 10 ) ) ;
}
}
1,点名器
import java. util. Random ;
public class Test02 {
public static void main ( String [ ] args) {
String [ ] names = { "张偲" , "徐灿" , "彭鹏" , "杨彩虹" , "刘婷婷" , "周隽乐" , "严荐翔" } ;
Random random = new Random ( ) ;
int index = random. nextInt ( names. length) ;
System . out. println ( names[ index] ) ;
}
}
2.验证码
Random ra= new Random ( ) ;
String [ ] name= new String [ ] { "a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" , "i" , "j" , "k" , "l" , "m" , "n" , "o" , "p" , "q" , "r" , "s" , "t" , "u" , "v" , "w" , "x" , "y" , "z" , "0" , "1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" } ;
StringBuffer sb= new StringBuffer ( ) ;
for ( int i = 0 ; i < 6 ; i++ ) {
int t= ra. nextInt ( name. length) ;
sb. append ( name[ t] ) ;
}
System . out. println ( sb) ;
3,扩展–种子数
import java. util. Random ;
public class Test03 {
public static void main ( String [ ] args) {
Random random = new Random ( ) ;
System . out. println ( random. nextInt ( ) ) ;
System . out. println ( random. nextInt ( 10 ) ) ;
System . out. println ( "---------------------" ) ;
MyRandom myRandom = new MyRandom ( ) ;
System . out. println ( myRandom. nextInt ( ) ) ;
System . out. println ( myRandom. nextInt ( 10 ) ) ;
}
}
public class MyRandom {
private long seed;
public MyRandom ( ) {
this ( seedUniquifier ( ) ^ System . nanoTime ( ) ) ;
}
public MyRandom ( long seed) {
this . seed = seed;
}
private static long seedUniquifier ( ) {
for ( ; ; ) {
long current = System . currentTimeMillis ( ) ;
long next = current * 181783497276652981L ;
if ( next% 3 == 0 || next% 17 == 0 || next% 4 == 0 )
return next;
}
}
public int nextInt ( ) {
return ( int ) seed;
}
public int nextInt ( int i) {
return Math . abs ( ( int ) seed) % i;
}
}
五,正则表达式
public class Test01 {
public static void main ( String [ ] args) {
String str = "小红13990022101 小绿15196677229" ;
String regex = "(1\\d{2})(\\d{4})(\\d{4})" ;
str = str. replaceAll ( regex, "$1****$3" ) ;
System . out. println ( str) ;
}
}
public class Test02 {
public static void main ( String [ ] args) {
String emial = "[email protected] " ;
String regex = "\\d{5,10}@qq.com" ;
boolean matches = emial. matches ( regex) ;
System . out. println ( matches) ;
}
}
public class Test03 {
public static void main ( String [ ] args) {
String str = "C:\\资源\\日韩\\波多野结衣.avi" ;
String regex = ":?\\\\" ;
String [ ] split = str. split ( regex) ;
for ( String string : split) {
System . out. println ( string) ;
}
}
}
import java. util. regex. Matcher ;
import java. util. regex. Pattern ;
public class Test04 {
public static void main ( String [ ] args) {
String str = "
"
;
String regex
= " ]*\\bsrc\\b\\s*=\\s*('|\")?([^'\"\n\r\f>]+(\\.jpg|\\.bmp|\\.eps|\\.gif|\\.mif|\\.miff|\\.png|\\.tif|\\.tiff|\\.svg|\\.wmf|\\.jpe|\\.jpeg|\\.dib|\\.ico|\\.tga|\\.cut|\\.pic)\\b)[^>]*>" ;
Pattern pattern
= Pattern . compile ( regex
) ;
Matcher matcher
= pattern
. matcher ( str
) ;
while ( matcher
. find ( ) ) {
String group
= matcher
. group ( 2 ) ;
System . out
. println ( group
) ;
}
}
}
六,Runtime–运行时环境类
public class Test01 {
public static void main ( String [ ] args) {
Runtime runtime = Runtime . getRuntime ( ) ;
System . out. println ( "获取系统的处理数:" + runtime. availableProcessors ( ) ) ;
System . out. println ( "获取最大内存数(字节):" + runtime. maxMemory ( ) ) ;
System . out. println ( "获取闲置内存数(字节):" + runtime. freeMemory ( ) ) ;
}
}
七,System–系统类
import java. io. InputStream ;
import java. io. PrintStream ;
import java. util. Scanner ;
public class Test01 {
public static void main ( String [ ] args) {
InputStream in = System . in;
Scanner scan = new Scanner ( in) ;
String next = scan. next ( ) ;
PrintStream err = System . err;
err. println ( next) ;
scan. close ( ) ;
}
}
public class Test02 {
public static void main ( String [ ] args) {
System . out. println ( "小明" ) ;
System . err. println ( "小红" ) ;
System . out. println ( "小强" ) ;
}
}
import java. util. Properties ;
public class Test03 {
public static void main ( String [ ] args) {
long currentTimeMillis = System . currentTimeMillis ( ) ;
System . out. println ( "获取毫秒:" + currentTimeMillis) ;
Properties properties = System . getProperties ( ) ;
System . out. println ( properties) ;
String value = System . getProperty ( "os.name" ) ;
System . out. println ( value) ;
System . exit ( 0 ) ;
}
}
八,扩展
public class Test01 {
public static void main ( String [ ] args) {
Runtime run = Runtime . getRuntime ( ) ;
long starTime = System . currentTimeMillis ( ) ;
long startMemory = run. freeMemory ( ) ;
StringBuffer sb = new StringBuffer ( 460000 ) ;
sb. append ( "杨彩虹" ) ;
for ( int i = 1 ; i <= 50000 ; i++ ) {
sb. append ( "小可爱,皇冠给你带" ) ;
}
long endMemory = run. freeMemory ( ) ;
long endTime = System . currentTimeMillis ( ) ;
System . out. println ( "消耗时长:" + ( endTime- starTime) ) ;
System . out. println ( "消耗内存:" + ( startMemory- endMemory) ) ;
}
}
import static java. lang. Math . * ;
public class Test02 {
public static void main ( String [ ] args) {
System . out. println ( max ( 10 , 20 ) ) ;
System . out. println ( min ( 10 , 20 ) ) ;
System . out. println ( abs ( - 100 ) ) ;
}
public static int abs ( int i) {
return 123456 ;
}
}