zoukankan      html  css  js  c++  java
  • 遍历数组

    //用方法调用 数组中的最大值
    
    public class MethodTest02 {
        public static void main(String[] args) {
            int[] arr = {11,22,54,23,1,99,5,77};
            int number = getMax(arr);
            System.out.println("number: " + number);
        }
        public static int getMax(int[] arr){
            int max = arr[0];
            for(int x = 1;x < arr.length;x++){
                if(arr[x] > max){
                    max = arr[x];
                }
            }
            return max;
        }
    }
    //用方法的形式 遍历打印一个数组:
    public class MethodTest01 {
        public static void main(String[] args) {
            int arr[] = {11,22,33,44,55,88};
    
            printArray(arr);
        }
    
        public static void printArray(int[] arr){
            System.out.print("[");
            for(int x=0;x<arr.length;x++){
                if(x == arr.length-1){              //遍历到最后一个元素的写法
                    System.out.print(arr[x]);
                }else{
                    System.out.print(arr[x] + "," );
                }
    
            }
            System.out.println("]");
        }
    } 

      


    //遍历数组最大值: public class ArryMax { public static void main(String[] args) { int[] arr = {12,45,98,73,60}; int max = arr[0]; for(int i=1; i<arr.length; i++){ if(arr[i] > max){ max = arr[i]; } } System.out.println("max:" + max); } } public class ArryMin { public static void main(String[] args) { int[] arr = {12,45,98,73,60}; int min = arr[0]; for(int i=1; i<arr.length; i++){ if(arr[i] < min){ min = arr[i]; } } System.out.println("min:" + min); } } //遍历数组 public class ArrDemo02 { public static void main(String[] args) { int[] arr = {11,22,33,44,55,66}; for(int i=0; i<arr.length; i++){ System.out.println(arr[i]); } } }
  • 相关阅读:
    ajax如何调用本页数据源不用一般处理程序
    管理员IP匹配方法
    Silverlight DataGrid赋数据源自动生成列表
    Winform WebBrowser加上进度条
    asp.net的几个帮助类
    asp.net App.config配置文件帮助类
    查找集合中某个元素的位置和某个元素的集合
    sqlServer通用分页
    定时执行某个方法
    关于IE6.7.8.FF兼容的问题
  • 原文地址:https://www.cnblogs.com/walkersss/p/14900725.html
Copyright © 2011-2022 走看看