java函數與數組

來源:互聯網
上載者:User

標籤:java

一、函數

函數格式:

修飾符 傳回值類型 函數名(參數類型 形式參數1,參數類型 形式參數2,…)

    {

           執行語句;

           return 傳回值;

    }

 

傳回值類型: 運算結果的類型,如果函數沒有傳回值則用void來表示

函數名:     標識符,名字任意取

形式參數:   是一個變數,用於儲存調用函數傳遞進來的實際參數

實際參數:   傳遞給形式參數的具體數值

傳回值:     返回給調用者

 

說明:

1)main方法是程式的入口,由虛擬機器調用,方法和方法之間不能嵌套,方法之間通過調用來使用;

2)對於函數沒有具體傳回值的情況,傳回值類型用關鍵字void表示,那麼該函數中的return語句如果在最後一行可以省略不寫;

3)函數重載:在同一個類中,有一個以上的同名函數,函數的參數列表或參數類型不一樣

  

例子

[[email protected] java]# cat Demo9.java public class Demo9 {    public static void main(String[] args) {    int i = 10;    int j = 20;    int max = getMax(i,j);    System.out.println(max);    }        public static int getMax(int x,int y) {    int result;    if (x>=y) {        result = x;    } else {        result =y;    }    return result;    } } //運行結果[[email protected] java]# java Demo920  [[email protected] java]# cat Demo10.java public class Demo10 {    public static void main(String[] args) {        getResult(5); }    public static void getResult(int x) {        System.out.println( x*8);} } //運行結果[[email protected] java]# java Demo1040


[[email protected] java]# cat Demo11.java public class Demo11 {    public static void main(String[] args) {    draw(10,20);    System.out.println(equalas(10,20)); }    public static void draw(int width,int height) {        for (int i = 0; i < width; i++) {            for (int j = 0; j < height; j++) {                System.out.print("*");            }        System.out.println();         }     }      public static boolean equalas(int a, int b) {        return a==b?true:false;        } } //運行結果[[email protected] java]# javac Demo11.java [[email protected] java]# java Demo11********************************************************************************************************************************************************************************************************false


  

二、數組

1.定義

格式一:

元素類型[] 數組名 = new 元素類型[元素個數或數組長度];

樣本:int[] arr = new int[5];

 

格式二:

元素類型[] 數組名 = new 元素類型[]{元素,元素,……};

樣本:int[] arr = new int[]{3,5,1,7};

      int[] arr = {3,5,1,7};

特點:直接明確數組的長度,以及數組中元素的內容

 

說明:

1)左半部分:聲明陣列變數

指定數組的元素類型、數組標識

2)右半部分:建立數組

用到關鍵字new,在記憶體中產生一個容器實體,開闢空間大小

3)建立數組之後不能修改數組的大小,可以使用length屬性擷取數組的大小

 

 

2.初始化

方式一:   

int[] arr3=new int[3];arr3[0]=1;arr3[1]=5;arr3[2]=6;

 

方式二:不使用運算子new

int[] arr = { 1, 2, 3, 4, 5 };int[] arr2 = new int[] { 1, 2, 3, 4, 5 };

 

3.數組遍曆

public static void main(String[] args) {       int[] x = { 1, 2, 3 };       for (int y = 0; y < x.length; y++) {           System.out.println(x[y]);       }    }

 

例子

//直接排序[[email protected] java]# cat Demo11.java public class Demo11 {    public static void main(String[] args) {    int[] arr = new int[3];    arr[0] = 1;    arr[1] = 3;    arr[2] = 2;    int max = arr[0];    for (int i=0; i<arr.length-1; i++) {        for (int j=i+1; j<arr.length; j++) {            if (arr[i] > arr[j]) {                 max = arr[i];                 arr[i] = arr[j];                 arr[j] = max;                }             }        }        System.out.println(arr[0] +"\n" +arr[1] +"\n" +arr[2]);   } } //運行結果[[email protected] java]# java Demo11123

 


//冒泡排序[[email protected] java]# cat Demo11.java public class Demo11 {    public static void main(String[] args) {    int[] arr = new int[3];    arr[0] = 1;    arr[1] = 3;    arr[2] = 2;    int max = arr[0];    for (int i=0; i<arr.length-1; i++) {        for (int j=i+1; j<arr.length-1-i; j++) {            if (arr[j] > arr[j+1]) {                 max = arr[j];                 arr[j] = arr[j+1];                 arr[j+1] = max;                }             }        }        System.out.println(arr[0] +"\n" +arr[1] +"\n" +arr[2]);   } } //運行結果[[email protected] java]# java Demo11123


 

三、數組(2維)

1.定義:

    數群組類型[][] 數組名 = new 數群組類型[一維數組的個數][每一個一維數組中元素的個數];

 

2.初始化:

2.1 靜態初始化

    int [][] a = new int[][]{ {12,34,45,89},{34,56,78,10},{1,3,6,4} };


2.2 動態初始化


 

 

例子

[[email protected] java]# cat Demo12.java public class Demo12 {    public static void getArr2(int [][] a) {        for (int i = 0; i<a.length; i++) {            for (int j =0 ; j<a[i].length; j++) {                    System.out.print(a[i][j] +" ,");                    }            }        System.out.println();    }     public static long getSum(int [][] a) {        long sum = 0L;        for (int i =0; i<a.length; i++) {            for (int j =0; j<a[i].length; j++) {                    sum += a[i][j];                }            }        return sum;    }     public static int getCount(int [][] a) {        int count = 0;        for (int i =0; i<a.length; i++) {            for (int j =0; j<a[i].length; j++) {                count++;              }        }        return count ;    }        public static void main(String[] args) {        int [][] a = new int [][]{{1,2,3,4,5,6},{6,5,4,3,2,1},{8,9,0}};        getArr2(a);        System.out.println("二維數組和:" +getSum(a));        System.out.println("二維數組元素個數:" +getCount(a));     } } //運行結果[[email protected] java]# java Demo121 ,2 ,3 ,4 ,5 ,6 ,6 ,5 ,4 ,3 ,2 ,1 ,8 ,9 ,0 ,二維數組和:59二維數組元素個數:15


 


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.