zoukankan      html  css  js  c++  java
  • day26(数组的使用)

    数组的使用

    • For-Each循环
    • 普通的for循环
    • 数组作方法入参
    • 数组作返回值
    package com.kuang.array;
    
    public class ArrayDemo03 {
        public static void main(String[] args) {
            int[] arrays = {1,2,3,4,5};
    
            //打印全部的数组元素
            for (int i = 0; i < arrays.length ; i++) {
                System.out.print(arrays[i]+"	");//结果  1  2  3  4  5
            }
            System.out.println(" ");
            System.out.println("=======================================");
            //计算所有元素的和
            int sum = 0;
            for (int i = 0; i < arrays.length; i++) {
                sum += arrays[i];
            }
            System.out.println("sum="+sum);//sum=15
            System.out.println("=======================================");
            //查找最大元素
            int max = arrays[0];
            for (int i = 1; i <arrays.length ; i++) {
                if(arrays[i]>max){
                    max = arrays[i];
                }
            }
            System.out.println("max="+max);//max=5
        }
    }
    
    package com.kuang.array;
    
    public class ArrayDemo04 {
        public static void main(String[] args) {
            int[] arrays = {1,2,3,4,5};
        //JDK1.5,省去下标
    
            printArray(arrays);//原数组结果 :1  2  3  4  5  
            System.out.println("");
            System.out.println("========================");
    
            int[] reverse = reverse(arrays);//数组作返回值
    
            printArray(reverse);//结果 5 4 3 2 1
        }
    
        //打印数组元素
        public static void printArray(int[] arrays){
            for (int i = 0; i <arrays.length ; i++) {
                System.out.print(arrays[i]+"	");
            }
        }
    
        //反转数组
        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];
                result[j] = arrays[i];
            }
            
            
            return result;//数组作返回值
        }
    
    }
    
  • 相关阅读:
    诸葛马前课andoid app 应用
    C#读写文件总结
    C#写的较完美验证码通用类
    利用C#转换图片格式及转换为ico
    集合&gt;哈希表类Hashtable和SortedList排序列表类
    C# 4.0 新特性dynamic、可选参数、命名参数等
    String.Format格式说明
    C# 4动态编程新特性与DLR剖析
    C#中const和readonly的区别
    2014年7月阅读链接
  • 原文地址:https://www.cnblogs.com/Caesar-spike/p/14651912.html
Copyright © 2011-2022 走看看