SHARE
TWEET

shift array 2

DulcetAirman Jun 13th, 2018 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package ch.fhnw.claudemartin;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Comparator;
  5.  
  6. public class SomeClass {
  7.  
  8.   public static void main(final String... args) {
  9.     final int TARGET = 0;
  10.     final int[] numbers = { 1, 4, 7, 0, 8, 6, 2, 0, 1, 0, 5 };
  11.     System.out.println(Arrays.toString(numbers));
  12.     final Integer[] objects = Arrays.stream(numbers).<Integer> mapToObj(i -> i)
  13.         .toArray(Integer[]::new);
  14.  
  15.     // stream api
  16.     final Comparator<? super Integer> comparator = //
  17.         (a, b) -> a == b ? 0 : TARGET == a ? 1 : TARGET == b ? -1 : 0;
  18.     final int[] result = Arrays.stream(numbers).mapToObj(i -> i)
  19.         .sorted(comparator).mapToInt(i -> i).toArray();
  20.     System.out.println(Arrays.toString(result));
  21.  
  22.     // in-place
  23.     Arrays.sort(objects, comparator);
  24.     System.out.println(Arrays.toString(objects));
  25.   }
  26. }
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top