Created
December 13, 2016 21:49
-
-
Save robertbenjamin/0837699b53f54e794bbcecb752e56992 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class removeZeros { | |
public static void main(String[] args) { | |
int[] list = {7, 2, 3, 0, 4, 6, 0, 0, 13, 0, 78, 0, 0, 19, 14}; | |
removeZeroes(list); | |
System.out.println(Arrays.toString(list)); | |
} | |
public static void removeZeroes(int[] list) { | |
for (int i = 0; i < list.length - 1; i++) { | |
if (list[i] == 0) { | |
int num = i; | |
while (list[num] == 0 && num < list.length - 1) { | |
num++; | |
} | |
list[i] = list[num]; | |
list[num] = 0; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment