Java:无法将 int 添加到数组列表
我在这里创建了一个空数组列表:
private ArrayList<OrderItem> conveyorBelt = new ArrayList<OrderItem>(10);
在同一个类中,我创建了一个方法,通过输入 orderNum (这是一个 int)将项目添加到传送带中。该方法如下所示:
public void addToConveyorBelt( int orderNum )
{
OrderItem oi;
conveyorBelt.add(oi.getOrderNum(orderNum)); // line 4
}
这不起作用。我在第 4 行收到编译错误,内容如下: http://i52.tinypic.com/ny8n86.jpg
有人知道我哪里出了问题吗?
ps - OrderItem 包含一个名为 theOrderNum 的变量和一个调用该变量的名为 getOrderNum 的方法。
I've created an empty arraylist here:
private ArrayList<OrderItem> conveyorBelt = new ArrayList<OrderItem>(10);
And in the same class, I've created a method where I add item into the conveyorBelt by inputting the orderNum (which is an int). This is what the method looks like:
public void addToConveyorBelt( int orderNum )
{
OrderItem oi;
conveyorBelt.add(oi.getOrderNum(orderNum)); // line 4
}
This doesn't work. I get a compile error on line 4, saying this: http://i52.tinypic.com/ny8n86.jpg
Anyone know where i'm going wrong with this?
p.s. - the OrderItem contains one variable called theOrderNum and a method that calls it called getOrderNum.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据错误消息,“getOrderNo”的方法签名不接受“int”类型的参数。根据您给出的内容,我怀疑 OrderItem 类看起来像这样:
如果是这样,您想从 orderNum 参数创建一个新的 OrderItem,然后将其添加到传送带:
如果不是,您需要更新问题以包括有关 OrderItem 类的更多信息。
According to the error message, the method signature of "getOrderNo" does not accept a parameter of type "int". From what you've given, I suspect the OrderItem class looks something like this:
If so, you want to create a new OrderItem from the orderNum parameter, and then add it to conveyerBelt:
If not, you need to update the question to include more information about the OrderItem class.
getOrderNum 显然返回一个 int 。您的数组列表是
OrderItems
的数组列表。你期待发生什么?不仅如此,还保证会出现空指针异常,因为
oi
未初始化。getOrderNum obviously returns an int. Your arraylist is an arraylist of
OrderItems
. What are you expecting to happen?Not only that, but you're guarunteed a null pointer exception there because
oi
isn't initialised.我怀疑你的方法应该是这样的:
I suspect your method should be something like this:
你这样定义 arraylist
这意味着你只能添加
OrderItem
到列表中更改如下
oi.getOrderNum(orderNum)
应该抛出 空指针异常you define arraylist like that
It means you can only add
OrderItem
to the listChange it as follows
oi.getOrderNum(orderNum)
should throw NullPointerException