今日學(xué)習(xí)內(nèi)容總結(jié)
- 獲取用戶輸入元素
- 匿名對(duì)象
- Random方法
- 對(duì)象數(shù)組
- ArrayList
- String
Scanner
- 實(shí)現(xiàn)鍵盤輸入數(shù)據(jù)
- 導(dǎo)入包:import java.util.Scanner;
- 使用方法:Scanner scanner = new Scanner(System.in);
- System.in 代表從鍵盤輸入
- 獲得鍵盤輸入的數(shù)字:scanner.nextInt();
- 獲得鍵盤輸入的字符串:scanner.next();
例子
import java.util.Scanner;
public class InputSum {
public static void main(String[] args) {
System.out.println("輸入第一個(gè)數(shù)字");
Scanner scanner=new Scanner(System.in);
int one=scanner.nextInt();
System.out.println("輸入第二個(gè)數(shù)字");
int two=scanner.nextInt();
System.out.println("二者之和為:"+(one+two));
}
}
- 實(shí)現(xiàn)文件輸入與輸出
- 使用方法 :
讀取文件:Scanner in = new Scanner(Paths.get("myfile.txt"),"UTF-8");
絕對(duì)路徑:"C:\mydirectory\myfile.txt"
寫入文件可用:PrintWriter out = new PrintWriter("myfile.txt","UTF-8");
匿名對(duì)象
不需要賦值給等號(hào)左邊叶组,直接new。匿名對(duì)象只能訪問(wèn)一次成員變量或成員方法
System.out.println(new Scanner(System.in));
Random
- 導(dǎo)入包:import java.util.Random
- 創(chuàng)建:Random r = new Random();
- 使用:r.nextInt()正負(fù)所有Int范圍內(nèi)隨機(jī)生成
r.nextInt(100) 表示從0-100之間隨機(jī)生成數(shù)字枯饿,但不包括100
案例:猜數(shù)游戲
import java.util.Random;
import java.util.Scanner;
public class RandomGame {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
Random random=new Random();
int num=random.nextInt(30);
while (true){
System.out.println("請(qǐng)輸入數(shù)字");
int n=scanner.nextInt();
if (n>num){
System.out.println("大了");
}
else if(n<num){
System.out.println("小了");
}
else{
System.out.println("對(duì)了");
break;
}
}
}
}
對(duì)象數(shù)組
- 創(chuàng)建方法:類名[] 對(duì)象數(shù)組 = new 類名[n];
Person[] person = new Person[3];
Person one = new Person();
person[0] = one;
ArrayList<E>
<E>代表泛型荔棉,泛型只能是引用類型闹炉,不可以是基本類型,可以是包裝類
基本類型 包裝類
byte Byte
short Short
int Integer
long Long
char Character
float Float
double Double
boolean Boolean
相對(duì)于數(shù)組來(lái)說(shuō)润樱,數(shù)組長(zhǎng)度固定渣触;而ArrayList長(zhǎng)度不固定,可使用add()方法往里添加數(shù)據(jù)
- 導(dǎo)包:import import java.util.ArrayList;
- 創(chuàng)建:ArrayList<String> list = new ArrayList<>();
- 常用方法:add() 向ArrayList中添加數(shù)據(jù)
get() 從ArrayList中獲取數(shù)據(jù)
remove() 從ArrayList中刪除數(shù)據(jù)
size() 獲取ArrayList的長(zhǎng)度
ArrayList<String> list = new ArrayList<>();
list.add("Hello");
list.add("World");
list.add("Hello Java");
System.out.println(list);
System.out.println(list.get(1));
list.remove(0);
System.out.println(list);
System.out.println(list.size());
ArrayList遍歷:
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
String
- 共有四種創(chuàng)建方法:
- String str1 = new String();創(chuàng)建一個(gè)空白字符串
- char[] charArray = {'A','B','C'};
String str2 = new String(charArray);通過(guò)字符數(shù)組創(chuàng)建 - byte[] byteArray = {97,98,99};
String str3 = new String(byteArray);通過(guò)字節(jié)數(shù)組創(chuàng)建 - String str4 = " "; 直接創(chuàng)建
==是用來(lái)進(jìn)行對(duì)象的地址值比較祥国,比較字符串內(nèi)容用equals();
字符串==比較
- 1昵观、分割字符串:str.substring(0,n);代表從字符串中第0號(hào)元素到第n-1號(hào)元素中分離出來(lái)晾腔;
str.substring(int index)代表從index開(kāi)始截取一直到末尾結(jié)束;
例子:String str="Hello"; String s=str.substring(0,3);//結(jié)果為Hel
- 2啊犬、檢測(cè)字符串是否相等:equals方法
方法一:兩個(gè)字符串變量比較
str.equals(s);
方法二:字符串字面量與字符串變量比較
"Hello".equals(str);
方法三:不區(qū)分大小寫比較:equalsIgnoreCase
"Hello".equalsIgnoreCase("hello")
- 3灼擂、字符串長(zhǎng)度:str.length()
- 4、拼接字符串 str.concat(String str);
- 5觉至、獲取指定索引位置的單個(gè)字符:str.charAt(int index);
- 6剔应、查找參數(shù)字符串在本字符串首次出現(xiàn)的索引位置:str.indexOf(String str);
- 7、字符串中的轉(zhuǎn)換方法:
- 將字符串拆成字符數(shù)組toCharArray(); str.toCharArray();
- 將字符串拆分成字節(jié)數(shù)組 getBytes(); str.getBytes();
- 字符串內(nèi)容替換 replace(CharSequence oldstring,CharSequence newstring);str.replace("",""); - 8语御、分割字符串:split(String regex); str.split("");
注意:split方法的參數(shù)是一個(gè)正則表達(dá)式