Java 正在尝试启动此链接列表
第二编程课 因此,我们的任务是建立一个链表,从头开始构建每个方法。 我从前天开始,出现了一个空指针异常,我想稍后我会解决它并继续。 在把我的程序删减到零以找到罪魁祸首之后,我留下的代码应该可以像从我们实验室复制的一样工作(这很有效)。 如果你们认为你们能理解为什么我的add方法会出现空指针异常,我会非常感激,看看我是否正确地执行了第二个构造函数。如果我能在这方面获得一些动力来开始,它将变得更容易,但因为是这样,我甚至不能开始 你会注意到空方法的分配,一旦我的构造函数+add方法开始工作,我就会开始使用它们 我的代码:Java 正在尝试启动此链接列表,java,Java,第二编程课 因此,我们的任务是建立一个链表,从头开始构建每个方法。 我从前天开始,出现了一个空指针异常,我想稍后我会解决它并继续。 在把我的程序删减到零以找到罪魁祸首之后,我留下的代码应该可以像从我们实验室复制的一样工作(这很有效)。 如果你们认为你们能理解为什么我的add方法会出现空指针异常,我会非常感激,看看我是否正确地执行了第二个构造函数。如果我能在这方面获得一些动力来开始,它将变得更容易,但因为是这样,我甚至不能开始 你会注意到空方法的分配,一旦我的构造函数+add方法开始工作,我就会开
import java.util.Collection;
import java.util.Iterator;
/**
* Created by hhhh on 11/2/2014.
*/
public class Lset<R> implements Set151Interface<R> {
private Node head;
private int length;
/**In the first (following) constructor im trying to re use code and call my clear method.
*Should save space and make code look cleaner.
*/
public Lset(){
clear();
}
public Lset(Collection<? extends R> list){
this();
for (R object : list) {
add(object);
}
}
/**
* Copied from Lab7, this add method checks to see if there are more nodes than just the head.
* After the check if false, creates a new node and adds it to the end of the list.
* @param entry
* @return
*/
@Override
public boolean add(R entry) {
Node newNode = new Node(entry);
// empty list is handled differently from a non-empty list
if (head.next == null) {
head = newNode;
} else {
Node lastNode = getNodeAt(length - 1);
lastNode.next = newNode;
}
length++;
return true;
}
@Override
public void clear() {
this.length = 0;
this.head = null;
}
@Override
public boolean contains(Object o) {
return false;
}
@Override
public Iterator<R> iterator() {
return null;
}
@Override
public boolean containsAll(Collection<?> c) {
return false;
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public boolean remove(Object o) {
return false;
}
@Override
public boolean addAll(Collection<? extends R> c) {
return false;
}
@Override
public boolean removeAll(Collection<?> c) {
return false;
}
@Override
public boolean retainAll(Collection<?> c) {
return false;
}
@Override
public int size() {
return length;
}
@Override
public Object[] toArray() {
return null;
}
@Override
public <T> T[] toArray(T[] array) {
return null;
}
/**
* Code used in Lab 7, getNodeAt uses the length field and starts at head to traverse array and arrive at the
* position desired.
* @param position
* @return
*/
private Node getNodeAt(int position) {
assert !isEmpty() && (position >= 0) && position < length;
Node cNode = head;
for (int i = 0; i < position; i++)
cNode = cNode.next;
assert cNode != null;
return cNode;
}
public String toString(){
String arrayString = "<";
for(int i = 0; i < length; i++){
String two = getNodeAt(i).toString();
arrayString += two;
if(i <= (length - 2)){
two = ", ";
arrayString += two;
}
}
arrayString += ">";
return arrayString;
}
//TODO comment better
public class Node {
/** Reference to the data */
public R data;
/** Reference to the next node is in the list */
public Node next;
/**
* Sets the data for this node.
* @param data data to be carried by this node.
*/
public Node(R data) {
this.data = data;
this.next = null;
}
/**
* Sets the data for the node and assigns the next node in the list.
* @param data data to be carried by this node.
* @param nextNode next node in the list.
*/
public Node(R data, Node nextNode) {
this.data = data;
this.next = nextNode;
}
/**
* Returns just the data portion of the node.
* @return The data portion of the node.
*/
public R getData() {
return this.data;
}
/**
* Modified just the data portion of the node.
* @param data new data to be contained within the node.
*/
public void setData(R data) {
this.data = data;
}
/**
* What node does this node point to.
* @return the node that this node points to or null if it does not
* point anywhere.
*/
public Node getNextNode() {
return this.next;
}
/**
* Change the node that this node points to.
* @param nextNode a new node for this node to point to.
*/
public void setNextNode(Node nextNode) {
this.next = nextNode;
}
/**
* Display the state of just the data portion of the node.
*/
public String toString() {
return this.data.toString();
}
}
}
import java.util.Collection;
导入java.util.Iterator;
/**
*由HHH于2014年11月2日创建。
*/
公共类Lset实现了set151接口{
专用节点头;
私有整数长度;
/**在第一个(以下)构造函数中,我试图重用代码并调用我的clear方法。
*应该节省空间,使代码看起来更干净。
*/
公共Lset(){
清除();
}
公共Lset(集合c){
返回false;
}
@凌驾
公共布尔值为空(){
返回false;
}
@凌驾
公共布尔删除(对象o){
返回false;
}
@凌驾
公共布尔addAll(集合c){
返回false;
}
@凌驾
公共布尔保留(集合c){
返回false;
}
@凌驾
公共整数大小(){
返回长度;
}
@凌驾
公共对象[]toArray(){
返回null;
}
@凌驾
公共T[]阵列(T[]阵列){
返回null;
}
/**
*在Lab 7中使用的代码,getNodeAt使用长度字段并从头部开始遍历数组并到达
*所需职位。
*@param位置
*@返回
*/
私有节点getNodeAt(int位置){
assert!isEmpty()&&(位置>=0)&&position
这是一种主要的方法,它正在扼杀它
private void testConstruction() {
System.out.println("\nTesting Constructor");
System.out.print("----------------------------------------");
System.out.println("----------------------------------------");
Set151Interface s = makeSet();
//added
s.add("Butterfinger");
test(s.size() == 0,
"size() should return 0: " + s.size());
test(s.toString().equals("<>"),
"toString returns \"<>\": " + s.toString());
ArrayList<String> temp = new ArrayList<String>();
temp.add("Butterfinger");
temp.add("Milky Way");
temp.add("Kit Kat");
temp.add("Three Muskateers");
Set151Interface s3 = makeSet(temp);
test(s3.size() == 4,
"size should return 4: " + s3.size());
test(s3.toString().equals("<Butterfinger, Milky Way, Kit Kat, Three Muskateers>"),
"toString should return\n "+
"\"<Butterfinger, Milky Way, Kit Kat, Three Muskateers>\":\n "
+ s3.toString());
}
private void testConstruction(){
System.out.println(“\n测试构造函数”);
System.out.print(“-------------------------------------------------”;
System.out.println(“--------------------------------------------------------”;
set151s=makeSet();
//增加
s、 添加(“Butterfinger”);
测试(s.大小()==0,
size()应返回0:“+s.size());
测试(s.toString()等于(“”),
“toString返回\“\”:“+s.toString());
ArrayList temp=新的ArrayList();
临时添加(“Butterfinger”);
临时添加(“银河”);
临时添加(“套件Kat”);
临时添加(“三个火枪手”);
SET151接口s3=生成集(临时);
测试(s3.size()=4,
“size应该返回4:”+s3.size());
test(s3.toString().equals(“”),
“toString应返回\n”+
“\”\:\n”
+s3.toString());
}
一旦butterfinger尝试添加,我就会得到指向此行的空指针异常
如果(head.next==null){
您刚刚声明了私有节点head;
并且它不接受任何赋值。因此编译器抛出NPE
谢谢大家的帮助,我找到了:)。
在第一天,我编辑了我的驱动程序(忘记了),一旦我重新复制了驱动程序,一切都正常(到目前为止),再次感谢各位!当试图构建一个完整的库时,单元测试(Junit或TestNG)是您的朋友…编译器不会抛出异常(除非编译器本身有bug)。