数组

 1 //数组元素求最值,反转
 2 public class Array {
 3     public static void main(String[] args) {
 4         int[] array=new int[]{50,47,68,24,100};
 5         int max,min;
 6         max=min=array[0];
 7         for(int i=1;i<array.length;i++)
 8         {if(array[i]>max)
 9                 max=array[i];
10             if(array[i]<min)
11                 min=array[i];}
12         System.out.println("最大值:"+max);
13         System.out.println("最小值:"+min);
14         int i=0;
15         int right=array.length-1;
16         for(;i<array.length/2;i++,right--)
17         {int t=array[i];
18         array[i]=array[right];
19         array[right]=t;}
20         for (i = 0; i < array.length; i++) {
21             System.out.println(array[i]);
22         }
23 
24     }
25 }
26 -----------------------------
27 最大值:100
28 最小值:24
29 100
30 24
31 68
32 47
33 50
34 
35 Process finished with exit code 0

数组作为函数的返回值(地址传递)

 1 public class Methodarray {
 2     public static void main(String[] args) {
 3         double[] a=cal(2.2,3.5,6.8);
 4         System.out.println(a[0]);
 5         System.out.println(a[1]);
 6         
 7     }
 8     public static double[] cal(double a,double b,double c){
 9         double[] array={a+b+c,(a+b+c)/3};
10         return array;
11     }
12 }
13 ----------------------------------------------
14 12.5
15 4.166666666666667
16 
17 Process finished with exit code 0

return语句只能返回一个,但使用数组,可返回数组地址,间接返回多个值

 

posted @ 2019-03-15 17:18  超人不会飞0  阅读(102)  评论(0编辑  收藏  举报