首页 > 其他 > 详细

跟随学习第二十一天

时间:2021-03-01 09:28:47      阅读:23      评论:0      收藏:0      [点我收藏+]

数组的使用

  • For-Each循环也叫增强型for循环

 public class Demo{
     public static void main(){
         int[] array = {1,2,3,4,5};//定义一个数组
         //这个增强型for循环只适用于jdk1.5以上的,而且没有下标
         for(int array : array){//前面的变量是数组里面的每个元素,后面的array是数组名
             System.out.pintln(array);
        }
    }
 }
  • 数组用方法进行使用

 public class ArrayDemo02 {
     
     public static void main(String[] args) {
 ?
         int[] array = {1,2,3,4,5};
 ?
         PrintArray(array);//调用定义的方法进行元素的输出
    }
         public static void PrintArray(int[] array){//定义一个方法进行输出数组元素
             for(int i=0; i< array.length;i++){
 ?
                 System.out.println(array[i]);
            }
        }
    }
  • 数组作为返回值

 public class ArrayDemo02 {
 ?
     public static void main(String[] args) {
 ?
         int[] array = {1, 2, 3, 4, 5};
 ?
         PrintArray(array);//调用定义的方法进行元素的输出
         
         int[] reverse = reverse(array);//
 ?
         PrintArray(reverse);//运用刚刚写得方法进行输出
    }
 ?
     public static void PrintArray(int[] array) {//定义一个方法进行输出数组元素
         for (int i = 0; i < array.length; i++) {
 ?
             System.out.println(array[i]);
        }
    }
 ?
     public static int[] reverse(int[] arrays) {//用作返回值的时候
 ?
         int[] result = new int[arrays.length];//定义一个新数组
         for (int i = 0, j = arrays.length - 1; i < arrays.length; i++, j--) {
 ?
             result[j] = arrays[i];//进行数组元素翻转
 ?
        }
         return result;//返回数组的值
    }
 }

 

跟随学习第二十一天

原文:https://www.cnblogs.com/xiawan/p/14461476.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!