简体   繁体   English

为什么我的方法多次打印?

[英]Why is my method printing multiple times?

I have three different programs that all read in from each other. 我有三个互不相同的程序。 One method creates a toString method for individual objects, the second reads in a file with a list of individual objects, and the third creates an additional toString method that calls the first one and creates a toString for the second method to use. 第一个方法为单个对象创建toString方法,第二个方法读取包含单个对象列表的文件,第三个方法创建另一个toString方法,该方法调用第一个对象,并创建toString供第二个方法使用。 The information is printing multiple times, and I can't figure out why. 该信息正在打印多次,我不知道为什么。 iList is an array list that contains various objects. iList是一个包含各种对象的数组列表。 The output I am getting is correct, but it is just printing four times instead of one. 我得到的输出是正确的,但它只打印四次而不是一次。

toString method in first program: 第一个程序中的toString方法:

public String toString() {

  NumberFormat dollarFmt = NumberFormat.getCurrencyInstance();
  DecimalFormat percentFmt = new DecimalFormat("#.0%");

  String output = "\nDescription: " + description.trim(); 
  output += "\nCost: " + dollarFmt.format(cost); 
  output += "   Percent Depreciation: " 
     + percentFmt.format(percentDepreciated);    
  output += "\nCurrent Value: " 
     + dollarFmt.format(cost - (cost * percentDepreciated));

  if (isEligibleToScrape()) {
     output += "\n*** Eligible to scrape ***";
  }   

  if (percentDepreciatedOutOfRange()) {
     output += "\n*** Percent Depreciated appears to be out of range ***";
  }
}

toString method in third program: 第三个程序中的toString方法:

public String toString() { 

  String output = ("\n" + inventoryName + "\n");

  int index = 1;
  while (index < iList.size()) {

     output += (iList.toString());  

     index++;
  }

  return output;
}

Calling toString from the third program in the second program: 从第二个程序中的第三个程序调用toString:

Inventory myInventoryList 
     = new Inventory(inventoryName, inventoryList);

  System.out.println(myInventoryList.toString());

You are adding iList.toString() to the output multiple times : 您将iList.toString()多次添加到输出中:

  while (index < iList.size()) {

     output += (iList.toString());  

     index++;
  }

That's why it's printing multiple times. 这就是为什么要多次打印的原因。

I don't know what's the type of iList , but it looks like it's some kind of list, so you probably want to add a String representation of an element of the list to the output in each iteration of the while loop (instead of the entire list every time). 我不知道iList的类型是iList ,但它看起来像某种列表,因此您可能想在while循环的每次迭代中将列表元素的String表示形式添加到输出中(而不是每次都有完整列表)。

Since iList is an ArrayList, you need to change the loop to : 由于iList是ArrayList,因此您需要将循环更改为:

  int index = 0;
  while (index < iList.size()) {
     output += iList.get(index).toString();   
     index++;
  }

Or : 要么 :

  for (int i=0; i<iList.size();i++)
     output += iList.get(i).toString();   

Of course it would be better to append the output to a StringBuilder instead of creating a new String in each iteration. 当然,最好将输出附加到StringBuilder,而不是在每次迭代中创建一个新的String。

这样做: output += (iList.get(index).toString());

Change this 改变这个

while (index < iList.size()) {
  output += (iList.toString());  
  index++;
}

to something like (assuming iList is a List ) 到类似的东西(假设iListList

while (index < iList.size()) {
  output += iList.get(index); // <-- toString() is implicit here  
  index++;
}

Which will get(int) the individual item to be printed (instead of printing the entire List each time) and (implicitly) invoke toString() (which you have already overridden). 这将get(int)要打印的单个项目(而不是每次打印整个List ),并(隐式)调用toString() (您已重写)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM