创建一个数组
创建一个数组
package com.guoba.array;
public class Demo01 {
public static void main(String[] args) {
//1.声明一个数组
int[] nums;
//2.创建一个数组
nums = new int[10];
//3.给数组中的元素赋值
nums[0] = 1;
nums[1] = 2;
nums[2] = 3;
nums[3] = 4;
nums[4] = 5;
nums[5] = 6;
nums[6] = 7;
nums[7] = 8;
nums[8] = 9;
nums[9] = 10;
//计算所有元素的和
int sum = 0;
//获取数组长度:arrays.length
for (int i = 0; i < nums.length; i++) {
sum = sum + nums[i];
}
System.out.println("数组的总和为:"+sum);
}
}