Java:无法将 int 添加到数组列表

发布于 2024-10-09 10:50:25 字数 576 浏览 4 评论 0原文

我在这里创建了一个空数组列表:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧梦荧光笔 2024-10-16 10:50:25

根据错误消息,“getOrderNo”的方法签名不接受“int”类型的参数。根据您给出的内容,我怀疑 OrderItem 类看起来像这样:

public class OrderItem {
    private int theOrderNum;

    OrderItem(int num) {
        theOrderNum = num;
    }

    public int getOrderNum() {
        return theOrderNum;
    }
}

如果是这样,您想从 orderNum 参数创建一个新的 OrderItem,然后将其添加到传送带:

public void addToConveyorBelt(int orderNum) {
    OrderItem oi = new OrderItem(orderNum);
    conveyorBelt.add(oi);
}

如果不是,您需要更新问题以包括有关 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:

public class OrderItem {
    private int theOrderNum;

    OrderItem(int num) {
        theOrderNum = num;
    }

    public int getOrderNum() {
        return theOrderNum;
    }
}

If so, you want to create a new OrderItem from the orderNum parameter, and then add it to conveyerBelt:

public void addToConveyorBelt(int orderNum) {
    OrderItem oi = new OrderItem(orderNum);
    conveyorBelt.add(oi);
}

If not, you need to update the question to include more information about the OrderItem class.

浅笑轻吟梦一曲 2024-10-16 10:50:25

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.

清风无影 2024-10-16 10:50:25

我怀疑你的方法应该是这样的:

public void addToConveyorBelt(int orderNum){
    OrderItem oi = getOrderItem(orderNum);
    conveyorBelt.add(oi); 
}

I suspect your method should be something like this:

public void addToConveyorBelt(int orderNum){
    OrderItem oi = getOrderItem(orderNum);
    conveyorBelt.add(oi); 
}
红颜悴 2024-10-16 10:50:25

你这样定义 arraylist

private ArrayList<OrderItem>  conveyorBelt = new ArrayList<OrderItem>(10);

这意味着你只能添加 OrderItem 到列表中

public void addToConveyorBelt( int orderNum )
{
OrderItem oi;
conveyorBelt.add(oi.getOrderNum(orderNum)); // here it fails
}

更改如下

public void addToConveyorBelt( int orderNum )
    {
    OrderItem oi = new OrderItem(orderNum);
    conveyorBelt.add(oi); // here it fails
    }

oi.getOrderNum(orderNum) 应该抛出 空指针异常

you define arraylist like that

private ArrayList<OrderItem>  conveyorBelt = new ArrayList<OrderItem>(10);

It means you can only add OrderItem to the list

public void addToConveyorBelt( int orderNum )
{
OrderItem oi;
conveyorBelt.add(oi.getOrderNum(orderNum)); // here it fails
}

Change it as follows

public void addToConveyorBelt( int orderNum )
    {
    OrderItem oi = new OrderItem(orderNum);
    conveyorBelt.add(oi); // here it fails
    }

oi.getOrderNum(orderNum) should throw NullPointerException

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文