java数组的静动态初始化,遍历,求和,获取最值和反转操作
class Demo01_数组的定义 {
public static void main(String[] args) {
Demo01_数组的定义 de = new Demo01_数组的定义();
int[] a = {123,234,124,432,234};
//de.a();
//de.b(a);
//de.c(a);
//de.d(a);
de.e(a);
}
//数组静态初始化
void a(){
int[] a = new int[3];
System.out.println(a.length);
a[0] = 123;
a[1] = 456;
a[2] = 789;
for (int i = 0;i<a.length;i++)
{
System.out.println(a[i]);
}
}
//数组动态初始化
void b(int[] a){
//int[] a = new int[]{132,456,798};
printArr(a);
}
//遍历操作
void printArr(int[] arr){
for (int i = 0;i<arr.length;i++){
System.out.print(arr[i]+ " ");
}
System.out.println();
}
//数组的求和
void c(int[] a){
int sum = 0;
for (int i = 0;i<a.length;i++){
System.out.print(a[i]+ " ");
sum += a[i];
}
System.out.println("\n该数组的和为:"+sum);
}
//数组中获取最值
void d(int[] a){
int max = a[0];
int x = 0;
for(int i = 0;i<a.length;i++){
System.out.print(a[i]+ " ");
if(max<a[i]){
max=a[i];
x = i;//获取索引
}
}
System.out.println("\n数组最大值为:"+"a["+x+"]:"+max);
int min = a[0];
int y = 0;
for(int i = 0;i<a.length;i++){
if(min>a[i]){
min=a[i];
y = i;//获取索引
}
}
System.out.println("数组最小值为:"+"a["+y+"]:"+min);
}
//数组的反转
void e(int[] a){
printArr(a);
for(int i=0,j=a.length-1;i<j;i++,j--){
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
printArr(a);
}
}
遍历
求和
获取最值
反转