0
点赞
收藏
分享

微信扫一扫

数组颠倒 这个代码真的厉害

前端王祖蓝 2022-03-12 阅读 43

public class 数组颠倒 {

	public static void main(String[] args) {
		int [] a = {1,2,3,4,5};//定义一个长度为5的数组
		for(int i=0;i<=(a.length-1)/2;i++){//“i<=(a.length-1)/2”//不论数组长度都可以对半颠倒
			int temp=a[i];
			a[i]=a[a.length-1-i];
			a[a.length-1-i]=temp;
			
		}
		for (int j = 0; j < a.length; j++) {
			System.out.println(a[j]);
		}		
	}

}

这个颠倒的代码真的好巧妙

举报

相关推荐

0 条评论