算法 (1)

一、数组

颠倒数组元素的顺序:

package algorithm;

/**
 * 颠倒数组元素的顺序
 */
public class ReverseArray {

    public static void main(String[] args) {
        int array[] = {4, 3, 1, 9, 8};
        reverseArray(array);
        for(int a : array){
            System.out.print(a + " ");
        }
    }

    public static void reverseArray(int a[]){
        int N = a.length;
        for(int i=0; i<N/2; i++){
            int temp = a[i];
            a[i] = a[N-1-i];
            a[N-1-i] = temp;
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/tenWood/p/10026472.html