0

I am trying to write a method that receives an array of numbers. If there are any zeros in the array it will add another zero, but the array must remain the same length so the last number is deleted from the new array. Here is what I have started to do, but I don't think it is going anywhere.

public static int[] MoveToRightOne(int userArray[] ) 
{ 
    int newArray [] = new int[userArray.length + 1]; 
    int zero = 0; 
    for(int i = 0; i < userArray.length; i++) 
    { 
        if (userArray[i] == 0) 
            zero = zero + 1; 
        newArray[i + zero] = userArray[i - 1]; 
    } 

    return(userArray); 
}
Adam Smith
  • 659
  • 6
  • 15
Bill
  • 25
  • 2
  • 9
  • 1
    Advice: tell us what language you're using? – John Saunders Oct 28 '14 at 02:02
  • Comments! Please add comments describing what each part of your code *intends* to do. – Scott Hunter Oct 28 '14 at 02:08
  • I don't quite understand the goal of the method. Is it if there are _any_ zeros it just replaces the last element of the array with a zero? Or is it for _every_ zero in the array it adds a zero? If so then what index of the array are they added to? – Adam Smith Oct 28 '14 at 02:09
  • related: http://stackoverflow.com/questions/7970857/shifting-elements-in-an-array – andrew Oct 28 '14 at 02:11
  • It adds a zero after every instance of zero in the array. So to maintain the original length of the array, it deletes integers from the end. Here is an example input and output: "1 2 0 3 0 4 0 5 0 6 6 0 0" "1 2 0 0 3 0 0 4 0 0 5 0 0" – Bill Oct 28 '14 at 02:13
  • You are returning userArray and not newArray – Abhijeet Kushe Oct 28 '14 at 02:23

2 Answers2

0

I think this will do what you want

public static int[] MoveToRightOne(int userArray[] ) {
    // the new array will have the same length as the input 
    int newArray [] = new int[userArray.length]; 
    // two indexes i for the input and j for the output
    int i = 0, j = 0;
    while(j < userArray.length){ // while it's not the end of the output
        // we insert the element
        newArray[j] = userArray[i];
        if(userArray[i] == 0){ // if the current element is a 0
            // we insert an additional 0
            j ++;
            if(j < userArray.length)
                newArray[j] = 0;
        }
        // increment indexes
        i ++;
        j ++;
    }
    return newArray; 
}
webNeat
  • 2,768
  • 1
  • 20
  • 22
0

The following code will serve your purpose

public static int[] MoveToRightOne(int userArray[] ) { 
    int newArray [] = new int[userArray.length];
    for(int i = 0, j = 0;j < userArray.length;i++,j++){
        newArray[j] = userArray[i];
        if(userArray[i] == 0 && j+1 < userArray.length){
            j ++;
            newArray[j] = 0;
        }
    }  
    return(newArray); 
}
R A Khan
  • 187
  • 4
  • 12
  • this won't work if the `userArray[i] == 0` and `j == userArray.length - 1`; it will increment it and try to insert at `newArray[userArray.length]` which is out of bounds ! – webNeat Oct 28 '14 at 02:32