java 數組排序、最大值、最小值

來源:互聯網
上載者:User

原文:http://liyaxi.iteye.com/blog/856334

public class ArraysTest {

/**
* 數組排序 
* 數組中的最大值 

* 數組中的最小值 */

public int[] aa = new int[10];

public void initArray() {  
        int[] myarray = { 2, 34, 5, 59, 23, 3, 56, 34, 9, 10 };  
        aa = myarray;  
        System.out.println("-----未排序之前輸出。。--------");  
        for (int i = 0; i < aa.length; i++) {  
            System.out.println(aa[i]);  
        }  
        System.out.println("--------------------");  
        }  
public void arraysort() {  //先排序
        Arrays.sort(aa);  //對數組aa進行排序
        int length = aa.length;
        System.out.println("----------排序後輸出----------");  
        for (int i = 0; i < length; i++) {  
            System.out.println(aa[i]);  
        }  
        System.out.println("--------------------");  
      }  
      public int getMaxValue() {  
        return aa[aa.length - 1];  //返回數組下標[數組長度減一]
      }  
      public int getMinValue() {  
        return aa[0];  //數組的第一位
     }  
    
     public static void main(String[] args) {
ArraysTest at = new ArraysTest();  
 
        at.initArray();  
        at.arraysort();  
        System.out.println("---------------------");  

        

System.out.println("最大值:" + at.getMaxValue());  

        System.out.println("最小值:" + at.getMinValue());  
}

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.