数组的使用

1.普通的for循环

2.for-each循环(增强for循环)

3.数组做方法入参

4.数组做返回值

package com.wuming.struct;

public class Demo11 {
    public static void main(String[] args) {
        int[] arrays={1,2,3,4,5};
        //打印全部数组元素
        for (int i = 0; i < arrays.length; i++) {
            System.out.println(arrays[i]);
        }
        System.out.println("=============");
        //计算所有元素的和
        int sum=0;
        for (int i = 1; i < arrays.length; i++) {
            sum+=arrays[i];
        }
        System.out.println("sum="+sum);
        System.out.println("==============");
        //查找最大元素
        int max=arrays[0];
        for (int i = 0; i < arrays.length; i++) {
            if (arrays[i]>max){
                max=arrays[i];
            }
        }
        System.out.println("max="+max);
    }
}

1
2
3
4
5
=============
sum=14
==============
max=5

上一篇:数组之Arrays工具类和算法


下一篇:【RS】Factorization Meets the Neighborhood: a Multifaceted Collaborative Filtering Model - 当因式分解遇上邻域:多层面协同过滤模型