1

I am stuck with an exercise which tells me to create a reversed array from the given one.

After some thinking I made such code:

    public int[] reverse3(int[] nums) {

  int[] nums2 = new int[3];

  for (int i = nums.length - 1; i >= 0; i--) {

     for (int j = 0; j < nums.length; j++) {

          nums2[j] = nums[i];

    }
  }
  return nums2;
}

But it is throwing out three exact same numbers.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Kite90
  • 9
  • 1

6 Answers6

2

You don't need a nested for loop - just iterate over the source array and fill the result array in the opposite order:

public int[] reverse(int[] nums) {
    int len = nums.length;
    int[] result = new int[len];
    for (int i = 0; i < len; ++i) {
        result[len - i - 1] = nums[i];
    }
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

From a first look, your code should be more like this:

public int[] reverse3(int[] nums) 
{

  // initialize a second array with the same length
  int[] nums2 = new int[nums.length];
  // initialize the nums2 index
  int index = 0;

  // you only need one loop for this (since we'll be incrementing the index of nums2)
  for (int i = nums.length - 1; i >= 0; i--) {
     nums2[index] = nums[i];
     index++;
  }

  return nums2;
}
Haytam
  • 4,643
  • 2
  • 20
  • 43
1

Swap the symetric values in the array like this :

 public static void reverse(int[] nums) {
    for (int i = 0; i < nums.length / 2; i++) {
        int temp = nums[i];
        nums[i] = nums[nums.length - 1 - i];
        nums[nums.length - 1 - i] = temp;
    }
Mustapha Belmokhtar
  • 1,231
  • 8
  • 21
0

To reverse an array you only have to swap the elements until the midpoint:

public int[] reverse(int[] nums) {
    int numsLength = nums.length;
    for (int i = 0; i < numsLength / 2; i++) {
        int temp = nums[i];
        nums[i] = nums[numsLength - i - 1];
        nums[numsLength - i - 1] = temp;
    }
    return nums;
}

This way is much more optimized.

Source: How do I reverse an int array in Java?

jeprubio
  • 17,312
  • 5
  • 45
  • 56
0

I am new to Java development and sorry if this question will be too silly, but it seems like I am stuck with the exercise which tells me to create a reversed array from the given one.

After some thinking I made such code:

    public int[] reverse3(int[] nums) {

  int[] nums2 = new int[3];

  for (int i = nums.length - 1; i >= 0; i--) {

     for (int j = 0; j < nums.length; j++) {

          nums2[j] = nums[i];

    }   }   return nums2; }

But it is throwing out three exact same numbers. Could I please count on some help? Thank you

Use one loop only. If you want to use 2 arrays(wich i do not see the point.) this will work:

    int j = 0;

    for(int i = nums.length -1; i >= 0; i--){
         nums2[j] = nums[i];
         j++;
     }

But if you want to use only one array, you can do this:

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

There are so many efficient ways to do it but to make you understand i am gonna modify your own code

public int[] reverse3(int[] nums) {

      int[] nums2 = new int[3];

      for (int i = nums.length - 1; i >= 0; i--) {

         for (int j = (nums.length-1) - i; j < nums.length; j++) {

              nums2[j] = nums[i];

        }
      }

      return nums2;
    }

or let's do a a little bit modification rather than using nums.length() again and again we can put it inside a variable

public int[] reverse3(int[] nums) {

      int[] nums2 = new int[3];
      int length = nums.length;
      for (int i = length - 1; i >= 0; i--) {

         for (int j = (length-1) - i; j < length; j++) {

              nums2[j] = nums[i];

        }
      }

      return nums2;
    }

Remember it is not an efficient way but to make you understand i just modify the logic. Using nested loops like that will decrease the performance so better avoid it and try to do it in much more optimized way..

Samarth Saxena
  • 1,121
  • 11
  • 18