## java数组练习一（1、找数组中的最大元素；2、求数组元素平均值；3、数组逆序；4、数组拷贝）

#### 找数组中的最大元素

``````public class Test {
public static void main(String[] args) {
int[] arr={1,2,3,4,5,6,7,8,9};
System.out.println(Max(arr));
}
public static int Max(int[] a){
int max=a[0];
for(int i=0;i<a.length;i++){
if(a[i]>max){
max=a[i];
}
}
return max;
}
}
``````

#### 求数组中元素的平均值

``````public class Test {
public static void main(String[] args) {
int[] arr={1,2,3,4,5,6,7,8,9};
System.out.println(average(arr));
}
public static double average(int[] a) {
double sum=0;
for (int i = 0; i < a.length; i++) {
sum+=a[i];
}
return sum/a.length;
}
}
``````

#### 数组逆序

``````import java.util.Arrays;
public class Test {
public static void main(String[] args) {
int[] arr={1,2,3,4,5,6,7,8,9};
reverse(arr);
System.out.println(Arrays.toString(arr));
}
public static int[] reverse(int[] a){
int left=0;
int right=a.length-1;
while(left<right){
int temp=a[left];
a[left]=a[right];
a[right]=temp;
left++;
right--;
}
return a;
}
}
``````

#### 数组拷贝

``````import java.util.Arrays;
public class Test {
public static void main(String[] args) {
int[] arr={1,2,3,4,5,6,7,8,9};
int[] newarr=Arrays.copyOf(arr,arr.length);
//int[] newarr=Arrays.copyOfRange(arr,2,6);
System.out.println(Arrays.toString(newarr));
}
}
``````