Java:在链表中添加数字会产生奇怪的结果
我必须编写一个程序,在LinkedList数组中存储多达10个任意长的数字,然后将它们相加,存储在第11个列表中,然后输出结果。我已经写了所有的代码,但是当我打印结果时,我得到了不可靠的结果。这是我的密码:Java:在链表中添加数字会产生奇怪的结果,java,linked-list,Java,Linked List,我必须编写一个程序,在LinkedList数组中存储多达10个任意长的数字,然后将它们相加,存储在第11个列表中,然后输出结果。我已经写了所有的代码,但是当我打印结果时,我得到了不可靠的结果。这是我的密码: import java.util.*; public class LongNumbers { private List<Integer> [] theLists; public LongNumbers(){ this.theLists = new Link
import java.util.*;
public class LongNumbers {
private List<Integer> [] theLists;
public LongNumbers(){
this.theLists = new LinkedList[11];
for (int i=0; i<11; i++)
this.theLists[i]= new LinkedList<>();
}
public void add(int location, int digit)
{
//add digit at head of LinkedList given by location
theLists[location].add(digit);
}
public int remove(int location)
{
//remove a digit from LinkedList given by location
return theLists[location].remove(location);
}
public boolean isEmpty(int location)
{
//check for an empty LinkedList given by location
return theLists[location].isEmpty();
}
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
//local variables
String number;
boolean userWantsToQuit = false;
LongNumbers lists = new LongNumbers();
int currArrayLocation = 0;
int currDigit = 0;
int maxLength = 0;
int numsEntered = 0;
int finalDigit = 0;
int carryDigit = 0;
char[] digits;
//get data
do{
System.out.println("Enter a number, or enter -1 to escape.");
number = kb.nextLine();
if(number.equals("-1"))
userWantsToQuit = true;
else {
numsEntered++;
if (number.length()> maxLength)
maxLength = number.length();
digits = new char[number.length()];
for (int i = 0; i < number.length(); i++){
digits[i] = number.charAt(i);
currDigit = digits[i]-48;
lists.add(currArrayLocation, currDigit);}
}
currArrayLocation++;
} while (!userWantsToQuit && currArrayLocation < 9 );
//if the linkedLists are full
if (currArrayLocation == 9)
System.out.println("The max amount of numbers have been entered.");
//to add numbers into final linked list
for (int i = 0; i < maxLength; i++){
for (currArrayLocation = 0; currArrayLocation < numsEntered; currArrayLocation++){
if (lists.theLists[currArrayLocation]== null){
lists.remove(0);}
//currDigit = 0;
else{
currDigit = lists.theLists[currArrayLocation].get(0);
int tempDigit = currDigit;
System.out.println("temp: " + tempDigit); //test line
finalDigit += tempDigit;
System.out.println(finalDigit); //test line
}
//add the proper digit to the last LinkedList and set carry digit
finalDigit = finalDigit + carryDigit;
carryDigit = finalDigit/10;
//lists.add(10, finalDigit);
//lists.remove(0);
//finalDigit = 0;
}
lists.add(10, finalDigit);
lists.remove(0);
finalDigit = 0;
}
//print the sum
if (lists.theLists[10] != null){
System.out.print("Sum: ");
for (int i = 0; i < lists.theLists[10].size() ; i++){
System.out.print(lists.theLists[10].get(i));
}
}
}//end main
}//end class
import java.util.*;
公共类长号码{
私人名单[]个人名单;
公众电话号码(){
this.theLists=新链接列表[11];
对于(int i=0;i maxLength)
maxLength=number.length();
数字=新字符[number.length()];
对于(int i=0;i
当我把100和200加起来,得到322。我认为我的问题在于注释//后面的几行将数字添加到最终的链表中
任何见解或想法都会很棒,提前谢谢 是的,这个确实有很多问题 我将您的代码通过BlueJ的调试器,经过痛苦的调试,我终于发现了问题。很快我就意识到你的第81行应该是:
currDigit = lists.theLists[currArrayLocation].get(i);
但是如果你这样做,你会得到一个索引自动边界异常
,这让我困惑了一段时间,但我最终发现了有问题的第97行:
lists.remove(0);
这让我发现您的公共int-remove(int-location)
方法完全错误。它所做的唯一一件事就是删除你其中一个号码中的一个数字。我想你的意思是这样的:
public void remove(int location)
{
//remove a digit from LinkedList given by location
for (int i=0; i<theLists[location].size(); i++){
theLists[location].remove(i);
theLists[location].add(i,0);
}
}
public static void bufferWithLeadingZeroes(List<Integer>[] theLists, int maxLength){
for (int i=0; i<10; i++){
int howManyToAdd = maxLength - theLists[i].size();
for (int j=0; j<howManyToAdd; j++){
theLists[i].add(0,0);
}
}
theLists[10].add(0,0);
}
但是,像往常一样,这又引起了另一个问题。当将不同长度的数字相加时,我们在第81行(我们之前修复的那行)上得到另一个IndexOutOfBoundsException
。结果表明,这是由顶部循环造成的,该循环假定输入的每个数字都具有相同的长度。为了解决这个问题,我写了一个方法,在所有数字的开头输入前导零,如下所示:
public void remove(int location)
{
//remove a digit from LinkedList given by location
for (int i=0; i<theLists[location].size(); i++){
theLists[location].remove(i);
theLists[location].add(i,0);
}
}
public static void bufferWithLeadingZeroes(List<Integer>[] theLists, int maxLength){
for (int i=0; i<10; i++){
int howManyToAdd = maxLength - theLists[i].size();
for (int j=0; j<howManyToAdd; j++){
theLists[i].add(0,0);
}
}
theLists[10].add(0,0);
}
所以现在,最后,吸盘起作用了。以下是(节略)输出:
您会注意到,Sum是用前导零打印的。您可能需要更改打印数字的最终循环,以便它可以修剪这些数字
最后,这里是我们完整的工作代码:
import java.util.*;
public class LongNumbers {
private List<Integer> [] theLists;
public LongNumbers(){
this.theLists = new LinkedList[11];
for (int i=0; i<11; i++)
this.theLists[i]= new LinkedList<>();
}
public void add(int location, int digit)
{
//add digit at head of LinkedList given by location
theLists[location].add(digit);
}
public void remove(int location)
{
//remove a digit from LinkedList given by location
for (int i=0; i<theLists[location].size(); i++){
theLists[location].remove(i);
theLists[location].add(i,0);
}
}
public boolean isEmpty(int location)
{
//check for an empty LinkedList given by location
return theLists[location].isEmpty();
}
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
//local variables
String number;
boolean userWantsToQuit = false;
LongNumbers lists = new LongNumbers();
int currArrayLocation = 0;
int currDigit = 0;
int maxLength = 0;
int numsEntered = 0;
int finalDigit = 0;
int carryDigit = 0;
char[] digits;
//get data
do{
System.out.println("Enter a number, or enter -1 to escape.");
number = kb.nextLine();
if(number.equals("-1"))
userWantsToQuit = true;
else {
numsEntered++;
if (number.length()> maxLength)
maxLength = number.length();
digits = new char[number.length()];
for (int i = 0; i < number.length(); i++){
digits[i] = number.charAt(i);
currDigit = digits[i]-48;
lists.add(currArrayLocation, currDigit);}
}
currArrayLocation++;
} while (!userWantsToQuit && currArrayLocation < 9 );
//if the linkedLists are full
if (currArrayLocation == 9)
System.out.println("The max amount of numbers have been entered.");
bufferWithLeadingZeroes(lists.theLists,maxLength);
//to add numbers into final linked list
for (int i = 0; i < maxLength; i++){
for (currArrayLocation = 0; currArrayLocation < numsEntered; currArrayLocation++){
if (lists.theLists[currArrayLocation]== null){
lists.remove(0);}
//currDigit = 0;
else{
currDigit = lists.theLists[currArrayLocation].get(i);
int tempDigit = currDigit;
System.out.println("temp: " + tempDigit); //test line
finalDigit += tempDigit;
System.out.println(finalDigit); //test line
}
}
if (finalDigit > 9){
i++;
int newDigit = lists.theLists[10].get(i-1) + (finalDigit/10);
lists.theLists[10].remove(i-1);
lists.theLists[10].add(i-1,newDigit);
finalDigit %= 10;
for (int j=(i-1); j>=0; j--){
if (lists.theLists[10].get(j) > 9){
if (j == 0){
lists.theLists[10].add(j,0);
j++;
i++;
}
newDigit = lists.theLists[10].get(j-1) + 1;
lists.theLists[10].remove(j-1);
lists.theLists[10].add(j-1,newDigit);
newDigit = lists.theLists[10].get(j) % 10;
lists.theLists[10].remove(j);
lists.theLists[10].add(j,newDigit);
}
}
i--;
}
lists.add(10, finalDigit);
finalDigit = 0;
}
//print the sum
if (lists.theLists[10] != null){
System.out.print("Sum: ");
for (int i = 0; i < lists.theLists[10].size() ; i++){
System.out.print(lists.theLists[10].get(i));
}
}
}//end main
public static void bufferWithLeadingZeroes(List<Integer>[] theLists, int maxLength){
for (int i=0; i<10; i++){
int howManyToAdd = maxLength - theLists[i].size();
for (int j=0; j<howManyToAdd; j++){
theLists[i].add(0,0);
}
}
theLists[10].add(0,0);
}
}//end class
import java.util.*;
公共类长号码{
私人名单[]个人名单;
公众电话号码(){
this.theLists=新链接列表[11];
对于(int i=0;i 9){
i++;
int newDigit=lists.theLists[10].get(i-1)+(finaldigt/10);
列表。列表[10]。删除(i-1);
列表。列表[10]。添加(i-1,新数字);
finalDigit%=10;
对于(int j=(i-1);j>=0;j--){
if(lists.theLists[10].get(j)>9){
如果(j==0){
列表[10]。添加(j,0);
j++;
i++;
}
newDigit=列表。列表[10]。获取(j-1)+1;
列表。列表[10]。删除(j-1);
列表。列表[10]。添加(j-1,新数字);
newDigit=列表。列表[10]。获取(j)%10;
列表。列表[10]。删除(j);
列表。列表[10]。添加(j,新数字);
}
}
我--;
}
列表。添加(10,最终数字);
finalDigit=0;
}
//打印总数
if(lists.theLists[10]!=null){
系统输出打印(“总和:”);
对于(int i=0;i 对于(int i=0;iSo此处的目标是使用数组将一组非常大的数字相加?或者是否有指定实现的特定赋值?这是一个需要使用一组链表将数字相加的赋值@jonahhaney考虑到java.math.biginger具有更平滑的存储大数字的实现。Jonah,我非常感谢你。这是我在这个网站上见过的最彻底的答案,我非常感谢你指导我完成每一步。我的教授并不是特别有帮助,我觉得我现在真正理解了每一步的错误所在,这不是每个程序员都应该追求的目标吗为?再次感谢,并为延迟回复@JonahHaneyNo prob表示歉意
import java.util.*;
public class LongNumbers {
private List<Integer> [] theLists;
public LongNumbers(){
this.theLists = new LinkedList[11];
for (int i=0; i<11; i++)
this.theLists[i]= new LinkedList<>();
}
public void add(int location, int digit)
{
//add digit at head of LinkedList given by location
theLists[location].add(digit);
}
public void remove(int location)
{
//remove a digit from LinkedList given by location
for (int i=0; i<theLists[location].size(); i++){
theLists[location].remove(i);
theLists[location].add(i,0);
}
}
public boolean isEmpty(int location)
{
//check for an empty LinkedList given by location
return theLists[location].isEmpty();
}
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
//local variables
String number;
boolean userWantsToQuit = false;
LongNumbers lists = new LongNumbers();
int currArrayLocation = 0;
int currDigit = 0;
int maxLength = 0;
int numsEntered = 0;
int finalDigit = 0;
int carryDigit = 0;
char[] digits;
//get data
do{
System.out.println("Enter a number, or enter -1 to escape.");
number = kb.nextLine();
if(number.equals("-1"))
userWantsToQuit = true;
else {
numsEntered++;
if (number.length()> maxLength)
maxLength = number.length();
digits = new char[number.length()];
for (int i = 0; i < number.length(); i++){
digits[i] = number.charAt(i);
currDigit = digits[i]-48;
lists.add(currArrayLocation, currDigit);}
}
currArrayLocation++;
} while (!userWantsToQuit && currArrayLocation < 9 );
//if the linkedLists are full
if (currArrayLocation == 9)
System.out.println("The max amount of numbers have been entered.");
bufferWithLeadingZeroes(lists.theLists,maxLength);
//to add numbers into final linked list
for (int i = 0; i < maxLength; i++){
for (currArrayLocation = 0; currArrayLocation < numsEntered; currArrayLocation++){
if (lists.theLists[currArrayLocation]== null){
lists.remove(0);}
//currDigit = 0;
else{
currDigit = lists.theLists[currArrayLocation].get(i);
int tempDigit = currDigit;
System.out.println("temp: " + tempDigit); //test line
finalDigit += tempDigit;
System.out.println(finalDigit); //test line
}
}
if (finalDigit > 9){
i++;
int newDigit = lists.theLists[10].get(i-1) + (finalDigit/10);
lists.theLists[10].remove(i-1);
lists.theLists[10].add(i-1,newDigit);
finalDigit %= 10;
for (int j=(i-1); j>=0; j--){
if (lists.theLists[10].get(j) > 9){
if (j == 0){
lists.theLists[10].add(j,0);
j++;
i++;
}
newDigit = lists.theLists[10].get(j-1) + 1;
lists.theLists[10].remove(j-1);
lists.theLists[10].add(j-1,newDigit);
newDigit = lists.theLists[10].get(j) % 10;
lists.theLists[10].remove(j);
lists.theLists[10].add(j,newDigit);
}
}
i--;
}
lists.add(10, finalDigit);
finalDigit = 0;
}
//print the sum
if (lists.theLists[10] != null){
System.out.print("Sum: ");
for (int i = 0; i < lists.theLists[10].size() ; i++){
System.out.print(lists.theLists[10].get(i));
}
}
}//end main
public static void bufferWithLeadingZeroes(List<Integer>[] theLists, int maxLength){
for (int i=0; i<10; i++){
int howManyToAdd = maxLength - theLists[i].size();
for (int j=0; j<howManyToAdd; j++){
theLists[i].add(0,0);
}
}
theLists[10].add(0,0);
}
}//end class