1.邏輯運算符
a.&& 和& 的對比:兩者同時爲true時返回true
A && B 當表達式A爲true時,纔會進行表達式B的運算;即當表達式A爲false時不會進行表達式B的運算;
A & B 不管表達式A是否爲true,均會進行表達式B的運算;
故在實際中經常使用A && B,可以減少程序的運行次數。
b.|| 和| 的對比:兩者存在爲true時返回true
A || B 當表達式A爲true時,不會進行表達式B的運算,直接返回true;
A | B 不管表達式A是否爲false,均會進行表達式B的運算;
故在實際中經常使用A || B,可以減少程序的運行次數。
c.! 取反
!true -------------> false
!false -------------> true
d.異或 ^ :兩者不同時返回true,否則返回false
true ^ false -------------> true
true ^ true -------------> false
2.位運算符
5的二進制是101,6的二進制是110
5|6 對每一位進行或運算,得到 111->7
5&6 對每一位進行與運算,得到 100->4
5^6 對每一位進行或運算,得到 011->3
~5 5的二進制是00000101,所以取非即爲11111010,即爲-6
6<<1 6向左移1位後,變成1100,對應的10進制是12
6>>1 6向右移1位後,變成11,對應的10進制是3
>>> 無符號的右移
3.scanner的使用
Scanner s =
new
Scanner(System.in);
int
i = s.nextInt();
System.out.println(
"讀取的整數是"
+ i);
String rn = s.nextLine();
String a = s.nextLine();
System.out.println(
"讀取的字符串是:"
+a);
4.if的缺省寫法
if (false)
System.out.println("0");
System.out.println("1");
System.out.println("2");
會打印1,2;注意缺省下的if以後邊緊鄰的 ; 作爲分隔
5.continue,break,return
continue結束本次循環,繼續下次的循環;
break結束當前的循環,即內層的循環;
return 返回退出,即該方法不再執行;
如何根據內層循環結束外層循環?
6.數組
int[] a; //聲明一個引用
a =
new
int
[5]; //創建一個長度是5的數組,並且使用引用a指向該數組//分配了長度是5的數組,但是沒有賦值
int
[] b =
new
int
[
5
];
//聲明的同時,指向一個數組
System.out.println(a[0]);//沒有賦值,那麼就會使用默認值//作爲int類型的數組,默認值是0
a[
0
]=
1
;
//下標0,代表數組裏的第一個數
System.out.println(a.length); //打印數組的長度
數組訪問下標範圍是0到長度-1
a[5
]=
101
;
//下標5,實質上是“第6個”,超出範圍 ,產生數組下標越界異常
int[] a =
new
int
[]{
100
,
102
,
444
,
836
,
3236};//寫法一: 分配空間同時賦值
int[] b = {
100
,
102
,
444
,
836
,
3236};//寫法二: 省略了new int[],效果一樣
7.增強型for循環
int
values [] =
new
int
[]{
18
,
62
,
68
,
82
,
65
,
9
};
for
(
int
i =
0; i < values.length; i++) {//常規遍歷
System.out.println(values[i]);
}
for
(
int
each : values) {//增強型for循環遍歷
System.out.println(each);
}
8.二維數組
int b[][] = new int[][]{ {1,2,3} , {4,5,6} , {7,8,9} };
9.
System.arraycopy(src, srcPos, dest, destPos, length)
System.arraycopy(src, srcPos, dest, destPos, length) //把一個數組的值,複製到另一個數組中
需要事先準備好目標數組,並分配長度。
src: 源數組
srcPos: 從源數組複製數據的起始位置
dest: 目標數組
destPos: 複製到目標數組的起始位置
length: 複製的長度
10.數組工具類
Arrays
a.數組複製:通過返回值可自動得到目標數組
int a[] = new int[] { 0, 1, 2, 3, 4, 5 };
int[] b = Arrays.copyOfRange(a, 1, 4);//1 2 3//源數組,起始下標,終止下標
b.打印數組內容
int a[] = new int[] { 18, 62, 68, 82, 65, 9 };
String content = Arrays.toString(a);//[18, 62, 68, 82, 65, 9]
c.數組的自然排序
int a[] = new int[] { 18, 62, 68, 82, 65, 9 };
System.out.println(Arrays.toString(a));//[9, 18, 62, 65, 68, 82]
d.數組內容是否一致比較
Arrays.equals(a, b)
e.數組填充
int a[] = new int[3];
Arrays.fill(a, 5);
System.out.println(Arrays.toString(a));//[5, 5, 5]