zoukankan      html  css  js  c++  java
  • 数组

     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语句只能返回一个,但使用数组,可返回数组地址,间接返回多个值

  • 相关阅读:
    导入动态页面的两种方法
    JSTL之c:set
    CentOS更新源
    MVC轻量web应用
    Linux设备驱动开发流程(转)
    g++: internal compiler error: Killed (program cc1plus)Please submit a full bug report,内存不足问题解决
    .PHONY的作用
    CMake(转)
    关于a+++++b含义的问题
    返回值为函数指针的函数(转)
  • 原文地址:https://www.cnblogs.com/XiaoJin0/p/10538326.html
Copyright © 2011-2022 走看看