1
    import java.awt.HeadlessException;
    import java.util.Iterator;
    import java.util.NoSuchElementException;

    import javax.xml.soap.Node;

    public class Deque<Item> implements Iterable<Item> {

    private int size;
    private Node<Item> head;
    private Node<Item> tail;

    private static class Node<Item> {
        private Item key;
        private Node<Item> leftNode;
        private Node<Item> rightNode;

        Node(Item key, Node<Item> left, Node<Item> right) {
            this.key = key;
            this.leftNode = left;
            this.rightNode = right;
        }

    }


    public Deque() {
        head = new Node<Item>(null,null,null);
        tail = new Node<Item>(null,head,null);
        head.rightNode = tail;
        size = 0;
    }
    public boolean isEmpty() {
        return head.rightNode == tail;
     }
    public int size() {
       return size;
    }
    public void addFirst(Item item) {

       if (item == null) throw new NullPointerException(" trying to add nothing");

       else {
           Node<Item> oldFirstNode = head.rightNode;
           head.rightNode = new Node<Item>(item,head,oldFirstNode);
           oldFirstNode.leftNode = head.rightNode;
           size++;

       }
     }
    public void addLast(Item item)  {

       if (item == null) throw new NullPointerException(" trying to add nothing");

       else {
           Node<Item> oldLastNode = tail.leftNode;
           tail.leftNode = new Node<Item>(item,oldLastNode,tail);
           oldLastNode.rightNode = tail.leftNode;
           size++;

       }
   }

     public Item removeFirst()  {              // remove and return the item from the front

       if (isEmpty()) throw new NoSuchElementException(" Deque is alrady empty");

       else {
           Node<Item> delete = head.rightNode;
           Node<Item> newFirstNode = head.rightNode.rightNode;
           newFirstNode.leftNode = head;
           head.rightNode = newFirstNode;
           delete.leftNode = null;
           delete.rightNode = null;
           Item myKeyItem = delete.key;
           delete = null;
           size--;
           return myKeyItem;
       }

   }
   public Item removeLast() {                 // remove and return the item from the end

       if (isEmpty()) throw new NoSuchElementException(" Deque is alrady empty");

       else {
           Node<Item> delete = tail.leftNode;
           Node<Item> newLastNode = tail.leftNode.leftNode;
           newLastNode.rightNode = tail;
           tail.leftNode = newLastNode;
           delete.leftNode = null;
           delete.rightNode = null;
           Item myKeyItem = delete.key;
           delete = null;
           size--;
           return myKeyItem;
       }
   }


   public Iterator<Item> iterator() {         // return an iterator over items in order from front to end
       return new DequeIterator();
   }

   private class DequeIterator implements Iterator<Item> {

       private Node<Item> current = head.rightNode;

       public boolean hasNext() {return current != tail;}

       public void remove() { throw new UnsupportedOperationException("not yet");  }

       public Item next() {
           if (!hasNext()) throw new NoSuchElementException("no next");
           Item key = current.key;
           current = current.rightNode;
           return key;

       }
   }
     public static void main(String[] args) { 

   Deque<Integer>[] s = new Deque[10];
   for (int i = 0; i < 10; i++) {
   s[i] =new Deque<Integer>(); // initial each object
   }
   s[0].addFirst(1);
  // s[1].addFirst(2);
   StdOut.print(s[0]);
   //StdOut.print(s[1]);

  } 


   } 

In the following code I have a compiling error stats that "Cannot instantiate the type Deque".

package Assign3;

 import java.util.Deque;



 public class graph {

//private Deque<Integer>[] graphRepDeques;
final static int Amount = 200;

private Deque<Integer>[] s = new Deque[Amount];

public graph(String filename) {

    In in = new In(filename);
    //final int Amount = 200;
    s = new Deque[Amount];

    for (int i = 0; i < Amount; i++) {

        String[] currentLine = in.readLine().split("\\s");
        int size = currentLine.length;
         s[i] =new Deque<Integer>(); // why Cannot instantiate the type Deque<Integer>???????
        for (int j = 1; j < size; j++) {
            int temp = Integer.parseInt(currentLine[j]);
            s[i].add(temp);
        }
    }


}

private  void show() {

    for (int i = 0; i < Amount; i++) {
           for (int a: s[i]) {
               //StdOut.print(a + " ");
           }
    }

}

public static void main(String[] args) {
    graph a = new graph("kargerMinCut.txt"); //whatever text you like with integers
    a.show();
}

}

The error I got is

Cannot instantiate the type Deque<Integer>

I can instantiate Deque in the main function in class "Deque", but why I cannot do it in class "graph"? Very confused here

I appreciate any help thanks.

The goal of my code is to read the numbers in a file line by line and for each line, I store the numbers I got into a object of class Deque. Therefore, I need an array of objects of Deque to store all the numbers. Similary to an array of linkedlist (Deque in my code).

The problem is I do not know how to initialized an array of this linkedlist (Deque in my code) properly so I can "push" all the keys in each linkedlist (Deque)

peng zhong
  • 13
  • 1
  • 6
  • You've given a *lot* of code here, but not actually said what the problem is. It would be easier to help you if you'd provide a *short* but complete program demonstrating the problem. – Jon Skeet Aug 10 '15 at 05:55
  • Thank you for your advise and I have made it short and explained the problem I received – peng zhong Aug 10 '15 at 06:15
  • 1
    @pengzhong, a `NullPointerException` wont be thrown at the place you specified with the code you have. That's why you no one is answering. Also jonskeet asked for a _complete_ example too. If you breakup into many small chunks, it's difficult to debug. Create the shortest possible program with can reproduce the problem and post the whole program. – Codebender Aug 10 '15 at 06:18
  • 3
    Please read http://stackoverflow.com/questions/218384 – Jon Skeet Aug 10 '15 at 06:20

2 Answers2

1

In your edited code, the problem is that you are just initializing the array. Not the Deque objects.

Deque<Integer>[] s = new Deque[10];

The above just creates an Array of 10 elements (and initializes all of them to null).

Hence s[0] is null, just like any other index. And you are trying to call addFirst(1) on null element.

So you should be initalizing each element in the array before using them,

s[i] = new Deque(); // In a loop if needed.
Codebender
  • 14,221
  • 7
  • 48
  • 85
  • Thank you so much for your help. the logic of "initializes all of them to null" is fairly confused but I got it thanks. – peng zhong Aug 10 '15 at 06:59
1

Since you applied Codebender's hint, you now just have to replace

s[i] = new Deque<Integer>();

with:

s[i] = new LinkedList<Integer>();

as you already mentioned in your question.


java.util.Deque is only an interface and therefore cannot be instantiated directly. All your variables (including the array) can still be of the type Deque and Deque[] respectively, since java.util.LinkedList implements that interface.

Carsten
  • 2,047
  • 1
  • 21
  • 46