2

I have an array similar to that:

[null, null, 3, 4, null]

I want to change it to array similar to that:

[3, 4, null, null, null]

I know how to do it manually, but it looks ugly. Is there any built-in(with minimum custom code) way to do that? (maybe something with System.arraycopy)

I finished with something like this(thanks @Braj for idea):

@Test
public void test() {
  Integer[] k = new Integer[]{null, null, 3, 2, 1};
  k[1] = null;
  Arrays.sort(k, new Comparator<Object>() {
    public int compare(Object o1, Object o2) {
      System.out.println(o1);
      if (o1 == null) return 1;
      else if(o2 == null) return -1;
      else return 0;
    }
  });
  assertArrayEquals(
    new Integer[]{3, 2, 1, null, null},
    k
  );
}

If somebody interested there is a bigger problem which tried to solve with this code

Community
  • 1
  • 1
kharandziuk
  • 12,020
  • 17
  • 63
  • 121
  • 2
    Please include the code you have tried. –  Jul 05 '14 at 11:40
  • Do you care about the ordering of non-null elements? Should they be sorted in any specific way? Should they retain their initial order? – toniedzwiedz Jul 05 '14 at 11:41
  • Yes, they should retain their initial order – kharandziuk Jul 05 '14 at 11:43
  • 1
    1) It looks ugly --> put that ugly code in a method, and you no longer have to see that ugly code (abstraction). As for using the API, there is a `java.util.Arrays`. If it is not there, you will waste more time searching for a solution than solving it yourself – SJuan76 Jul 05 '14 at 11:43
  • You can take a look at Guava and its [`nullsLast`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/Ordering.html) ordering. I'm not sure you can use it without sorting the collection in any way at the same time so it may not be that concise a solution. – toniedzwiedz Jul 05 '14 at 11:48
  • If somebody interested there is a [bigger problem](http://codereview.stackexchange.com/questions/56193/queue-over-resizable-array-implementation) which I tried to solve with this code – kharandziuk Jul 05 '14 at 15:05

2 Answers2

6

Use Arrays.sort(T[],Comparator) and pass Comparator to sort based on your own logic.

Read more here and find sample code as well


Here is the sample code that sorts the array as well while pushing null values in the end.

Note: (change it as per your need, not fully tested)

    final Integer[] data = new Integer[] { null, null, 3, 4, null };
    Arrays.sort(data, new Comparator<Integer>() {
        public int compare(Integer o1, Integer o2) {
            if(o1 != null && o2!=null){
                return o1.compareTo(o2);
            }else if(o2 != null){
                return o2;
            }else{                  
                return Integer.MIN_VALUE; // null values in the end
            }
        }
    });

    System.out.println(Arrays.toString(data));

output:

   [3, 4, null, null, null]

EDIT

If you are interested only in moving the null values in the end then find sample code in the below post that is answered by @Dmitry Ginzburg

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
2

The sort algo from Java standard library is stable, so, if we're making the elements, which are not null the same (in the context of ordering), it would keep the order:

Arrays.sort(data, new Comparator<Integer>() {
    public int compare (Integer x1, Integer x2) {
        if (x1 == null && x2 != null)
            return 1;
        else if (x1 != null && x2 == null)
            return -1;
        else
            return 0;
    }
});
Dmitry Ginzburg
  • 7,391
  • 2
  • 37
  • 48
  • Incorrect result. I tested your code for input `[null, null, 3, 4, 2, null]` and output `[3, 4, 2, null, null, null]` – Braj Jul 05 '14 at 12:29
  • Your code is correct, but Braj provided an idea earlier. Thanks! – kharandziuk Jul 05 '14 at 12:38
  • @DmitryGinzburg Sorry I have taken it in wrong way. I was thinking from sorting perspective as well. As per sorting logic it should be []2, 3, 4, null, null, null] but I was wrong. I think OP just want to move the items and not interested in sorting. – Braj Jul 05 '14 at 19:31
  • @Braj, yep, it's really what OP requesting. – Dmitry Ginzburg Jul 06 '14 at 08:12