makeLast challenge

gudii9
gudii9 used Ask the Experts™
on
Hi,

I am trying


http://codingbat.com/prob/p137188

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

int len= nums.length;
int lenNew=2*len;


int numsNew[]=new int[lenNew];
numsNew[lenNew-1]=nums[len-1];

for(int i=0;i<lenNew-1;i++)
{
numsNew[i]=0;
}
return numsNew;
  
}

Open in new window


I passed alll the tests. How do i improve above code. please advise
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2014
Top Expert 2015
Commented:
int len= nums.length;
  int[] numsNew=new int[2*len];
  numsNew[2*len-1]=nums[len-1];
  return numsNew;
Top Expert 2016
Commented:
How do i improve above code
By reading this?

Note: by default, a new int array contains all 0's.
awking00Information Technology Specialist

Commented:
Slight variation of ozo's solution (no points)
            int[] array = new int[2*nums.length];
            array[array.length -1] = nums[nums.length - 1];
            return array;

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial