Scanner
import java.util.Scanner;
public class TestShunxu {
public static void main(String[] args) {
/**
* next()與nextLine()比較:
* next():
* 1、一定要讀取到有效字符後纔可以結束輸入。
* 2、對輸入有效字符之前遇到的空白,next() 方法會自動將其去掉。
* 3、只有輸入有效字符後纔將其後面輸入的空白作爲分隔符或者結束符。
* 4、next() 不能得到帶有空格的字符串。
* nextLine():
* 1、以Enter爲結束符,也就是說 nextLine()方法返回的是輸入回車之前的所有字符。
* 2、可以獲得空白。
*/
Scanner sc = new Scanner(System.in);
if ( sc.hasNext() ) {
// 此時輸入“hello world”
String str = sc.next();
System.out.println(str);// hello
}
Scanner sc2 = new Scanner(System.in);
if ( sc.hasNextLine() ) {
// 此時輸入“hello world”
String str2 = sc2.nextLine();
System.out.println(str2);// hello world
}
Scanner sc3 = new Scanner(System.in);
int sum = 0;
while ( sc3.hasNextInt() ) { // 此處死循環,但輸入的不是int時就自動結束
int i = sc3.nextInt();
sum += i;
}
System.out.println("總和:"+sum);
//還有 nextInt nextFloat nextDouble nextLong
//要注意:if中使用什麼方法在sc點的時候也用同樣的方法,不然牛客網 leetcode等網站時測試用例不通過
sc.close();//規範化最好關閉
}
}
switch
// switch表達式支持byte short int char string
// case穿透,不寫break下面的case仍然會走下去
int i = 0;
switch ( i ) {
case 1:
System.out.println(111);
break;
case 2:
System.out.println(222);
break;
case 3:
System.out.println(333);
break;
case 4:
System.out.println(444);
break;
default:
System.out.println("no founddddddddddddddddd");
}
可變參數
如果該方法除了可變參數還有其它的參數,可變參數必須放到最後;
調用使用了可變參數的方法時:
a. 可以不寫參數,即傳入空參;
b. 可以直接在裏邊寫入參數,參數間用逗號隔開;
c. 可以傳入一個數組;
擁有可變參數的方法可以被重載,在被調用時,如果能匹配到參數定長的方法則優先調用參數定長的方法。
可變參數可以兼容數組參數,但數組參數無法兼容可變參數,所以試圖使用數組作爲參數去實現重載時,會報錯,說明可變參數與數組衝突
//求若干個整型數中的最大值
public int getMax(int... items){ //定義可變參數items
int max = Integer.MIN_VALUE;
for(int item : items){
max = item > max? item : max; //取大值
}
return max;
}
遞歸
JAVA中使用遞歸和尾遞歸實現1000的階乘的比較.
在JAVA中求階乘首先遇到的問題就是結果溢出,不管是使用int還是long,double都無法表示1000!這麼大的天文數字,這裏暫且用BigInteger解決這個問題!
下面是使用遞歸和尾遞歸分別計算1000的階乘:
mport java.math.BigInteger;
public class TestShunxu {
public static void main(String[] args) {
long t = System.currentTimeMillis();
System.out.println(factorial(new BigInteger("1000")));
System.out.println(System.currentTimeMillis()- t);
t = System.currentTimeMillis();
System.out.println(factorial2(new BigInteger("1000"),BigInteger.ONE));
System.out.println(System.currentTimeMillis()- t);
}
/**
* 使用線性遞歸計算階乘
* @param n
* @return
*/
public static BigInteger factorial(BigInteger n ){
if (n.compareTo(BigInteger.ZERO) < 0) {
return BigInteger.ZERO;
}
if (n.equals(BigInteger.ONE) || n.equals(BigInteger.ZERO)) {
return new BigInteger("1");
}
return n.multiply(factorial(n.subtract(BigInteger.ONE)));
}
/**
* 使用尾遞歸計算階乘
* 如果一個函數中所有遞歸形式的調用都出現在函數的末尾,我們稱這個遞歸函數是尾遞歸的。
* 當遞歸調用是整個函數體中最後執行的語句且它的返回值不屬於表達式的一部分時,這個遞歸調用就是尾遞歸。
* 尾遞歸函數的特點是在迴歸過程中不用做任何操作,這個特性很重要,因爲大多數現代的編譯器會利用這種特點自動生成優化的代碼。
* 尾遞歸是極其重要的,不用尾遞歸,函數的堆棧耗用難以估量,需要保存很多中間函數的堆棧。
* 通過參數傳遞結果,達到不壓棧的目的
* @param n
* @param result
* @return
*/
public static BigInteger factorial2(BigInteger n,BigInteger result){
if (n.compareTo(BigInteger.ZERO) < 0) {
return BigInteger.ZERO;
}
if (n.equals(BigInteger.ONE) || n.equals(BigInteger.ZERO)) {
return result;
}
return factorial2(n.subtract(BigInteger.ONE),n.multiply(result));
}
}
數組
// 靜態初始化
int[] a = {1, 2, 3, 4, 5};
// 動態初始化 默認值全部爲默認值
int[] b = new int[5];
// 一旦被創建,長度不可改變,複製等新建操作其實是new了一個新的數組
// 儲存的對象類型必須相同
// 可以使基本類型,也可以是引用類型
// 數組本身屬於引用類型
// 有序
// 二維數組
int[][] a = { {1,2}, {1,2,3}, {1,2,3,4} };
System.out.println( Arrays.toString(a) );// [[I@b4c966a, [I@2f4d3709, [I@4e50df2e]
System.out.println( Arrays.deepToString(a) );// [[1, 2], [1, 2, 3], [1, 2, 3, 4]]