Java基礎--12.ArrayList,String,Math
一. ArrayList
1.1 概述
java.util.ArrayList 是大小可變的數組的實現,存儲在內的數據稱爲元素。此類提供一些方法來操作內部存儲
的元素。 ArrayList 中可不斷添加元素,其大小也自動增長.
1.2 使用步驟
1.2.1 查看類
java.util.ArrayList :該類需要 import導入使後使用。
,表示一種指定的數據類型,叫做泛型。 E ,取自Element(元素)的首字母。在出現 E 的地方,我們使用一種引用數據類型將其替換即可,表示我們將存儲哪種引用類型的元素.
ArrayList<String>,ArrayList<Student>
1.2.2 查看構造方法
public ArrayList() :構造一個內容爲空的集合
基本格式:
ArrayList<String> list = new ArrayList<String>();
1.2.3 查看成員方法
public boolean add(E e) :將指定的元素添加到此集合的尾部.
參數E e,在構造ArrayList對象時, 指定了什麼數據類型,那麼add(E e)方法中,只能添加什麼數據類型的對象.
ArrayList<String> list = new ArrayList<>();
//創建學生對象
String s1 = "曹操";
String s2 = "劉備";
String s3 = "孫權";
//把學生對象作爲元素添加到集合
list.add(s1);
list.add(s2);
list.add(s3);
1.2.4 常用方法和遍歷
對於元素的操作,基本體現在——增、刪、查。常用的方法有:
public boolean add(E e) :將指定的元素添加到此集合的尾部。
public E remove(int index) :移除此集合中指定位置上的元素。返回被刪除的元素。
public E get(int index) :返回此集合中指定位置上的元素。返回獲取的元素。
public int size() :返回此集合中的元素數.遍歷集合時,可以控制索引範圍,防止越界.
ArrayList<String> list = new ArrayList<String>();
//添加元素
list.add("hello");
list.add("world");
list.add("java");
//public E get(int index):返回指定索引處的元素
System.out.println("get:"+list.get(0));
System.out.println("get:"+list.get(1));
System.out.println("get:"+list.get(2));
//public int size():返回集合中的元素的個數
System.out.println("size:"+list.size());
//public E remove(int index):刪除指定索引處的元素,返回被刪除的元素
System.out.println("remove:"+list.remove(0));
//遍歷輸出
for(int i = 0; i < list.size(); i++){
System.out.println(list.get(i));
}
1.2.5 如何存儲基本數據類型
ArrayList對象不能存儲基本類型,只能存儲引用類型的數據。類似 不能寫,但是存儲基本數據類型對應的包裝類型是可以的。所以,想要存儲基本類型數據, <> 中的數據類型,必須轉換後才能編寫,轉換寫法如下:
基本類型 | 基本類型包裝類型 |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
二. String
2.1 概述
java.lang.String 類代表字符串。Java程序中所有的字符串文字(例如 “abc” )都可以被看作是實現此類的實
例。類 String 中包括用於檢查各個字符串的方法,比如用於比較字符串,搜索字符串,提取子字符串以及創建具有翻譯爲大寫或小寫的所有字符的字符串的副本。
2.2 使用步驟
查看構造方法:
String() 創建一個空的字符串對象
String(String original) 根據字符串來創建一個字符串對象
String(char[] value) 通過字符數組來創建字符串對象
String(byte[] bytes) 通過字節數組來構造新的字符串對象
String(byte[] bytes, int offffset, int length) 通過字節數組一部分來構造新的字符串對象
public static void main(String[] args) {
// 1.String() 創建一個空的字符串對象
String str1 = new String();
System.out.println("str1:" + str1);
// 2.String(String original) 根據字符串來創建一個字符串對象
String str2 = new String("abc");
System.out.println("str2:" + str2);
// 3.String(char[] value) 通過字符數組來創建字符串對象
char[] chs = {'A', 'B', 'C', 'D', 'E'};
String str5 = new String(chs);
System.out.println("str5:" + str5);
// 4.String(byte[] bytes) 通過字節數組來構造新的字符串對象
byte[] bytes = {97, 98, 99, 100, 101}; // a, b, c, d, e
String str3 = new String(bytes);
System.out.println("str3:" + str3);
// 5.String(byte[] bytes, int offset, int length) 通過字節數組一部分來構造新的字符串對象
String str4 = new String(bytes, 1, 2);
System.out.println("str4:" + str4);
String str6 = "abcde";
System.out.println("str6:" + str6);
}
2.3 特點
-
字符串不變:字符串的值在創建後不能被更改。
String s1 = "abc"; s1 += "d"; System.out.println(s1); // "abcd" // 內存中有"abc","abcd"兩個對象,s1從指向"abc",改變指向,指向了"abcd"。
-
因爲String對象是不可變的,所以它們可以被共享。
String s1 = "abc";
String s2 = "abc";
// 內存中只有一個"abc"對象被創建,同時被s1和s2共享。
2.4 常用方法
2.4.1 判斷功能的方法
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("‐‐‐‐‐‐‐‐‐‐‐");
}
}
2.4.2 獲取功能的方法
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));
}
}
2.4.3 轉換功能的方法
public char[] toCharArray () :將此字符串轉換爲新的字符數組。
public byte[] getBytes () :使用平臺的默認字符集將該 String編碼轉換爲新的字節數組。
public String toLowerCase() :使用默認語言環境的規則將此 String所有字符轉換爲小寫。
public String toUpperCase() :將此 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("‐‐‐‐‐‐‐‐‐‐‐");
}
}
2.4.4 分割功能的方法
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
}
}
}
三. Math
3.1 概述
java.lang.Math 類包含用於執行基本數學運算的方法,如初等指數、對數、平方根和三角函數。類似這樣的工具類,其所有方法均爲靜態方法,並且不會創建對象,調用起來非常簡單。
3.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