Queue over resizable array implementation I study data structures on coursera's course, and there is an extra exercise to create a Queue data structure.

I created it:

class Queue {

  Integer[] data;
  int head, tail;

  public Queue() {
    data = new Integer[2];
    head = 0;
    tail = 0;
  }

  public int size() {
    return tail - head;
  }

  public boolean isEmpty() {
    return size() == 0;
  }

  private void realign() {
    java.util.Arrays.sort(data, new java.util.Comparator&lt;Object&gt;() {
      public int compare(Object o1, Object o2) {
        if (o1 == null) return 1;
        else if(o2 == null) return -1;
        else return 0;
      }
    });
    tail -= head;
    head = 0;
  }

  private void resize() {
    if (tail == data.length &amp;&amp; size() != data.length) {
      realign();
    }
    if (data.length == size()) {
      int newLength = data.length * 2;
      //Duplication
      Integer[] newData = new Integer[newLength];
      System.arraycopy(data, 0, newData, 0, data.length);
      data = newData;
    } else if (size() == data.length / 4 &amp;&amp; size() != 0) {
      int newLength = data.length / 2;
      //Duplication
      Integer[] newData = new Integer[newLength];
      System.arraycopy(data, 0, newData, 0, data.length);
      data = newData;
    }
  }

  public void enqueue(Integer item) {
    if (item == null) { throw new NullPointerException(); }
    resize();
    data[tail] = item;
    tail++;
    return;
  }

  public int dequeue() {
    if (isEmpty()) { throw new java.util.NoSuchElementException(); }
    int res = data[head];
    data[head] = null;
    head++;   
    if (head == tail) {
      head = tail = 0;
    }
    return res;
  }
}


And a test suit for it:

import org.junit.Test;
import static org.junit.Assert.*;
import java.util.*;

public class QueueTest {


  @Test
  public void testQueueN() {
    Queue q = new Queue();
    int N = 65;
    for(int i = 0; i &lt; N; i++) {
      q.enqueue(i);
    }
    assertFalse(q.isEmpty());
    for(int i = 0; i &lt; N; i++) {
      assertEquals(i, q.dequeue());
      assertEquals(N - i -1, q.size());
    }
    assertTrue(q.isEmpty());
  }

  @Test
  public void testEnDeEn() {
    Queue q = new Queue();
    q.enqueue(2);
    assertEquals(1, q.size());
    assertEquals(2, q.dequeue());
    q.enqueue(5);
    assertEquals(5, q.dequeue());
  }

  @Test(expected=java.util.NoSuchElementException.class)
  public void testDeOnEmpty() {
    Queue q = new Queue();
    q.dequeue();
  }
  @Test(expected=NullPointerException.class)
  public void testEnWithNull() {
    Queue q = new Queue();
    q.enqueue(null);
  }
}


Please, review it from perspective of implementation and performance.

Update: I improved(thanks to @Vogel612) this code a little bit. Improved version
Imports:

import java.util.*;


NEVER do this. Look how many classes you got now. This is ineffective and needlessly clutters your IDE's &quot;do what I want button&quot;-suggestions.
The fun thing is, in your main class you do it the other way round:

java.util.Arrays.sort(data, new java.util.Comparator&lt;Object&gt;() {


why not:
import java.util.Arrays;
import java.util.Comparator;
import java.util.NoSuchElementException;

//Later:
Arrays.sort(data, new Comparator&lt;Object&gt;()) {

This is shorter, does exactly the same and allows your ClassLoader to preload the classes needed --&gt; possible performance improvements
Queue&lt;E&gt; and Scoping
While we're in the java.util package.. You can find an interface there.. Queue&lt;E&gt;. This is something you can program against. To prevent a name-conflict I'd rename your implementation to MyQueue or something similar.
You class would then begin with the following:
public class MyQueue implements Queue&lt;Integer&gt; {
    private Integer[] data;
    private int head;
    private int tail;

Here I also made some changes to your code.
Why? In short: You should restrict the visibility of members as much as possible. With your code, classes in the same package as your Queue could do:
Queue queue = new Queue(); 
queue.data[0] = Integer.MAX_VALUE;

This is not good. noone should be messing with the state of your class. That's why these members should be private (or maximally protected), but definitely not package-private which is the default scope.
same goes for your head and tail.