1. 程式人生 > >數組中如何按照表中顯示

數組中如何按照表中顯示

內容 ati public text clas else 返回 args 功能

package wac.wev.LianXi;
/*

  • 需求1:需要定義遍歷元素的功能,輸出結果:[元素1, 元素2, 元素3, 元素4, 元素5...]
  • 2:單獨調用
  • */
    public class Text {
    public static void main(String[] args){
    //創建一個數組
    int[] arr = {13,25,1,25,32,13,2,3,4,5,24,79};
    bianLi(arr);
    }
    //明確返回值:void
    //明確參數類型和參數個數:int[] arr
    public static void bianLi(int[] arr){
    //確定輸出結構內容,首先輸出"["
    System.out.print("[");
    //遍歷數組的元素
    for(int x =0;x<arr.length;x++){
    if(x==arr.length-1){//限定X的範圍
    System.out.print(arr[x]+"]");//如果符合要求輸出的內容,和格式
    }else{
    System.out.print(arr[x]+", ");//如果不符合要求的內容和格式
    }
    }
    }
    }

數組中如何按照表中顯示