package com.zhang.array;
public class ArrayDemo01 {
//变量的类型 变量的名字 = 变量的值;
//数组类型
public static void main(String[] args) {
int[] nums; //1.声明一个数组
//int nums2[]; //c和c++
nums =new int[10];//这里面可以放10个int类型的数字
int[] num2 =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;
for (int i=0;i<nums.length;i++){
sum = sum +nums[i];
}
System.out.println(sum);
}
}
package com.zhang.array;
public class ArrayDemo02 {
public static void main(String[] args) {
//静态初始化: 创建 + 赋值
int[] a = {1,2,3,4,5,6,7,8};
//引用类型
Man[] mans = {new Man(),new Man()};
System.out.println(a[0]);
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
//动态初始化 : 包含默认初始化
int[] b = new int[10];
b[0]=10;
System.out.println(b[0]);
System.out.println(b[1]);
System.out.println(b[2]);
System.out.println(b[3]);
}
}
package com.zhang.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.println(arrays[i]);
}
System.out.println("=======================");
//计算所有元素的和
int sum =0;
for (int i = 0; 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);
}
}
package com.zhang.array;标签:Day09,nums,int,System,arrays,println,out From: https://www.cnblogs.com/fenghuahua/p/17306772.html
public class ArrayDemo04 {
public static void main(String[] args) {
int[ ] arrays={1,2,3,4,5};
//JDK1.5, 没有下标
for (int array : arrays) {
System.out.println(array);
}
//printArray(arrays);
int[] reverse = reverse(arrays);
printArray(reverse);
}
//反转数组
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];
}
return result;
}
//打印数组元素
public static void printArray(int[] arrays){
for (int i = 0; i < arrays.length; i++) {
System.out.println(arrays[i]+"");
}
}
}