'JAVA基礎String類、static關鍵字、Arrays類、Math類'

Java 數學 IT技術資源共享 2019-09-08
"

今日內容

String類

static關鍵字

Arrays類

Math類

教學目標

能夠使用String類的構造方法創建字符串對象

能夠明確String類的構造方法創建對象,和直接賦值創建字符串對象的區別

能夠使用文檔查詢String類的判斷方法

能夠使用文檔查詢String類的獲取方法

能夠使用文檔查詢String類的轉換方法

能夠理解static關鍵字

能夠寫出靜態代碼塊的格式

能夠使用Arrays類操作數組

能夠使用Math類進行數學運算

第一章 String類

1.1String類概述

概述

java.lang.String 類代表字符串。Java程序中所有的字符串文字(例如 “abc” )都可以被看作是實現此類的實 例。

類 String 中包括用於檢查各個字符串的方法,比如用於比較字符串,搜索字符串,提取子字符串以及創建具有翻 譯為大寫或小寫的所有字符的字符串的副本。

特點

1.字符串不變:字符串的值在創建後不能被更改。

String s1 = "abc"; s1 += "d";
System.out.println(s1); // "abcd"
// 內存中有"abc","abcd"兩個對象,s1從指向"abc",改變指向,指向了"abcd"。

2.因為String對象是不可變的,所以它們可以被共享。


String s1 = "abc"; String s2 = "abc";
// 內存中只有一個"abc"對象被創建,同時被s1和s2共享。
  1. “abc” 等效於 char[] data={ ‘a’ , ‘b’ , ‘c’ } 。
例如:
String str = "abc";
相當於:
char data[] = {'a', 'b', 'c'}; String str = new String(data);
// String底層是靠字符數組實現的。

1.2使用步驟

查看類

java.lang.String :此類不需要導入。 查看構造方法

public String() :初始化新創建的 String對象,以使其表示空字符序列。

public String(char[] value) :通過當前參數中的字符數組來構造新的String。

public String(byte[] bytes) :通過使用平臺的默認字符集解碼當前參數中的字節數組來構造新的 String。

構造舉例,代碼如下:

// 無參構造
String str = new String();
// 通過字符數組構造
char chars[] = {'a', 'b', 'c'}; String str2 = new String(chars);
// 通過字節數組構造
byte bytes[] = { 97, 98, 99 }; String str3 = new String(bytes);

1.3常用方法

判斷功能的方法

public boolean equals (Object anObject) :將此字符串與指定對象進行比較。

public boolean equalsIgnoreCase (String anotherString) :將此字符串與指定對象進行比較,忽略大小 寫。

方法演示,代碼如下:

public class String_Demo01 {
public static void main(String[] args) {
// 創建字符串對象
String s1 = "hello";
String s2 = "hello";
String s3 = "HELLO";
// boolean equals(Object obj):比較字符串的內容是否相同
System.out.println(s1.equals(s2)); // true
System.out.println(s1.equals(s3)); // false System.out.println("‐‐‐‐‐‐‐‐‐‐‐");
//boolean equalsIgnoreCase(String str):比較字符串的內容是否相同,忽略大小寫
System.out.println(s1.equalsIgnoreCase(s2)); // true
System.out.println(s1.equalsIgnoreCase(s3)); // true
System.out.println("‐‐‐‐‐‐‐‐‐‐‐");
}
}

Object 是” 對象”的意思,也是一種引用類型。作為參數類型,表示任意對象都可以傳遞到方法中。

獲取功能的方法

public int length () :返回此字符串的長度。

public String concat (String str) :將指定的字符串連接到該字符串的末尾。

public char charAt (int index) :返回指定索引處的 char值。

public int indexOf (String str) :返回指定子字符串第一次出現在該字符串內的索引。

public String substring (int beginIndex) :返回一個子字符串,從beginIndex開始截取字符串到字符 串結尾。

public String substring (int beginIndex, int endIndex) :返回一個子字符串,從beginIndex到 endIndex截取字符串。含beginIndex,不含endIndex。

方法演示,代碼如下:

public class String_Demo02 {
public static void main(String[] args) {
//創建字符串對象
String s = "helloworld";
// int length():獲取字符串的長度,其實也就是字符個數System.out.println(s.length());
System.out.println("‐‐‐‐‐‐‐‐");
// String concat (String str):將將指定的字符串連接到該字符串的末尾. String s = "helloworld";
String s2 = s.concat("**hello itheima");
System.out.println(s2);// helloworld**hello itheima
// char charAt(int index):獲取指定索引處的字符System.out.println(s.charAt(0));
System.out.println(s.charAt(1));
System.out.println("‐‐‐‐‐‐‐‐");
// int indexOf(String str):獲取str在字符串對象中第一次出現的索引,沒有返回‐1
System.out.println(s.indexOf("l"));
System.out.println(s.indexOf("owo"));
System.out.println(s.indexOf("ak"));
System.out.println("‐‐‐‐‐‐‐‐");
// String substring(int start):從start開始截取字符串到字符串結尾System.out.println(s.substring(0));
System.out.println(s.substring(5));
System.out.println("‐‐‐‐‐‐‐‐");
// String substring(int start,int end):從start到end截取字符串。含start,不含end。
System.out.println(s.substring(0, s.length()));
System.out.println(s.substring(3,8));
}
}

轉換功能的方法

public char[] toCharArray () :將此字符串轉換為新的字符數組。

public byte[] getBytes () :使用平臺的默認字符集將該 String編碼轉換為新的字節數組。

public String replace (CharSequence target, CharSequence replacement) :將與target匹配的字符串使 用replacement字符串替換。

方法演示,代碼如下:

public class String_Demo03 {
public static void main(String[] args) {
//創建字符串對象String s = "abcde";
// char[] toCharArray():把字符串轉換為字符數組
char[] chs = s.toCharArray();
for(int x = 0; x < chs.length; x++) {
System.out.println(chs[x]);
}
System.out.println("‐‐‐‐‐‐‐‐‐‐‐");
// byte[] getBytes ():把字符串轉換為字節數組byte[] bytes = s.getBytes();
for(int x = 0; x < bytes.length; x++) {
System.out.println(bytes[x]);
}
System.out.println("‐‐‐‐‐‐‐‐‐‐‐");
// 替換字母it為大寫IT
String str = "itcast itheima";
String replace = str.replace("it", "IT");
System.out.println(replace); // ITcast ITheima System.out.println("‐‐‐‐‐‐‐‐‐‐‐");
}
}

CharSequence 是一個接口,也是一種引用類型。作為參數類型,可以把String對象傳遞到方法中。

分割功能的方法

public String[] split(String regex) :將此字符串按照給定的regex(規則)拆分為字符串數組。

方法演示,代碼如下:

public class String_Demo03 {
public static void main(String[] args) {
//創建字符串對象
String s = "aa|bb|cc";
String[] strArray = s.split("|"); // ["aa","bb","cc"] for(int x = 0; x < strArray.length; x++) {
System.out.println(strArray[x]); // aa bb cc
}
}
}

1.4String類的練習

拼接字符串

定義一個方法,把數組{1,2,3}按照指定個格式拼接成一個字符串。格式參照如下:[word1#word2#word3]。

public class StringTest1 {
public static void main(String[] args) {
//定義一個int類型的數組int[] arr = {1, 2, 3};
//調用方法
String s = arrayToString(arr);
//輸出結果System.out.println("s:" + s);
}
/*
*寫方法實現把數組中的元素按照指定的格式拼接成一個字符串
*兩個明確:
*返回值類型:String
*參數列表:int[] arr
*/
public static String arrayToString(int[] arr) {
// 創建字符串s
String s = new String("[");
// 遍歷數組,並拼接字符串
for (int x = 0; x < arr.length; x++) { if (x == arr.length ‐ 1) {
s = s.concat(arr[x] + "]");
} else {
s = s.concat(arr[x] + "#");
}
}
return s;
}
}

統計字符個數

鍵盤錄入一個字符,統計字符串中大小寫字母及數字字符個數


public class StringTest2 {
public static void main(String[] args) {
//鍵盤錄入一個字符串數據
Scanner sc = new Scanner(System.in); System.out.println("請輸入一個字符串數據:"); String s = sc.nextLine();
//定義三個統計變量,初始化值都是0 int bigCount = 0;
int smallCount = 0; int numberCount = 0;
//遍歷字符串,得到每一個字符for(int x=0; x<s.length(); x++) {
char ch = s.charAt(x);
//拿字符進行判斷if(ch>='A'&&ch<='Z') {
bigCount++;
}else if(ch>='a'&&ch<='z') { smallCount++;
}else if(ch>='0'&&ch<='9') { numberCount++;
}else {
System.out.println("該字符"+ch+"非法");
}
}
//輸出結果
System.out.println("大寫字符:"+bigCount+"個");
System.out.println("小寫字符:"+smallCount+"個");
System.out.println("數字字符:"+numberCount+"個");
}
}

第二章 static關鍵字

2.1概述

關於 static 關鍵字的使用,它可以用來修飾的成員變量和成員方法,被修飾的成員是屬於類的,而不是單單是屬 於某個對象的。也就是說,既然屬於類,就可以不靠創建對象來調用了。

2.2定義和使用格式

類變量

當 static 修飾成員變量時,該變量稱為類變量。該類的每個對象都共享同一個類變量的值。任何對象都可以更改 該類變量的值,但也可以在不創建該類的對象的情況下對類變量進行操作。

類變量:使用 static關鍵字修飾的成員變量。

定義格式:

static 數據類型 變量名;

舉例:

static int numberID;

比如說,基礎班新班開班,學員報到。現在想為每一位新來報到的同學編學號(sid),從第一名同學開始,sid為1,以此類推。學號必須是唯一的,連續的,並且與班級的人數相符,這樣以便知道,要分配給下一名新同學的學 號是多少。這樣我們就需要一個變量,與單獨的每一個學生對象無關,而是與整個班級同學數量有關。

所以,我們可以這樣定義一個靜態變量numberOfStudent,代碼如下:

public class Student { private String name; private int age;
// 學生的id
private int sid;
// 類變量,記錄學生數量,分配學號
public static int numberOfStudent = 0;
public Student(String name, int age){ this.name = name;
this.age = age;
// 通過 numberOfStudent 給學生分配學號
this.sid = ++numberOfStudent;
}
// 打印屬性值
public void show() {
System.out.println("Student : name=" + name + ", age=" + age + ", sid=" + sid );
}
}
public class StuDemo {
public static void main(String[] args) {
Student s1 = new Student("張三", 23);
Student s2 = new Student("李四", 24);
Student s3 = new Student("王五", 25);
Student s4 = new Student("趙六", 26);
s1.show(); // Student : name=張三, age=23, sid=1
s2.show(); // Student : name=李四, age=24, sid=2
s3.show(); // Student : name=王五, age=25, sid=3
s4.show(); // Student : name=趙六, age=26, sid=4
}
}

靜態方法

當 static 修飾成員方法時,該方法稱為類方法 。靜態方法在聲明中有 static ,建議使用類名來調用,而不需要 創建類的對象。調用方式非常簡單。

類方法:使用 static關鍵字修飾的成員方法,習慣稱為靜態方法。定義格式:

修飾符 static 返回值類型 方法名 (參數列表){
// 執行語句
}

舉例:在Student類中定義靜態方法


public static void showNum() {
System.out.println("num:" + numberOfStudent);
}

靜態方法調用的注意事項:

靜態方法可以直接訪問類變量和靜態方法。

靜態方法不能直接訪問普通成員變量或成員方法。反之,成員方法可以直接訪問類變量或靜態方法。

靜態方法中,不能使用this關鍵字。

小貼士:靜態方法只能訪問靜態成員。

調用格式

被static修飾的成員可以並且建議通過類名直接訪問。雖然也可以通過對象名訪問靜態成員,原因即多個對象均屬於一個類,共享使用同一個靜態成員,但是不建議,會出現警告信息。

格式:

// 訪問類變量類名.類變量名;

// 調用靜態方法

類名.靜態方法名(參數);

調用演示,代碼如下:

public class StuDemo2 {
public static void main(String[] args) {
// 訪問類變量
System.out.println(Student.numberOfStudent);
// 調用靜態方法
Student.showNum();
}
}

2.3靜態原理圖解

static 修飾的內容:

是隨著類的加載而加載的,且只加載一次。

存儲於一塊固定的內存區域(靜態區),所以,可以直接被類名調用。

它優先於對象存在,所以,可以被所有對象共享。

"

今日內容

String類

static關鍵字

Arrays類

Math類

教學目標

能夠使用String類的構造方法創建字符串對象

能夠明確String類的構造方法創建對象,和直接賦值創建字符串對象的區別

能夠使用文檔查詢String類的判斷方法

能夠使用文檔查詢String類的獲取方法

能夠使用文檔查詢String類的轉換方法

能夠理解static關鍵字

能夠寫出靜態代碼塊的格式

能夠使用Arrays類操作數組

能夠使用Math類進行數學運算

第一章 String類

1.1String類概述

概述

java.lang.String 類代表字符串。Java程序中所有的字符串文字(例如 “abc” )都可以被看作是實現此類的實 例。

類 String 中包括用於檢查各個字符串的方法,比如用於比較字符串,搜索字符串,提取子字符串以及創建具有翻 譯為大寫或小寫的所有字符的字符串的副本。

特點

1.字符串不變:字符串的值在創建後不能被更改。

String s1 = "abc"; s1 += "d";
System.out.println(s1); // "abcd"
// 內存中有"abc","abcd"兩個對象,s1從指向"abc",改變指向,指向了"abcd"。

2.因為String對象是不可變的,所以它們可以被共享。


String s1 = "abc"; String s2 = "abc";
// 內存中只有一個"abc"對象被創建,同時被s1和s2共享。
  1. “abc” 等效於 char[] data={ ‘a’ , ‘b’ , ‘c’ } 。
例如:
String str = "abc";
相當於:
char data[] = {'a', 'b', 'c'}; String str = new String(data);
// String底層是靠字符數組實現的。

1.2使用步驟

查看類

java.lang.String :此類不需要導入。 查看構造方法

public String() :初始化新創建的 String對象,以使其表示空字符序列。

public String(char[] value) :通過當前參數中的字符數組來構造新的String。

public String(byte[] bytes) :通過使用平臺的默認字符集解碼當前參數中的字節數組來構造新的 String。

構造舉例,代碼如下:

// 無參構造
String str = new String();
// 通過字符數組構造
char chars[] = {'a', 'b', 'c'}; String str2 = new String(chars);
// 通過字節數組構造
byte bytes[] = { 97, 98, 99 }; String str3 = new String(bytes);

1.3常用方法

判斷功能的方法

public boolean equals (Object anObject) :將此字符串與指定對象進行比較。

public boolean equalsIgnoreCase (String anotherString) :將此字符串與指定對象進行比較,忽略大小 寫。

方法演示,代碼如下:

public class String_Demo01 {
public static void main(String[] args) {
// 創建字符串對象
String s1 = "hello";
String s2 = "hello";
String s3 = "HELLO";
// boolean equals(Object obj):比較字符串的內容是否相同
System.out.println(s1.equals(s2)); // true
System.out.println(s1.equals(s3)); // false System.out.println("‐‐‐‐‐‐‐‐‐‐‐");
//boolean equalsIgnoreCase(String str):比較字符串的內容是否相同,忽略大小寫
System.out.println(s1.equalsIgnoreCase(s2)); // true
System.out.println(s1.equalsIgnoreCase(s3)); // true
System.out.println("‐‐‐‐‐‐‐‐‐‐‐");
}
}

Object 是” 對象”的意思,也是一種引用類型。作為參數類型,表示任意對象都可以傳遞到方法中。

獲取功能的方法

public int length () :返回此字符串的長度。

public String concat (String str) :將指定的字符串連接到該字符串的末尾。

public char charAt (int index) :返回指定索引處的 char值。

public int indexOf (String str) :返回指定子字符串第一次出現在該字符串內的索引。

public String substring (int beginIndex) :返回一個子字符串,從beginIndex開始截取字符串到字符 串結尾。

public String substring (int beginIndex, int endIndex) :返回一個子字符串,從beginIndex到 endIndex截取字符串。含beginIndex,不含endIndex。

方法演示,代碼如下:

public class String_Demo02 {
public static void main(String[] args) {
//創建字符串對象
String s = "helloworld";
// int length():獲取字符串的長度,其實也就是字符個數System.out.println(s.length());
System.out.println("‐‐‐‐‐‐‐‐");
// String concat (String str):將將指定的字符串連接到該字符串的末尾. String s = "helloworld";
String s2 = s.concat("**hello itheima");
System.out.println(s2);// helloworld**hello itheima
// char charAt(int index):獲取指定索引處的字符System.out.println(s.charAt(0));
System.out.println(s.charAt(1));
System.out.println("‐‐‐‐‐‐‐‐");
// int indexOf(String str):獲取str在字符串對象中第一次出現的索引,沒有返回‐1
System.out.println(s.indexOf("l"));
System.out.println(s.indexOf("owo"));
System.out.println(s.indexOf("ak"));
System.out.println("‐‐‐‐‐‐‐‐");
// String substring(int start):從start開始截取字符串到字符串結尾System.out.println(s.substring(0));
System.out.println(s.substring(5));
System.out.println("‐‐‐‐‐‐‐‐");
// String substring(int start,int end):從start到end截取字符串。含start,不含end。
System.out.println(s.substring(0, s.length()));
System.out.println(s.substring(3,8));
}
}

轉換功能的方法

public char[] toCharArray () :將此字符串轉換為新的字符數組。

public byte[] getBytes () :使用平臺的默認字符集將該 String編碼轉換為新的字節數組。

public String replace (CharSequence target, CharSequence replacement) :將與target匹配的字符串使 用replacement字符串替換。

方法演示,代碼如下:

public class String_Demo03 {
public static void main(String[] args) {
//創建字符串對象String s = "abcde";
// char[] toCharArray():把字符串轉換為字符數組
char[] chs = s.toCharArray();
for(int x = 0; x < chs.length; x++) {
System.out.println(chs[x]);
}
System.out.println("‐‐‐‐‐‐‐‐‐‐‐");
// byte[] getBytes ():把字符串轉換為字節數組byte[] bytes = s.getBytes();
for(int x = 0; x < bytes.length; x++) {
System.out.println(bytes[x]);
}
System.out.println("‐‐‐‐‐‐‐‐‐‐‐");
// 替換字母it為大寫IT
String str = "itcast itheima";
String replace = str.replace("it", "IT");
System.out.println(replace); // ITcast ITheima System.out.println("‐‐‐‐‐‐‐‐‐‐‐");
}
}

CharSequence 是一個接口,也是一種引用類型。作為參數類型,可以把String對象傳遞到方法中。

分割功能的方法

public String[] split(String regex) :將此字符串按照給定的regex(規則)拆分為字符串數組。

方法演示,代碼如下:

public class String_Demo03 {
public static void main(String[] args) {
//創建字符串對象
String s = "aa|bb|cc";
String[] strArray = s.split("|"); // ["aa","bb","cc"] for(int x = 0; x < strArray.length; x++) {
System.out.println(strArray[x]); // aa bb cc
}
}
}

1.4String類的練習

拼接字符串

定義一個方法,把數組{1,2,3}按照指定個格式拼接成一個字符串。格式參照如下:[word1#word2#word3]。

public class StringTest1 {
public static void main(String[] args) {
//定義一個int類型的數組int[] arr = {1, 2, 3};
//調用方法
String s = arrayToString(arr);
//輸出結果System.out.println("s:" + s);
}
/*
*寫方法實現把數組中的元素按照指定的格式拼接成一個字符串
*兩個明確:
*返回值類型:String
*參數列表:int[] arr
*/
public static String arrayToString(int[] arr) {
// 創建字符串s
String s = new String("[");
// 遍歷數組,並拼接字符串
for (int x = 0; x < arr.length; x++) { if (x == arr.length ‐ 1) {
s = s.concat(arr[x] + "]");
} else {
s = s.concat(arr[x] + "#");
}
}
return s;
}
}

統計字符個數

鍵盤錄入一個字符,統計字符串中大小寫字母及數字字符個數


public class StringTest2 {
public static void main(String[] args) {
//鍵盤錄入一個字符串數據
Scanner sc = new Scanner(System.in); System.out.println("請輸入一個字符串數據:"); String s = sc.nextLine();
//定義三個統計變量,初始化值都是0 int bigCount = 0;
int smallCount = 0; int numberCount = 0;
//遍歷字符串,得到每一個字符for(int x=0; x<s.length(); x++) {
char ch = s.charAt(x);
//拿字符進行判斷if(ch>='A'&&ch<='Z') {
bigCount++;
}else if(ch>='a'&&ch<='z') { smallCount++;
}else if(ch>='0'&&ch<='9') { numberCount++;
}else {
System.out.println("該字符"+ch+"非法");
}
}
//輸出結果
System.out.println("大寫字符:"+bigCount+"個");
System.out.println("小寫字符:"+smallCount+"個");
System.out.println("數字字符:"+numberCount+"個");
}
}

第二章 static關鍵字

2.1概述

關於 static 關鍵字的使用,它可以用來修飾的成員變量和成員方法,被修飾的成員是屬於類的,而不是單單是屬 於某個對象的。也就是說,既然屬於類,就可以不靠創建對象來調用了。

2.2定義和使用格式

類變量

當 static 修飾成員變量時,該變量稱為類變量。該類的每個對象都共享同一個類變量的值。任何對象都可以更改 該類變量的值,但也可以在不創建該類的對象的情況下對類變量進行操作。

類變量:使用 static關鍵字修飾的成員變量。

定義格式:

static 數據類型 變量名;

舉例:

static int numberID;

比如說,基礎班新班開班,學員報到。現在想為每一位新來報到的同學編學號(sid),從第一名同學開始,sid為1,以此類推。學號必須是唯一的,連續的,並且與班級的人數相符,這樣以便知道,要分配給下一名新同學的學 號是多少。這樣我們就需要一個變量,與單獨的每一個學生對象無關,而是與整個班級同學數量有關。

所以,我們可以這樣定義一個靜態變量numberOfStudent,代碼如下:

public class Student { private String name; private int age;
// 學生的id
private int sid;
// 類變量,記錄學生數量,分配學號
public static int numberOfStudent = 0;
public Student(String name, int age){ this.name = name;
this.age = age;
// 通過 numberOfStudent 給學生分配學號
this.sid = ++numberOfStudent;
}
// 打印屬性值
public void show() {
System.out.println("Student : name=" + name + ", age=" + age + ", sid=" + sid );
}
}
public class StuDemo {
public static void main(String[] args) {
Student s1 = new Student("張三", 23);
Student s2 = new Student("李四", 24);
Student s3 = new Student("王五", 25);
Student s4 = new Student("趙六", 26);
s1.show(); // Student : name=張三, age=23, sid=1
s2.show(); // Student : name=李四, age=24, sid=2
s3.show(); // Student : name=王五, age=25, sid=3
s4.show(); // Student : name=趙六, age=26, sid=4
}
}

靜態方法

當 static 修飾成員方法時,該方法稱為類方法 。靜態方法在聲明中有 static ,建議使用類名來調用,而不需要 創建類的對象。調用方式非常簡單。

類方法:使用 static關鍵字修飾的成員方法,習慣稱為靜態方法。定義格式:

修飾符 static 返回值類型 方法名 (參數列表){
// 執行語句
}

舉例:在Student類中定義靜態方法


public static void showNum() {
System.out.println("num:" + numberOfStudent);
}

靜態方法調用的注意事項:

靜態方法可以直接訪問類變量和靜態方法。

靜態方法不能直接訪問普通成員變量或成員方法。反之,成員方法可以直接訪問類變量或靜態方法。

靜態方法中,不能使用this關鍵字。

小貼士:靜態方法只能訪問靜態成員。

調用格式

被static修飾的成員可以並且建議通過類名直接訪問。雖然也可以通過對象名訪問靜態成員,原因即多個對象均屬於一個類,共享使用同一個靜態成員,但是不建議,會出現警告信息。

格式:

// 訪問類變量類名.類變量名;

// 調用靜態方法

類名.靜態方法名(參數);

調用演示,代碼如下:

public class StuDemo2 {
public static void main(String[] args) {
// 訪問類變量
System.out.println(Student.numberOfStudent);
// 調用靜態方法
Student.showNum();
}
}

2.3靜態原理圖解

static 修飾的內容:

是隨著類的加載而加載的,且只加載一次。

存儲於一塊固定的內存區域(靜態區),所以,可以直接被類名調用。

它優先於對象存在,所以,可以被所有對象共享。

JAVA基礎String類、static關鍵字、Arrays類、Math類

2.4靜態代碼塊

靜態代碼塊:定義在成員位置,使用static修飾的代碼塊{ }。

位置:類中方法外。

執行:隨著類的加載而執行且執行一次,優先於main方法和構造方法的執行。

格式:

public class ClassName{ static {
// 執行語句
}
}

作用:給類變量進行初始化賦值。用法演示,代碼如下:


public class Game {
public static int number;
public static ArrayList<String> list;
static {
// 給類變量賦值
number = 2;
list = new ArrayList<String>();
// 添加元素到集合中
list.add("張三");
list.add("李四");
}
}

小貼士:

static 關鍵字,可以修飾變量、方法和代碼塊。在使用的過程中,其主要目的還是想在不創建對象的情況下,去調用方法。下面將介紹兩個工具類,來體現static 方法的便利。

第三章 Arrays類

3.1概述

java.util.Arrays 此類包含用來操作數組的各種方法,比如排序和搜索等。其所有方法均為靜態方法,調用起來 非常簡單。

3.2 操作數組的方法

public static String toString(int[] a) :返回指定數組內容的字符串表示形式。


public static void main(String[] args) {
// 定義int 數組
int[] arr = {2,34,35,4,657,8,69,9};
// 打印數組,輸出地址值
System.out.println(arr); // [I@2ac1fdc4
// 數組內容轉為字符串
String s = Arrays.toString(arr);
// 打印字符串,輸出內容
System.out.println(s); // [2, 34, 35, 4, 657, 8, 69, 9]
}

public static void sort(int[] a) :對指定的 int 型數組按數字升序進行排序。

public static void main(String[] args) {
// 定義int 數組
int[] arr = {24, 7, 5, 48, 4, 46, 35, 11, 6, 2};
System.out.println("排序前:"+ Arrays.toString(arr)); // 排序前:[24, 7, 5, 48, 4, 46, 35, 11, 6,
2]
// 升序排序
Arrays.sort(arr);
System.out.println("排序後:"+ Arrays.toString(arr));// 排序後:[2, 4, 5, 6, 7, 11, 24, 35, 46,
48]
}

3.3練習

請使用 Arrays 相關的API,將一個隨機字符串中的所有字符升序排列,並倒序打印

public class ArraysTest {
public static void main(String[] args) {
// 定義隨機的字符串
String line = "ysKUreaytWTRHsgFdSAoidq";
// 轉換為字符數組
char[] chars = line.toCharArray();
// 升序排序
Arrays.sort(chars);
// 反向遍歷打印
for (int i = chars.length‐1; i >= 0 ; i‐‐) {
System.out.print(chars[i]+" "); // y y t s s r q o i g e d d a W U T S R K H F A
}
}
}

第四章 Math類

4.1概述

java.lang.Math 類包含用於執行基本數學運算的方法,如初等指數、對數、平方根和三角函數。類似這樣的工具 類,其所有方法均為靜態方法,並且不會創建對象,調用起來非常簡單。

4.2 基本運算的方法

public static double abs(double a) :返回 double 值的絕對值。

double d1 = Math.abs(‐5); //d1的值為5 
double d2 = Math.abs(5); //d2的值為5

public static double ceil(double a) :返回大於等於參數的最小的整數。


double d1 = Math.ceil(3.3); //d1的值為 4.0
double d2 = Math.ceil(‐3.3); //d2的值為 ‐3.0
double d3 = Math.ceil(5.1); //d3的值為 6.0

public static double floor(double a) :返回小於等於參數最大的整數。

double d1 = Math.floor(3.3); //d1的值為3.0 
double d2 = Math.floor(‐3.3); //d2的值為‐4.0
double d3 = Math.floor(5.1); //d3的值為 5.0

public static long round(double a) :返回最接近參數的 long。(相當於四捨五入方法)

long d1 = Math.round(5.5); //d1的值為6.0
long d2 = Math.round(5.4); //d2的值為5.0

4.3 練習

請使用 Math 相關的API,計算在 -10.8 到 5.9 之間,絕對值大於 6 或者小於 2.1 的整數有多少個?

public class MathTest {
public static void main(String[] args) {
// 定義最小值
double min = ‐10.8;
// 定義最大值
double max = 5.9;
// 定義變量計數
int count = 0;
// 範圍內循環
for (double i = Math.ceil(min); i <= max; i++) {
// 獲取絕對值並判斷
if (Math.abs(i) > 6 || Math.abs(i) < 2.1) {
// 計 數
count++;
}
}
System.out.println("個數為: " + count + " 個");
}
}
"

相關推薦

推薦中...