SHARE
TWEET

shift array

DulcetAirman Jun 13th, 2018 (edited) 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package ch.claude_martin;
  2.  
  3. import java.util.Arrays;
  4.  
  5. public class SomeClass {
  6.   public static void main(final String[] args) {
  7.     final int TARGET = 0;
  8.     final int[] numbers = { 1, 4, 7, 0, 8, 6, 2, 0, 1, 0, 5 };
  9.     System.out.println(Arrays.toString(numbers));
  10.     var c = 0;
  11.     for (var i = 0; i < numbers.length; i++) {
  12.       if (TARGET == numbers[i])
  13.         c++;
  14.       else if (0 < c) {
  15.         numbers[i - c] = numbers[i];
  16.         numbers[i] = TARGET;
  17.       }
  18.     }
  19.     System.out.println(Arrays.toString(numbers));
  20.   }
  21. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top