数组遍历:
public class HelloWord {
public static void main(String[] args) {
int[]arr={1,2,3,4,5};
System.out.print("[ ");
for(int i=0;i<arr.length;i++){
if(i==arr.length-1) System.out.println(arr[i]+" ]");
else System.out.print(arr[i]+", ");
}
}
}
数组求最大值:
public class HelloWord {
public static void main(String[] args) {
int[]arr={1,2,3,4,5};
int max=arr[0];
for(int i=1;i<arr.length;i++){
if(max<arr[i])max=arr[i];
}
System.out.println(max);
}
}
数组逆序:
1.
ublic class HelloWord {
public static void main(String[] args) {
int[] arr={1,2,3,4,5};
for(int i=0;i<arr.length/2;i++){
int temp;
temp=arr[i];
arr[i]=arr[arr.length-1-i];
arr[arr.length-1-i]=temp;
}
for (int i=0;i<arr.length;i++)
System.out.print(arr[i]+" ");
}
}
2.
public class HelloWord {
public static void main(String[] args) {
int[] arr={1,2,3,4,5};
daoxu(arr);
}
public static void daoxu(int[]arr) {
for(int i=0;i<arr.length/2;i++){
int temp;
temp=arr[i];
arr[i]=arr[arr.length-1-i];
arr[arr.length-1-i]=temp;
}
for (int i=0;i<arr.length;i++)
System.out.print(arr[i]+" ");
}
}
原文:https://www.cnblogs.com/gongzizhubing/p/15313925.html