java語言之數組-----選擇排序

來源:互聯網
上載者:User

標籤:

1. 數組的基本知識:數組是用來儲存變數的集合

2. 數組的聲明格式:元素類型 [] 數組引用變數名 = new 元素類型[元素個數] 

               如: int [] arrary = new int [10]  聲明並建立一個具有int類型的10個元素  arrary 是一個含有十個int 類型的元素數組的引用變數

3. 數組初始化:

    int [] arrary = new int [] {1,2,3,4,56,7};

或        int [] arrary = new int [6];

            arrary [0] = 1;............arrary [5] = 7;

或        int [] arrary = {1,2,3,4,56,7};

3. for each 迴圈 : 不使用下標就可以順序遍曆整個數組

            for ( double u : mylist ) {

          System.out.println (u);

}                        //對mylist中的每個元素進行以下操作

4. 複製數組的三種方法:

    1) 使用迴圈語句逐個複製數組元素

          如:int arrary1 [] = {1,2,3,4,5,6};  

            int arrary2 [] = new int [arrary1.length];

            for (int i = 0; i < arrary1.length; i ++){

            arrary2 [i] = arrary1 [i]; 

}          

       2)使用System類中的靜態方法arraryCopy

    3)使用clone方法複製數組

5. 給方法傳遞數組

    public static void printArrary(int [] arrary){

      for (int i = 0; i < arrary.length){

      System.out.println(arrary [i]);

  }

}

      public static void main ( String [] args){

      int [] getArrary = {1,2,3,4,5};

        printArrary(getArrary);

}

6. 選擇排序法

    先找到數組中最小的的數,然後把它放在數列的第一位。接下來在剩下來的數中找到最小數放在第一個數的後面,以此類推直到僅剩下一個數為止。

      如:

                       class Exerc05{

                              public static void selectSort(int []arrary1){
                                for (int i = 0; i<arrary1.length-1; i++){
                                     for (int j = i + 1; j<arrary1.length; j++){
                                        if (arrary1[i] > arrary1[j]){
                                             int temp = arrary1[i];
                                                 arrary1[i] = arrary1[j];
                                                 arrary1[j] = temp;
                                        }
                                 }
                          }
                }


                      public static void printArrary(int[] arrary3){
                         System.out.print("[");
                                 for (int k = 0; k<arrary3.length; k++){
                                       if (k!= arrary3.length - 1){
                                           System.out.print(arrary3[k] + ",");
                                       }
                                       else
                                         System.out.println(arrary3[k] + "]");
                                }
                       }


             }

 


                       class Exerc06{
                             public static void main(String[]args){
                                    Exerc05 arr = new Exerc05();

                                       int[] arrary2 = {1,3,6,7,2,9};

                                           //int arr1 = arr.selectSort(arrary2);不能將一個數組賦值給一個變數;編譯報錯;
                                              //排序前列印
                                                  arr.printArrary(arrary2);
                                                  //System.out.print(arr1);
                                                  arr.selectSort(arrary2);
                                                //排序後列印
                                           arr.printArrary(arrary2);
                                    }
                          }

 

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.