java:第八章

來源:互聯網
上載者:User

標籤:scanner   數組名   bounds   length   case   span   tin   java   ext   

第八章 數組

     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:繼續(表示結束本輪迴圈,進入下一輪迴圈)
        注意:多層迴圈,只會對直接的迴圈起作用。

 

例:數組逆序後排序public static void main(String[] args){        int[] aa = new int[]{1,3,-1,5,-2};        System.out.print("原數組為:");        for(int i=0;i<aa.length;i++){        System.out.print(aa[i]+"\t");            }        System.out.println("");        System.out.print("逆序並處理後的數組為:");        for(int j = aa.length-1;j>=0;j--){            if(aa[j]<0){                aa[j]=0;            }System.out.print((aa[j]+"\t"));        }    }例:在數組中插入值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));    }

 

java:第八章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.