静态代码块
public class Main {
static {
System. out. println ( "测试类的静态代码执行" ) ;
}
public static void main ( String[ ] args) {
Person p1= new Person ( ) ;
Person p2= new Person ( ) ;
}
}
class Person {
static {
System. out. println ( "Person类中静态代码块执行" ) ;
}
}
成员内部类
public class Main {
public static void main ( String[ ] args) {
outer. inner in = new outer ( ) . new inner ( ) ;
in. show ( ) ;
}
}
class outer {
int num = 4 ;
class inner {
void show ( ) {
System. out. println ( num) ;
}
}
}
内部类
public class Solution {
public static void main ( String[ ] args) {
class Cat implements Animal {
public void shout ( ) {
System. out. println ( "喵喵。。。" ) ;
}
}
animalShout ( new Cat ( ) ) ;
}
private static void animalShout ( Animal an) {
an. shout ( ) ;
}
}
interface Animal {
void shout ( ) ;
}
匿名内部类
public class Solution {
public static void main ( String[ ] args) {
animalShout ( new Animal ( ) {
@Override
public void shout ( ) {
System. out. println ( "喵喵。。。" ) ;
}
} ) ;
}
private static void animalShout ( Animal an) {
an. shout ( ) ;
}
}
interface Animal {
void shout ( ) ;
}
String类的初始化
public class Main {
public static void main ( String[ ] args) throws Exception {
String str1 = new String ( ) ;
String str2 = new String ( "abcd" ) ;
char [ ] charArray = new char [ ] { 'D' , 'E' , 'F' } ;
String str3 = new String ( charArray) ;
System. out. println ( "a" + str1 + "b" ) ;
System. out. println ( str2) ;
System. out. println ( str2) ;
}
}
统计字串的个数
public class Solution {
public static void main ( String[ ] args) {
String str = "nbaernbatnbaynbauinbaopnba" ;
String key = "nba" ;
int count = getKeyStringCount ( str, key) ;
System. out. println ( count) ;
}
private static int getKeyStringCount ( String str, String key) {
int count = 0 ;
if ( str. indexOf ( key) == - 1 )
return count;
int index = 0 ;
while ( ( index = str. indexOf ( key) ) != - 1 ) {
str = str. substring ( index + key. length ( ) ) ;
count++ ;
}
return count;
}
}
定位字符串
public class Main {
public static void main ( String[ ] args) throws Exception {
String s = "ababcdedcba" ;
System. out. println ( s. length ( ) ) ;
System. out. println ( "first index of 'c':" + s. indexOf ( 'c' ) ) ;
System. out. println ( "last index of 'c':" + s. lastIndexOf ( 'c' ) ) ;
System. out. println ( "first index of \"ab\":" + s. indexOf ( "ab" ) ) ;
System. out. println ( "last index of \"ab\":" + s. lastIndexOf ( "ab" ) ) ;
}
}
字符串的替换和去除空格的操作
import java. util. Arrays;
public class Main {
public static void main ( String[ ] args) {
String s = "itcast" ;
System. out. println ( "将it替换为cn.it:" + s. replace ( "it" , "cn.it" ) ) ;
String str = " i t c a s t " ;
System. out. println ( "去除字符串两端的空格:" + str. trim ( ) ) ;
System. out. println ( "去除字符串所有的空格:" + str. replace ( " " , "" ) ) ;
}
}
字符串判断操作
import java. util. Arrays;
public class Main {
public static void main ( String[ ] args) {
String s1 = "string" , s2 = "str" , s3 = "string" ;
System. out. println ( "判断是否以str开头:" + s1. startsWith ( "str" ) ) ;
System. out. println ( "判断是否是以ng结尾:" + s1. endsWith ( "ng" ) ) ;
System. out. println ( "判断是否包含字符串abc:" + s1. contains ( "abc" ) ) ;
System. out. println ( "判断两个字符串是否相等:" + s2. equals ( s1) ) ;
}
}
获取系统全部属性
import java. util. *;
public class Main {
public static void main ( String[ ] args) throws Exception {
Properties properties= System. getProperties ( ) ;
System. out. println ( properties) ;
System. exit ( 0 ) ;
System. exit ( 1 ) ;
Set< String> propertyNames= properties. stringPropertyNames ( ) ;
for ( String key: propertyNames) {
String value = System. getProperty ( key) ;
System. out. println ( key+ "------>" + value) ;
}
}
}
获取时间和数组拷贝
import java. util. *;
public class Main {
public static void main ( String[ ] args) throws Exception {
long startTime= System. currentTimeMillis ( ) ;
int sum= 0 ;
for ( int i= 0 ; i< ( int ) 1e9 ; i++ ) {
sum+= i;
}
long endTime= System. currentTimeMillis ( ) ;
System. out. println ( ( endTime- startTime) + "毫秒" ) ;
int [ ] fromArray= { 10 , 20 , 30 } ;
int [ ] toArray= new int [ 10 ] ;
System. arraycopy ( fromArray, 0 , toArray, 1 , 3 ) ;
System. out. println ( Arrays. toString ( toArray) ) ;
}
}
虚拟机运行时的状态
import java. util. *;
public class Main {
public static void main ( String[ ] args) throws Exception {
Runtime rt = Runtime. getRuntime ( ) ;
System. out. println ( "处理器的个数:" + rt. availableProcessors ( ) ) ;
System. out. println ( "空闲内存大小:" + rt. freeMemory ( ) / 1024 / 1024 + "MB" ) ;
System. out. println ( "最大可用内存数量:" + rt. maxMemory ( ) / 1024 / 1024 + "MB" ) ;
rt. exec ( "notepad.exe" ) ;
Process process = rt. exec ( "notepad.exe" ) ;
Thread. sleep ( 3000 ) ;
process. destroy ( ) ;
}
}
Iterator接口
import java. util. *;
public class Main {
public static void main ( String[ ] args) throws Exception {
ArrayList list = new ArrayList ( ) ;
list. add ( "data_1" ) ;
list. add ( "data_2" ) ;
list. add ( "data_3" ) ;
Iterator it = list. iterator ( ) ;
while ( it. hasNext ( ) ) {
Object obj = it. next ( ) ;
System. out. println ( obj) ;
}
for ( Object obj : list) {
System. out. println ( obj) ;
}
}
}
HashSet集合
import java. util. *;
public class Main {
public static void main ( String[ ] args) throws Exception {
HashSet set = new HashSet ( ) ;
for ( int i = 1 ; i <= 5 ; i++ ) {
set. add ( i) ;
set. add ( i) ;
}
Iterator it = set. iterator ( ) ;
while ( it. hasNext ( ) ) {
Object obj = it. next ( ) ;
System. out. println ( obj) ;
}
}
}
HashMap集合
import java. util. *;
public class Main {
public static void main ( String[ ] args) throws Exception {
Map map = new HashMap ( ) ;
map. put ( "1" , "Jack" ) ;
map. put ( "2" , "Rose" ) ;
map. put ( "3" , "Luck" ) ;
map. put ( "3" , "Tom" ) ;
Set keySet = map. keySet ( ) ;
Iterator it = keySet. iterator ( ) ;
while ( it. hasNext ( ) ) {
Object key = it. next ( ) ;
Object value = map. get ( key) ;
System. out. println ( key + ": " + value) ;
}
}
}