Two ways to reverse a one-dimensional array

(Many methods)

1. Use a new array to receive the inverted array, double loop, the number of loops is the array.length

int[] arr6= {
    
    1,7,9,11,13,15,17,19};
//双层循环
int[] arr7=new int[arr6.length];
for (int i = 0; i < arr6.length; i++) {
    
    
	for (int k = arr6.length-1-i; k >=0; k--) {
    
    
				arr7[i]=arr6[k];
				break;//一次交换就退出
	}
}

2. Use the original array, exchange symmetrically, and the number of loops is the array.length/2

for (int i = 0; i < arr6.length/2; i++) {
    
    
     int tem=arr6[i];
     arr6[i]=arr6[arr6.length-1-i];
     arr6[arr6.length-1-i]=tem;
}

Guess you like

Origin blog.csdn.net/ExceptionCoder/article/details/107108831