第八章 數組
?????1.數組的聲明定義
???????? 數據類型[]變量名 = new 數據類型[長度];
???????? 列:int[]ary = new int[5];
???? 2.取值,賦值
???????? 取值:數據名[下標];
???????? 列:int a = ary[1];
???????? 賦值:變量=數據名[下標];
???????? 列:ary[1]=10;
???? 3.數組的遍歷
???????? 數組的長度:數組名.length;
???????? for(int i=0;i<數組名.length;i++){
????? //數組名[i]:訪問每個元素的值
? }
???? 4.數組常見的異常
???? ? 數組下標越界
???????? ArrayIndexOutOfBoundsException
???????? 當訪問數組的下標超過0~length-1時,
???????? 就會出現以上的錯誤。
???????? 注意:數組下標范圍:0~length-1
???? 5.數組常用的方法
???????? Arrays.toString(數組名);//展示數組內容
???????? Arrays.sort(數組名);??? //數組升序排列
???? 6.后序遍歷
???????? for(int i = ary.length-1;i>=0;i--){
???????????? ary[i]
???????? }
???? 7.比較字符串的大小
???????? 如果a.compareIgnoreCase(b)>0為true,
???????????? 那么a>b.
???????? 如果a.compareIgnoreCase(b)<0為true,
???????????? 那么a<b.
???????? 如果a.compareIgnoreCase(b)=0為true,
???????????? 那么a=b.
???? 8.break和continue
??????? break:終止,結束(表示終止當前循環結構)
??????? continue:繼續(表示結束本輪循環,進入下一輪循環)
??????? 注意:多層循環,只會對直接的循環起作用。
?


第八章 數組1.數組的聲明定義數據類型[]變量名 = new 數據類型[長度];列:int[]ary = new int[5];2.取值,賦值取值:數據名[下標];列:int a = ary[1];賦值:變量=數據名[下標];列:ary[1]=10;3.數組的遍歷數組的長度:數組名.length;for(int i=0;i<數組名.length;i++){//數組名[i]:訪問每個元素的值 }4.數組常見的異常數組下標越界ArrayIndexOutOfBoundsException當訪問數組的下標超過0~length-1時,就會出現以上的錯誤。注意:數組下標范圍:0~length-15.數組常用的方法Arrays.toString(數組名);//展示數組內容Arrays.sort(數組名); //數組升序排列6.后序遍歷for(int i = ary.length-1;i>=0;i--){ary[i]}7.比較字符串的大小如果a.compareIgnoreCase(b)>0為true,那么a>b.如果a.compareIgnoreCase(b)<0為true,那么a<b.如果a.compareIgnoreCase(b)=0為true,那么a=b.8.break和continuebreak:終止,結束(表示終止當前循環結構)continue:繼續(表示結束本輪循環,進入下一輪循環)注意:多層循環,只會對直接的循環起作用。


例:在數組中插入值public static void main(String[] args){int[] ary = new int[6];ary[0] = 60;ary[1] = 63;ary[2] = 82;ary[3] = 85;ary[4] = 99;int index = 0;Scanner console = new Scanner(System.in);System.out.println("請輸入一個數:");int num = console.nextInt();for(int i = 0;i<ary.length;i++){if(ary[i]>num){index = i;System.out.println(ary[i]);break;}}for(int i =4;i>=index;i--){ary[i+1] = ary[i];}ary[index] = num;System.out.println(Arrays.toString(ary));}
?