jsp数组列表问题

发布于 2024-07-21 12:35:21 字数 2153 浏览 6 评论 0原文

我正在尝试创建一个数组,如果值/大小超过 20,则不会添加项目。 此解决方案仅添加 1 项,然后即使值小于 20 也会停止。 我该如何更改它,使其只接受最多 20 个值。

package business;
import java.io.Serializable;
import java.util.ArrayList;

public class Cart implements Serializable
{
private ArrayList<LineItem> items;

    public Cart()
{
    items = new ArrayList<LineItem>();
}

    public ArrayList<LineItem> getItems()
{
    return items;
}

    public int getCount()
{
    return items.size();
}

    public void addItem(LineItem item)
{
    String code = item.getProduct().getCode();
    int quantity = item.getQuantity();
    double credit = item.getProduct().getCHours();
    String credit2 = Double.toString(item.getProduct().getCHours());
    int isize = items.size();


    for (int i = 0; i < items.size(); i++)
    {
        if(isize <= 20)
        {
        LineItem lineItem = items.get(i);
            lineItem.setQuantityCredit(credit);
            return;
       }
    }
    items.add(item);
    }

    public void addItemCredit(LineItem item)
{
        double credit = item.getProduct().getCHours();
        String credit2 = Double.toString(item.getProduct().getCHours());
        String code = item.getProduct().getCode();

    for (int i = 0; i < 20; i++)
    {
        LineItem lineItem2 = items.get(i);
            lineItem2.setQuantityCredit(credit);
            return;

        }
    items.add(item);

    }

public void removeItem(LineItem item)
{
        String code = item.getProduct().getCode();
        for (int i = 0; i < items.size(); i++)
        {
            LineItem lineItem = items.get(i);
            if (lineItem.getProduct().getCode().equals(code))
            {
                items.remove(i);
                return;
            }
        }
    }
}

我认为你说的是​​对的,所以我尝试了这个,

if(isize <= 20) 
{ 
LineItem lineItem = items.get(i); lineItem.setQuantityCredit(credit); 
} 
return; 
} 
items.add(item); 
} 


and 


if(isize <= 20) 
{ 
LineItem lineItem = items.get(i); lineItem.setQuantityCredit(credit); 
} 
} 
items.add(item); 
return; 
} 

但都不起作用。 那么我应该在哪里放置 return 语句呢?

i am trying to create an array that doesnt add items if the value/size is more than 20.
this solution adds only 1 item and then stops even if the value is less than 20.
how do i change it so that it accepts values only upto 20 max.

package business;
import java.io.Serializable;
import java.util.ArrayList;

public class Cart implements Serializable
{
private ArrayList<LineItem> items;

    public Cart()
{
    items = new ArrayList<LineItem>();
}

    public ArrayList<LineItem> getItems()
{
    return items;
}

    public int getCount()
{
    return items.size();
}

    public void addItem(LineItem item)
{
    String code = item.getProduct().getCode();
    int quantity = item.getQuantity();
    double credit = item.getProduct().getCHours();
    String credit2 = Double.toString(item.getProduct().getCHours());
    int isize = items.size();


    for (int i = 0; i < items.size(); i++)
    {
        if(isize <= 20)
        {
        LineItem lineItem = items.get(i);
            lineItem.setQuantityCredit(credit);
            return;
       }
    }
    items.add(item);
    }

    public void addItemCredit(LineItem item)
{
        double credit = item.getProduct().getCHours();
        String credit2 = Double.toString(item.getProduct().getCHours());
        String code = item.getProduct().getCode();

    for (int i = 0; i < 20; i++)
    {
        LineItem lineItem2 = items.get(i);
            lineItem2.setQuantityCredit(credit);
            return;

        }
    items.add(item);

    }

public void removeItem(LineItem item)
{
        String code = item.getProduct().getCode();
        for (int i = 0; i < items.size(); i++)
        {
            LineItem lineItem = items.get(i);
            if (lineItem.getProduct().getCode().equals(code))
            {
                items.remove(i);
                return;
            }
        }
    }
}

i think wat u said is right so i tried this

if(isize <= 20) 
{ 
LineItem lineItem = items.get(i); lineItem.setQuantityCredit(credit); 
} 
return; 
} 
items.add(item); 
} 


and 


if(isize <= 20) 
{ 
LineItem lineItem = items.get(i); lineItem.setQuantityCredit(credit); 
} 
} 
items.add(item); 
return; 
} 

but neither worked. so whr do i place d return statement?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

夏夜暖风 2024-07-28 12:35:21

我只是想在尝试回答之前澄清您的代码。

// get the total number of items in the cart
int isize = items.size();

// loop through the items
   // if there are more than 20 items then set the credit quantity and
     // return without adding
   // otherwise do nothing 
for (int i = 0; i < items.size(); i++)
{
    if(isize <= 20)
    {
    LineItem lineItem = items.get(i);
        lineItem.setQuantityCredit(credit);
        return;
   }
}
// if there are more than 20 items add another
items.add(item);

只添加一项而不再添加的原因如下:
第一次调用此方法时,“isize”等于 0,因此跳过 for 循环。 第二次调用此方法时,“isize”等于零,因此循环执行一次并在添加之前返回。

我不确定你想在 for 循环内部做什么,因为它确实没有意义。 如果您只想将 items 数组限制为 20 个或更少,只需添加

if (items.size() < 20) {
  items.add(item);
}

到您的代码中即可。 如果您正在尝试一些更复杂的逻辑,请解释您的目标。

I just want to clarify your code before I attempt to answer.

// get the total number of items in the cart
int isize = items.size();

// loop through the items
   // if there are more than 20 items then set the credit quantity and
     // return without adding
   // otherwise do nothing 
for (int i = 0; i < items.size(); i++)
{
    if(isize <= 20)
    {
    LineItem lineItem = items.get(i);
        lineItem.setQuantityCredit(credit);
        return;
   }
}
// if there are more than 20 items add another
items.add(item);

The reason that it is adding only one item and no more is as follows:
The first time this method is called "isize" equals zero and therefore the for loop is skipped. The second time this method is called the "isize" equals zero and therefore the loop goes once and returns before adding.

I'm not certain what you are attempting to do inside of the for loop as it really doesn't make sense. If you just want the items array to be limited to 20 or less just add

if (items.size() < 20) {
  items.add(item);
}

to your code. If there is some more complex logic that you are attempting then explain your goals.

眉黛浅 2024-07-28 12:35:21

在将项目添加到列表之前,您将从 addItem 方法返回。

if(isize <= 20)
{
     LineItem lineItem = items.get(i);
     lineItem.setQuantityCredit(credit);
     return; // This is the problem
}

You're returning from the addItem method before adding the item to the list.

if(isize <= 20)
{
     LineItem lineItem = items.get(i);
     lineItem.setQuantityCredit(credit);
     return; // This is the problem
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文