Java toString方法与for循环不起作用,不确定原因
这应该是一个to-string方法,它是这个Java toString方法与for循环不起作用,不确定原因,java,Java,这应该是一个to-string方法,它是这个ArrayList中所有项的串联,但是当我调用它时,它根本不起任何作用。我没有得到任何错误;只是代码根本不起任何作用 我以前使用过这个方法,它不是for循环,它只是多次调用toString方法,但现在我尝试连接所有toString方法,但由于某种原因它不起作用 import java.util.ArrayList; /*this class creates an object of type catalog used to put ite
ArrayList
中所有项的串联,但是当我调用它时,它根本不起任何作用。我没有得到任何错误;只是代码根本不起任何作用
我以前使用过这个方法,它不是for循环,它只是多次调用toString
方法,但现在我尝试连接所有toString
方法,但由于某种原因它不起作用
import java.util.ArrayList;
/*this class creates an object of type catalog used to put items into and defines
methods used to manipulate this catalog object*/
public class Catalog
{
// instance variables - replace the example below with your own
private ArrayList<Item> items;
private final int MAX = 20;
private int size;
private int itemNum;
/**
* Constructor for objects of class Catalog
*/
public Catalog()
{
//makes empty arraylist
// initialise instance variables
items = new ArrayList<>(MAX);
size = 0;
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public void addItem(Item theItem)
{
// put your code here
items.add(theItem);
size = items.size();
}
public Item remItem(int theItem)
{
Item temp = this.find(theItem);
items.remove(temp);
size = items.size();
return temp;
}
public Item find(int itemNum){
for (Item item : this.items){
if (item.getItemNumber() == (itemNum)) {
return item;
}
}
return null;
}
public String toString()
{
String itemlist = "";
for (int i = 0; i < this.items.size(); i++){
itemlist += items.get(i).toString();
}
return itemlist;
}
public boolean isEmpty(){
return items.isEmpty();
}
}
这是我的项目类:
//This Is a class for a general Item
public class Item
{
// instance variables - replace the example below with your own
private int itemnum;
private String title;
private double price;
/**
* Constructor for objects of class Item
*/
public Item(int id, String t, double p)
{
// initialise instance variables
itemnum = id;
title = t;
price = p;
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public int getItemNumber()
{
// put your code here
return itemnum;
}
public String getItemType()
{
return "Item";
}
public String getItemTitle()
{
return title;
}
public String toString()
{
String line1, line2, line3, line4, out;
String itemtype = this.getItemType();
line1 = String.format("Item number: %d%n", itemnum);
line2 = String.format("Item type: %s%n", itemtype);
line3 = String.format("Item title: %s%n", title);
line4 = String.format("Item price: %.2f%n", price);
out = line1 + line2 + line3 + line4 + "\n";
return out;
}
}
可能这个.items.size()即将为零,它返回一个空字符串 更改了项目类的测试代码。还是没有问题
Catalog c=新目录();
System.out.println(“空:+c.toString());
c、 增补项(新项目(1,“abc”,2d));
c、 增补项(新项目(2,“定义”,3d));
c、 增补(新项目(3,“ghi”,4d));
System.out.println(“非空:+c.toString());
使用您提供的Item类,现在输出为
输出:
Empty :
Not empty : Item number: 1
Item type: Item
Item title: abc
Item price: 2.00
Item number: 2
Item type: Item
Item title: def
Item price: 3.00
Item number: 3
Item type: Item
Item title: ghi
Item price: 4.00
Process finished with exit code 0
试着编译代码并用命令行(java-jar YourApp.jar)运行它,但是IDE也应该像标准输出一样打印它。你确定
items
列表不是空的吗?你的items
对象是什么?你能编辑你的问题并发布整个类吗,如果你这样做,帮助会容易得多。没有错误表明代码正在工作。我想这与有关。项不包含任何数据。请发布完整的代码。您也在一个循环内连接,可能应该使用StringBuilder
。我仍然没有得到此代码的任何输出。也许这是一个IDE错误,但我刚刚发布了我的item类case@SteveRats我使用您提供的项
类更新了示例
Empty :
Not empty : Item number: 1
Item type: Item
Item title: abc
Item price: 2.00
Item number: 2
Item type: Item
Item title: def
Item price: 3.00
Item number: 3
Item type: Item
Item title: ghi
Item price: 4.00
Process finished with exit code 0