编辑:这个问题似乎比我想要的更难以回答。但有人设法搞清楚了。
我从Deque课程中得到一个NullPointerException,我想知道是否有人可以告诉我这里发生了什么?这项任务和原则对我来说相当新鲜,所以我很难弄清楚我是否意外地错误地解释了其中一个概念。
尽快添加更多信息:
Netbeans上的输出:
Exception in thread "main" java.lang.NullPointerException
at deque.Deque.insertRight(Deque.java:46)
at deque.DequeTester.main(DequeTester.java:25)
Java Result: 1
此外," myList"中的每个DoublyLinkedList;是一个空值。全部十个。
主要:
public class DequeTester
{
public static void main(String[] args) {
// Declare and instantiate a new list-based Deque
Deque myDeque = new Deque(10);
// See Assignment 3 requirements for setting up the test
long iter = 1;
for(int i = 0; i < 10; i++)
{
if(i % 2 == 1)
{
myDeque.insertLeft(iter);
}
else
{
myDeque.insertRight(iter); //exception from here to Deque class
}
iter++;
}
//additional code
}
}
这是我必须填写的Deque课程:
public class Deque
{
// TO-DO: Instance variable(s) go here
private int left;
private int right;
private DoublyLinkedList[] myList;
private int maxSize;
private int numElems;
//--------------------------------------------------------------
public Deque(int size) // constructor
{
// TO-DO
numElems = 0;
maxSize = size;
myList = new DoublyLinkedList[maxSize];
left = (maxSize / 2) + 1;
right = maxSize / 2;
}
//--------------------------------------------------------------
public void insertLeft(long j) // put item at left of deque
{
// TO-DO
if(left == 0)
{
left = maxSize + 1;
}
left--;
myList[left].insertFirst(j);
numElems++;
}
//--------------------------------------------------------------
public void insertRight(long j) // put item at right of deque
{
// TO-DO
if(right == maxSize)
{
right = -1;
}
right++;
myList[right].insertLast(j); //Exception is thrown on this line.
numElems++;
}
//--------------------------------------------------------------
public long removeLeft() // take item from left of deque
{
// TO-DO
long temp;
Link linkTemp = myList[left++].deleteLast();
temp = linkTemp.dData;
if(left == 0)
{
left = maxSize;
}
numElems--;
return temp;
}
//--------------------------------------------------------------
public long removeRight() // take item from right of deque
{
// TO-DO
long temp;
Link linkTemp = myList[right--].deleteFirst();
temp = linkTemp.dData;
if(right == maxSize)
{
right = 0;
}
numElems--;
return temp;
}
//--------------------------------------------------------------
public boolean isEmpty() // true if deque is empty
{
// TO-DO
return numElems == 0;
}
//--------------------------------------------------------------
public void display()
{
// TO-DO
for(int i = 0; i < numElems; i++)
{
myList[i].displayForward();
}
if(numElems != 0)
{
if(right < left)
{
for(int i = right; i < left; i++)
{
myList[i].displayForward();
}
}
else
{
for(int i = left; i < numElems; i++)
{
myList[i].displayForward();
}
for(int i = 0; i < right; i++)
{
myList[i].displayForward();
}
}
}
}
//--------------------------------------------------------------
}
这是为赋值提供的DoublyLinkedList类:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package deque;
public class DoublyLinkedList
{
private Link first; // ref to first item
private Link last; // ref to last item
// -------------------------------------------------------------
public DoublyLinkedList() // constructor
{
first = null; //was null // no items on list yet
last = null; //was null
}
// -------------------------------------------------------------
public boolean isEmpty() // true if no links
{ return first==null; }
// -------------------------------------------------------------
public void insertFirst(long dd) // insert at front of list
{
Link newLink = new Link(dd); // make new link
if( isEmpty() ) // if empty list,
last = newLink; // newLink <-- last
else
first.previous = newLink; // newLink <-- old first
newLink.next = first; // newLink --> old first
first = newLink; // first --> newLink
}
// -------------------------------------------------------------
public void insertLast(long dd) // insert at end of list
{
Link newLink = new Link(dd); // make new link
if( isEmpty() ) // if empty list,
first = newLink; // first --> newLink
else
{
last.next = newLink; // old last --> newLink
newLink.previous = last; // old last <-- newLink
}
last = newLink; // newLink <-- last
}
// -------------------------------------------------------------
public Link deleteFirst() // delete first link
{ // (assumes non-empty list)
Link temp = first;
if(first.next == null) // if only one item
last = null; // null <-- last
else
first.next.previous = null; // null <-- old next
first = first.next; // first --> old next
return temp;
}
// -------------------------------------------------------------
public Link deleteLast() // delete last link
{ // (assumes non-empty list)
Link temp = last;
if(first.next == null) // if only one item
first = null; // first --> null
else
last.previous.next = null; // old previous --> null
last = last.previous; // old previous <-- last
return temp;
}
// -------------------------------------------------------------
public void displayForward()
{
System.out.print("List (first-->last): ");
Link current = first; // start at beginning
while(current != null) // until end of list,
{
current.displayLink(); // display data
current = current.next; // move to next link
}
System.out.println("");
}
// -------------------------------------------------------------
}
以及提供的Link类,如果您需要它:
public class Link
{
public long dData; // data item
public Link next; // next link in list
public Link previous; // previous link in list
// -------------------------------------------------------------
public Link(long d) // constructor
{ dData = d; }
// -------------------------------------------------------------
public void displayLink() // display this link
{ System.out.print(dData + " "); }
// -------------------------------------------------------------
}
答案 0 :(得分:2)
我认为你的问题始于:
private DoublyLinkedList[] myList;
...
myList = new DoublyLinkedList[maxSize];
你在这里做的是声明一组maxSize
DoublyLinkedList
s,而你想要的只是一个DoublyLinkedList
,你可以将deque的元素存储到其中。