JAVA---数组反转

package lll;

public class Test {
	public static void main(String[] args) {
		int[] arr1=new int[]{1,2,3,4,5,6};
		int temp=0;
		//方法一
		for(int i=0;i<arr1.length/2;i++){
			temp=arr1[i];
			arr1[i]=arr1[arr1.length-i-1];
			arr1[arr1.length-i-1]=temp;
		}
		for(int i=0;i<arr1.length;i++){
			System.out.print(arr1[i]+" ");
		}
		System.out.println();
		//方法二
		int[] arr2=new int[]{6,7,8,9,10};
		for(int i=0,j=arr2.length-1;i<j;i++,j--){
			temp=arr2[i];
			arr2[i]=arr2[j];
			arr2[j]=temp;
		}
		for(int i=0;i<arr2.length;i++){
			System.out.print(arr2[i]+" ");
		}
	}
}

posted @ 2022-01-05 17:32  ice--cream  阅读(45)  评论(0编辑  收藏  举报