Java, recursively reverse an array

后端 未结 13 1131
悲&欢浪女
悲&欢浪女 2020-12-17 16:44

I haven\'t found anything with the specific needs of my function to do this, yes, it is for homework.

So I have:

public void reverseArray(int[] x) {
         


        
13条回答
  •  心在旅途
    2020-12-17 17:24

    Since there's been no statement that loops cannot be used:

    void reverseArray(int[] x) {
        if (x != null) {
            for (int i = 0; i < length.x / 2; i++) {
                int j = (length.x - 1) - i;
                int temp = x[i];
                x[i] = x[j];
                x[j] = temp;
             }
             reverseArray(null);
         }
    }
    

    Probably the fastest of the lot.

提交回复
热议问题