Creating a New Reverse Java Array

Posted by mandir08 on Stack Overflow See other posts from Stack Overflow or by mandir08
Published on 2010-04-19T20:04:40Z Indexed on 2010/04/19 20:13 UTC
Read the original article Hit count: 194

Filed under:
|

Given an array of ints length 3, return a new array with the elements in reverse order, so {1, 2, 3} becomes {3, 2, 1}.

public int[] reverse3(int[] nums) {
  int[] values = new int[3];
  for(int i=0; i<=nums.length-1; i++) {
     for(int j=nums.length-1; j>=0; j--) {
   values[i]=nums[j];
  }
  }
  return values;

  }

I cant get this to work properly, usually the last int in the array, becomes every single int in the new array

© Stack Overflow or respective owner

Related posts about java

Related posts about arrays