Java数组学习003数组的使用

数组使用

  • 普通的For循环

  • For-Each循环

  • 数组作方法入参

  • 数组作返回值

public static void main(String[] args) {
       int[] arrays = {1,2,3,4,5,};
       //没有下标
       for (int array : arrays) {
           System.out.println(array);//输出所有数
      }
       //打印数组元素
   public static void printArray(int[]arrays){
       for (int i = 0; i < arrays.length; i++) {
           System.out.print(arrays[i]+" ");
      }
  }

例子:

反转

public static void main(String[] args) {
       int[] arrays = {1,2,3,4,5,};
       int[] reverse = reverse(arrays);
  }
   //反转打印
   public static int[] reverse(int[]arrays){
       int[]result = new int[arrays.length];
       for (int i = 0,j=result.length-1; i <arrays.length ; i++,j--) {
           result[j] = arrays[i];
           System.out.print(arrays[j]+" ");
      }
       return result;
  }
}

 

posted @ 2021-02-21 10:30  鍠钺  阅读(33)  评论(0)    收藏  举报