java自學第4期——:Scanner類、匿名物件介紹、Random類、ArrayList集合、標準類格式、String類、static靜態、Arrays工具類、Math類(1)
阿新 • • 發佈:2020-12-07
一、Scanner類
1、api簡介:
應用程式程式設計介面
2、Scanner類:
- 作用:獲取鍵盤輸入的資料
- 位置: java.util.Scanner.
- 使用:使用成員方法nextInt() 和 next() 分別接收整型和字串型別資料
//將Scanner類例項化,並用System.in表示鍵盤輸入 Scanner scanner = new Scanner(System.in); //使用成員方法nextInt() 和next() 分別接收整型和字串型別資料 int num = scanner.nextInt(); String str = scanner.next()
- //next()和nextLine()的區別詳解:
next()方法在讀取內容時,會過濾掉有效字元前面的無效字元,對輸入有效字元之前遇到的空格鍵、Tab鍵或Enter鍵等結束符,next()方法會自動將其過濾掉;只有在讀取到有效字元之後,next()方法才將其後的空格鍵、Tab鍵或Enter鍵等視為結束符;所以next()方法不能得到帶空格的字串。 - nextLine()方法字面上有掃描一整行的意思,它的結束符只能是Enter鍵,即nextLine()方法返回的是Enter鍵之前沒有被讀取的所有字元,它是可以得到帶空格的字串的。
二、匿名物件
- 格式: new 類名稱();
- 注意:new一次使用一次,彼此互不關聯
- 使用:匿名物件作方法的返回值:
public static void main(String[] args){
method(new Scanner(System.in))
}
//匿名物件作方法的返回值
public static void method(Scanner sc){
int num = sc.nextInt();
String str = sc.next();
}
三、Random類
- 作用:獲取隨機數
- 位置:java.util.Random;
- 方法1:nextInt() : 返回一個任意範圍隨機數。
- 方法2:nextInt(int bound): 返回一個0~(bound-1)範圍內的隨機數。
Random random = new Random();
int num1 = random.nextInt();
int num2 = random.nextInt(11);//0~10
int num3 = sandom.nextInt(10)+1;//1~10
四、ArrayList集合類
- 作用:長度可以改變的集合
- 位置:java.util.ArrayList;
- 注意:ArrayList的泛型只能是引用型別,不能是基本資料型別;可通過基本資料型別的包裝類解決
- 使用:
public boolean add(E e): 新增元素,新增元素需要與泛型一致
public E get(int index) :從集合中獲取元素,引數是索引編號,返回值就是對應位置的元素
public E remove(int index) :從集合中刪除元素,引數是索引編號,返回值是被刪除掉的元素
public int size():獲取集合的尺寸長度,返回值是集合中包含的元素個數
//普通陣列
int[] array1 = new int[長度];
//物件陣列,可存放該型別的物件
物件型別[] 陣列名 = new 物件型別[長度];
//陣列長度不可變,因此可用長度可變的集合代替陣列
//ArrayList<E>的尖括號中的E代表泛型,指裝在集合中的所有元素都是統一的型別
//泛型只能是引用型別,不能是基本型別
//注意建立語句的寫法,右邊ArrayList<>()
ArrayList<String> array = new ArrayList<>();
//ArrayList直接打印出來的是內容,不是地址值,內容為空時列印中括號
System.out.println(array);
public static void main(String[] args) {
//建立
ArrayList<String> list = new ArrayList<>();
System.out.println(list);//[]
//新增add
list.add("原子裂變");
list.add("核子聚變");
list.add("高維打擊");
//[原子裂變, 核子聚變, 高維打擊]
System.out.println(list);
//獲取get
String string = list.get(1);//將返回值存入string中
System.out.println(string);
//刪除remove
list.remove(2);
System.out.println(list);
//獲取集合中的元素個數size
int num = list.size();
System.out.println(num);
}
//ArrayList<int> array = new ArrayList<>();
// 錯誤語句,ArrayList的泛型只能是引用型別,不能是基本資料型別;
// 集合裡儲存的都是地址值,但基本資料型別沒有地址值
// 基本型別的使用需要對應的“包裝類”
/*
基本型別 包裝類
byte Byte
short Short
int Integer *
long Long
float Float
double Double
char Character *
boolean Boolean
*/
ArrayList<Integer> array = new ArrayList<>();
array.add(12345);
array.add(13265);
array.add(15433);
System.out.println(array);
int a = array.get(2);
int b = array.size();
System.out.println(a);
System.out.println(b);
五、標準的類
//一個標準的類,應該包含以下四部分
public class demo07test2 {
//private 成員變數
private String name;
private int age;
//無參構造
public demo07test2(){
}
//全參構造
public demo07test2(String name,int age){
this.name = name;
this.age = age;
}
//get set方法
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public int getAge(){
return age;
}
public void setAge(int age){
this.age = age;
}
}
六、String類
- 作用:字串資料處理
- 位置: java.lang.String;
- 注意:
1.String類是不可變類,字串是常量,String物件建立後不可被修改直至被銷燬
2.字串可以共享使用,避免重複建立,節省記憶體
3.字串效果上是char[]陣列,但java9後是byte[]再加一個encoding-flag來儲存欄位,一個字串的每個字元只佔用1個字 - 建立字串的4種方式:
三種構造方法:
public String():建立一個空白字串,不含有任何內容;
public String(char[] array):根據字元陣列的內容建立相應的字串;
public String(byte[] array):根據位元組陣列的內容建立對應的字串;
一種直接建立方法:
String str = "hello world":雙引號直接建立了字串物件(JVM自動幫你new了物件)
String str1 = new String();
System.out.println(str1);//第一種方法
char[] array1 = {'n','e','w'};
String str2 = new String(array1);
System.out.println(str2);//第二種方法
byte[] array2 = {'b','e'};
String str3 = new String(array2);
System.out.println(str3);//第三種方法
String str4 = "newbe";
System.out.println(str4);//直接建立字串物件
- 字串比較方法:
public boolean equals(object obj):引數可以是任意物件,只有引數是一個字串且內容相同時才給true,否則false。任何物件都能用object進行接收。equalsIgnoreCase方法可以忽略大小寫比較 - 字串獲取相關
public int length(): 獲取字串的字元個數;
public String concat(String str):將當前字串和引數字串拼接成為返回值新的字串;
public char charAt(int index): 獲取指定索引位置的單個字元。索引從0開始;
public int indexOf(String str): 查詢引數字串在在本字串當中首次出現的索引位置,如果沒有則返回-1; - 字串的擷取方法:
public String substring(int index):擷取從引數位置一直到字串末尾,返回新的字串;
public String substring(int begin, int end):擷取從begin開始,一直到end結束中間的字串;左閉右開 - 字串轉換相關方法
public char[] toCharArray(): 將當前字串拆分成字元陣列作為返回值;
public byte[] getBytes(): 獲得當前字串底層的字元陣列;
public String replace(CharSequence oldString,charSequence newString): 將所有出現的老字串替換成新的字串; - 字串分割相關
public String[] split(String regex):按引數的規則將字串切割成若干部分;
若regex為".",則要寫成“\.” (正則表示式)
比較:
String str1 = "newbe";
String str2 = "newbe";
System.out.println(str1.equals(str2));//true
System.out.println("newbe".equalsIgnoreCase("NEWBE"));//true
獲取:
public static void main(String[] args) {
//獲取字串個數,length方法()
int num = "newswonderxswlawsl".length();
System.out.println(num);
String str1 = "awsl";
String str2 = "xswl";
String str3 = "2333";
//拼接str3.str2,contact方法
String str4 = str3.concat(str2);
String str5 = str4.concat(str1);
System.out.println(str5);
//查詢str1中第三個字元,charAt(int index)方法
char char1 = str1.charAt(2);
System.out.println(char1);
//查詢str1中s的索引值2,indexOf(String str)方法
int num2 = str1.indexOf('s');
int num3 = str1.indexOf('c');//沒有則返回-1
System.out.println(num2);
System.out.println(num3);
}
擷取:
public static void main(String[] args) {
String str1 = "2333xswl";
String str2 = str1.substring(4);//xswl
String str3 = str1.substring(4,7);//xsw
System.out.println(str2);
System.out.println(str3);
}
轉換
public static void main(String[] args ){
String str1 = "青山不改,綠水長流";
System.out.println(str1);
char[] char1 = str1.toCharArray();
System.out.println(char1);//列印 青山不改,綠水長流
byte[] byte2 = str1.getBytes();
System.out.println(byte2);//列印[B@10f87f48
System.out.println(byte2[2]);//列印-110
String sre2 = str1.replace("不","也");
System.out.println(sre2);//列印青山也改,綠水長流
}
分割:
public static void main(String[] args) {
String str1 = "aaa,bbb,ccc";
String[] str2 = str1.split(",");
for (int i = 0; i < str2.length; i++) {
System.out.println(str2[i]); //aaa bbb ccc
}
String str3 = "aaa bbb ccc";
String[] str4 = str3.split(" ");
for (int i = 0; i < str4.length; i++) {
System.out.println(str4[i]); //aaa bbb ccc
}
String str5 = "aaa.bbb.ccc";
String[] str6 = str5.split(".");
for (int i = 0; i < str6.length; i++) {
System.out.println(str6[i]); //無
}
String[] str8 = str5.split("\\.");
for (int i = 0; i < str8.length; i++) {
System.out.println(str8[i]); //aaa bbb ccc
}
}
如何統計字串中各種字元出現的次數?
public class demo08statistics {
public static void main(String[] args) {
String str1 = "abbCCC";
int a=0,b=0,C=0;
char[] chararray1 = str1.toCharArray();
for (int i = 0; i < chararray1.length; i++) {
if (chararray1[i] == 'a') a++;
if (chararray1[i] == 'b') b++;
if (chararray1[i] == 'C') C++;
}
System.out.println("a有"+ a + "個");
System.out.println("b有" + b +"個");
System.out.println("C有" + C + "個");
}
}
七、static靜態
- 注意:
用了static關鍵字的內容屬於類,所有物件共享同一份;
static修飾成員變數時,該變數不再屬於物件自己,而是屬於所在的類;
執行時靜態內容總是在前,非靜態在後
靜態方法中不能使用this關鍵字,this代表當前物件, 靜態方法不涉及物件; - 方法呼叫相關
沒有static的成員方法需要一個物件才能使用它;
成員方法通過建立物件再呼叫,靜態方法還可以且推薦直接用類名稱呼叫(方便區分成員方法),
當使用物件名呼叫靜態方法時實質上也是轉換成通過類名稱呼叫;
對於本類中的靜態方法,可以省略類名稱直接呼叫;
成員方法可以直接訪問成員變數和靜態變數,但靜態方法只能直接訪問靜態變數,
原因:記憶體中現有的靜態內容,後有的動態內容,先來的沒見過後來的,但後來的聽過先來者;
public static void main(String[] args) {
demo01basic one = new demo01basic();//建立物件,引用型別
one.method1();//通過物件呼叫成員方法
demo01basic.method2();//通過類呼叫靜態方法
method3();//對於本類中的靜態方法,可以省略類名稱直接呼叫;
}
public static void method3(){
System.out.println("xiuer");
}
int num1;//成員變數
static int num2;//靜態變數
public void method4(){
System.out.println(num1);
System.out.println(num2);//成員方法可以訪問成員變數和靜態變數
}
public static void method5(){
// System.out.println(num1); 報錯,靜態方法無法訪問成員變數
System.out.println(num2);//靜態方法可以訪問靜態變數
}
- 靜態程式碼塊:
public class ***{
static{
靜態程式碼塊的內容;
}
}
當第一次用到本類時,靜態程式碼塊執行唯一的一次;第二次執行時不會執行靜態程式碼塊
八、Arrays工具類
- 作用:實現陣列的常見操作
- 位置:java.util.arrays;
- 使用:
java.util.arrays是一個與陣列相關的工具類,裡面提供了大量的靜態方法,用來實現陣列的常見操作;
public static String toString(陣列): 將引數陣列變成字串,預設格式[元素1,元素2,元素3...]
public static void sort(陣列):按照預設升序(從小到大)對陣列元素進行排序;如果是自定義的型別,需要有Comparable或者Comparactor介面的支援;
public static void main(String[] args) {
char[] array1 = {'2','2','3','3','強','者'}; //定義一個數組
System.out.println(array1); //2233強者
System.out.println(Arrays.toString(array1)); //[2, 2, 3, 3, 強, 者]
int[] array2 = {2,4,1,3,6,5};
Arrays.sort(array2);
System.out.println(Arrays.toString(array2)); //; 注意語法 [1, 2, 3, 4, 5, 6]
String[] array3 = {"aaa","ccc","bbb"};
Arrays.sort(array3);
System.out.println(Arrays.toString(array3)); //字串預設按照字母升序排列 [aaa, bbb, ccc]
}
八、Math類
- 作用:這類 Math包含用於執行基本的數字運算等基本指數、對數、平方根法、三角函式。
- 位置:java.lang.Object;
- 方法:
public static double abs(double num):獲取絕對值
public static double ceil(double num): 向上取整
public static double floor(double num): 向下取整
public static long round(double num): 四捨五入
Math.PI:獲取PI的值
public static void main(String[] args) {
System.out.println(Math.abs(-3.14));//3.14
System.out.println(Math.ceil(3.14));//4.0
System.out.println(Math.floor(3.14));//3.0
System.out.println(Math.round(3.14));//3
System.out.println(Math.PI);//3.141592653589793
}