swapEnds challenge

Hi,

I am working on belw challenge
http://codingbat.com/prob/p118044
i wrote as below and passed all tests.

public int[] swapEnds(int[] nums) {
int len=nums.length;

  int[] newNums=new int[len];
  for(int i=0;i<len;i++){
  
  newNums[i]=nums[i];
  
  }
  
 newNums[0]= nums[len-1];
 newNums[len-1]=nums[0];
 return newNums;
}

Open in new window


How to improve my code and alternate ways of ding it. Please advise
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
int t=nums[0];
  nums[0]=nums[nums.length-1];
  nums[nums.length-1]=t;
  return nums;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gurpsbassiCommented:
your code seems fine to me.
0
ozoCommented:
it would be sufficient to loop for(int i=1;i<len-1;i++){
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

gudii9Author Commented:
int t=nums[0];
 nums[0]=nums[nums.length-1];
  nums[nums.length-1]=t;
return nums;

instead of above can we write like below without t?


  nums[0]=nums[nums.length-1];
  nums[nums.length-1]=nums[0];
 return nums;
0
gudii9Author Commented:
i think i got it. Since you are putting last in first so first needs to be saved somewhere to use later
0
gudii9Author Commented:
it would be sufficient to loop for(int i=1;i<len-1;i++){

this i will fix next time
0
ozoCommented:
instead of above can we write like below without t?


  nums[0]=nums[nums.length-1];
  nums[nums.length-1]=nums[0];
 return nums;
No,  nums[0] would have the same value as nums[nums.length-1];
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.