gpt4 book ai didi

java - 无限整数链表 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 10:13:40 25 4
gpt4 key购买 nike

我正在编写一个程序,通过将整数中的每个数字设为自己的节点来创建无限长的整数。

例如:123,456 = [1]->[2]->[3]->[4]->[5]->[6]

此外,我需要能够将两个无限整数的内容相加。

例如:[1]->[2]->[3]->[4]->[5]->[6] + [1]->[2]->[3]= [1]->[2]->[3]->[5]->[7]->[9]

但是,当我尝试反向遍历其中一个链表时,在第一次循环后出现 NullPointerException;

while((currentOne != null) && (currentTwo != null)) {
// for(int i = firstOperand.getNumberOfDigitsWithZeroes(); i>0; i--)

if(currentOne.data > currentTwo.data) {
tempResult = currentOne.data - currentTwo.data;
//result = tempResult + result;
returnResult.addToFront(tempResult);
} else if(currentOne.data < currentTwo.data) {
currentOne.previous.data -= 1;
currentOne.data += 10;
tempResult = currentOne.data - currentTwo.data;
//result = tempResult + result;
returnResult.addToFront(tempResult);
} else if((currentOne == firstOperand.firstNode && currentTwo == secondOperand.firstNode) &&
currentOne.data - currentTwo.data == 0) {
break;
} else {
returnResult.addToFront(0);
}
currentOne = currentOne.previous;
currentTwo = currentTwo.previous;
}

firstOperand是数字100000000000000000000的链表。currentOne是初始化为firstOperand的最后一个节点的节点。 currentTwo是一个初始化为第二个操作数的最后一个节点的节点,其内容为000000000000000000001。错误位于行

                        if(currentOne.data > currentTwo.data)

错误发生在循环的第二次运行之后,我不知道为什么,因为当以字符串形式打印时,第二个操作数显示完整的000000000000000000001。所以,我不确定为什么我会得到空指针异常,而不是 currentTwo.data 等于 0。

这是 InfiniteInteger 对象的构造函数

public LInfiniteInteger(String s)
{
// TO DO
int newDigit = 0;
isNegative = false;
numberOfDigits = 0;
middlePosition = 0;
firstNode = null;
middleNode = null;
lastNode =null;
for(int i =0; i<s.length(); i++)
{
newDigit = (int)s.charAt(i) - 48;
if((newDigit >= 0) && (newDigit <= 9))
{
// this.add(newDigit);
if(firstNode == null)
{
firstNode = new Node(null, newDigit, null);
lastNode = firstNode;
}
else
{
Node newNode = new Node(lastNode, newDigit, null);
lastNode.next = newNode;
lastNode = newNode;
}
numberOfDigits++;

if(numberOfDigits %2 == 1);
{
if(middleNode == null)
{
middleNode = firstNode;
}
else
{
middleNode = middleNode.next;
}
middlePosition++;
}
}
else if((newDigit == ((int)'-') - 48))
{
isNegative = true;
}
}
}

这是发生错误的完整方法:

    public InfiniteIntegerInterface minus(final InfiniteIntegerInterface anInfiniteInteger)
{
// TO DO
LInfiniteInteger firstOperand = new LInfiniteInteger(this.toString());
LInfiniteInteger secondOperand = new LInfiniteInteger(anInfiniteInteger.toString());
LInfiniteInteger returnResult = new LInfiniteInteger("");
int tempResult;
boolean resultIsNegative = false;
Node currentOne = firstOperand.lastNode;
Node currentTwo = secondOperand.lastNode;


while(firstOperand.getNumberOfDigitsWithZeroes() > secondOperand.getNumberOfDigitsWithZeroes())
{
Node temp = new Node(0);
temp.next=secondOperand.firstNode;
secondOperand.firstNode = temp;
secondOperand.addNumberOfDigits();

}
while(firstOperand.getNumberOfDigitsWithZeroes() < secondOperand.getNumberOfDigitsWithZeroes())
{
Node temp = new Node(0);
temp.next=firstOperand.firstNode;
firstOperand.firstNode = temp;
firstOperand.addNumberOfDigits();
}

if((firstOperand.isNegative == false) && (secondOperand.isNegative == false))
{
if(firstOperand.compareMag(secondOperand) == 1)
{
//algorithm
//System.out.println(currentTwo.data);
//System.out.println(secondOperand.toString());
for(int i = secondOperand.getNumberOfDigitsWithZeroes(); i > 0; i--)
{
System.out.println(currentOne.data);
currentOne = currentOne.previous;
}
currentOne = firstOperand.lastNode;
while((currentOne != null) && (currentTwo != null))
// for(int i = firstOperand.getNumberOfDigitsWithZeroes(); i>0; i--)
{
if(currentOne.data > currentTwo.data)
{
tempResult = currentOne.data - currentTwo.data;
//result = tempResult + result;
returnResult.addToFront(tempResult);
}
else if(currentOne.data < currentTwo.data)
{
currentOne.previous.data -= 1;
currentOne.data += 10;
tempResult = currentOne.data - currentTwo.data;
//result = tempResult + result;
returnResult.addToFront(tempResult);
}
else if((currentOne == firstOperand.firstNode && currentTwo == secondOperand.firstNode) && currentOne.data - currentTwo.data == 0)
{
break;
}
else
{
returnResult.addToFront(0);
}
currentOne = currentOne.previous;
currentTwo = currentTwo.previous;
}
if(currentOne == null)
{
while(currentTwo != null)
{
//result = currentTwo.data + result;
returnResult.addToFront(currentTwo.data);
currentTwo = currentTwo.previous;
}
}
else if(currentTwo == null)
{
while(currentTwo != null)
{
//result = currentTwo.data + result;
returnResult.addToFront(currentTwo.data);
currentTwo = currentTwo.previous;
}
}

return returnResult;
}
else if(firstOperand.compareMag(secondOperand) == 0)
{
returnResult.add(0);
return returnResult;
}
else
{
LInfiniteInteger tempReturnResult = new LInfiniteInteger(secondOperand.minus(firstOperand).toString());
Node currentOneInner = tempReturnResult.firstNode;
while(currentOneInner != null)
{
returnResult.add(currentOneInner.data);
currentOneInner = currentOneInner.next;
}
returnResult.isNegative = true;
return returnResult;
}
}
else if((firstOperand.isNegative == false) && (secondOperand.isNegative == true))
{
secondOperand.isNegative = false;
LInfiniteInteger tempReturnResult = new LInfiniteInteger(firstOperand.plus(secondOperand).toString());
Node currentOneInner = tempReturnResult.firstNode;
while(currentOneInner != null)
{
returnResult.add(currentOneInner.data);
currentOneInner = currentOneInner.next;
}
return returnResult;
}
else if((firstOperand.isNegative == true) && (secondOperand.isNegative == false))
{
firstOperand.isNegative = false;
LInfiniteInteger tempReturnResult = new LInfiniteInteger(firstOperand.plus(secondOperand).toString());
Node currentOneInner = tempReturnResult.firstNode;
while(currentOneInner != null)
{
returnResult.add(currentOneInner.data);
currentOneInner = currentOneInner.next;
}
returnResult.isNegative = true;
return returnResult;
}
else
{
if(firstOperand.compareMag(secondOperand) == -1)
{
firstOperand.isNegative = false;
secondOperand.isNegative = false;
LInfiniteInteger tempReturnResult = new LInfiniteInteger(firstOperand.minus(secondOperand).toString());
Node currentOneInner = tempReturnResult.firstNode;
while(currentOneInner != null)
{
returnResult.add(currentOneInner.data);
currentOneInner = currentOneInner.next;
}
return returnResult;
}
else if(firstOperand.compareMag(secondOperand) == 0)
{
returnResult.add(0);
return returnResult;
}
else
{
firstOperand.isNegative = false;
secondOperand.isNegative = false;
LInfiniteInteger tempReturnResult = new LInfiniteInteger(firstOperand.minus(secondOperand).toString());
Node currentOneInner = tempReturnResult.firstNode;
while(currentOneInner != null)
{
returnResult.add(currentOneInner.data);
currentOneInner = currentOneInner.next;
}
returnResult.isNegative = true;
return returnResult;
}
}

}

我正在尝试减去 1000000000000000 和 0000000000000001。

这是我的 Node 类:

    private class Node
{
private int data;
private Node next;
private Node previous;

private Node(Node previousNode, int aData, Node nextNode)
{
previous = previousNode;
data = aData;
next = nextNode;
}

private Node(int aData)
{
this(null, aData, null);
}
}

最佳答案

该问题可能是由于未能初始化成员变量数据而发生的。仔细检查你的代码并逐步调试。

关于java - 无限整数链表 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36023184/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com