java7
第八章 數(shù)組
?????1.數(shù)組的聲明定義
???????? 數(shù)據(jù)類型[]變量名 = new 數(shù)據(jù)類型[長(zhǎng)度];
???????? 列:int[]ary = new int[5];
???? 2.取值,賦值
???????? 取值:數(shù)據(jù)名[下標(biāo)];
???????? 列:int a = ary[1];
???????? 賦值:變量=數(shù)據(jù)名[下標(biāo)];
???????? 列:ary[1]=10;
???? 3.數(shù)組的遍歷
???????? 數(shù)組的長(zhǎng)度:數(shù)組名.length;
???????? for(int i=0;i<數(shù)組名.length;i++){
????? //數(shù)組名[i]:訪問每個(gè)元素的值
? }
???? 4.數(shù)組常見的異常
???? ? 數(shù)組下標(biāo)越界
???????? ArrayIndexOutOfBoundsException
???????? 當(dāng)訪問數(shù)組的下標(biāo)超過0~length-1時(shí),
???????? 就會(huì)出現(xiàn)以上的錯(cuò)誤。
???????? 注意:數(shù)組下標(biāo)范圍:0~length-1
???? 5.數(shù)組常用的方法
???????? Arrays.toString(數(shù)組名);//展示數(shù)組內(nèi)容
???????? Arrays.sort(數(shù)組名);??? //數(shù)組升序排列
???? 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:終止,結(jié)束(表示終止當(dāng)前循環(huán)結(jié)構(gòu))
??????? continue:繼續(xù)(表示結(jié)束本輪循環(huán),進(jìn)入下一輪循環(huán))
??????? 注意:多層循環(huán),只會(huì)對(duì)直接的循環(huán)起作用。
?
第八章 數(shù)組1.數(shù)組的聲明定義數(shù)據(jù)類型[]變量名 = new 數(shù)據(jù)類型[長(zhǎng)度];列:int[]ary = new int[5];2.取值,賦值取值:數(shù)據(jù)名[下標(biāo)];列:int a = ary[1];賦值:變量=數(shù)據(jù)名[下標(biāo)];列:ary[1]=10;3.數(shù)組的遍歷數(shù)組的長(zhǎng)度:數(shù)組名.length;for(int i=0;i<數(shù)組名.length;i++){//數(shù)組名[i]:訪問每個(gè)元素的值 }4.數(shù)組常見的異常數(shù)組下標(biāo)越界ArrayIndexOutOfBoundsException當(dāng)訪問數(shù)組的下標(biāo)超過0~length-1時(shí),就會(huì)出現(xiàn)以上的錯(cuò)誤。注意:數(shù)組下標(biāo)范圍:0~length-15.數(shù)組常用的方法Arrays.toString(數(shù)組名);//展示數(shù)組內(nèi)容Arrays.sort(數(shù)組名); //數(shù)組升序排列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:終止,結(jié)束(表示終止當(dāng)前循環(huán)結(jié)構(gòu))continue:繼續(xù)(表示結(jié)束本輪循環(huán),進(jìn)入下一輪循環(huán))注意:多層循環(huán),只會(huì)對(duì)直接的循環(huán)起作用。 View Code 例:在數(shù)組中插入值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("請(qǐng)輸入一個(gè)數(shù):");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));} View Code?
轉(zhuǎn)載于:https://www.cnblogs.com/wojiatingting/p/6962583.html
總結(jié)
- 上一篇: 兴业信用卡现金分期多久到账?没到账怎么办
- 下一篇: CentOS 7 搭建JAVA环境