Java jsp数组列表问题

Java jsp数组列表问题,java,jsp,netbeans6.5,Java,Jsp,Netbeans6.5,我正在尝试创建一个数组,如果值/大小大于20,则不添加项。 此解决方案仅添加1项,然后即使该值小于20,也会停止。 如何更改它,使其最多只接受20个值 package business; import java.io.Serializable; import java.util.ArrayList; public class Cart implements Serializable { private ArrayList<LineItem> items; public C

我正在尝试创建一个数组,如果值/大小大于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;
            }
        }
    }
}
包装业务;
导入java.io.Serializable;
导入java.util.ArrayList;
公共类Cart实现了可序列化
{
私有ArrayList项;
公共购物车()
{
items=newarraylist();
}
公共阵列列表getItems()
{
退货项目;
}
public int getCount()
{
返回items.size();
}
公共无效附加项(行项目)
{
字符串代码=item.getProduct().getCode();
int数量=item.getQuantity();
双积分=item.getProduct().getCHours();
字符串credit2=Double.toString(item.getProduct().getCHours());
int isize=items.size();
对于(int i=0;i如果(isize您在将项目添加到列表之前从
addItem
方法返回

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

if(isize我只想在回答之前澄清一下您的代码

// 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);
//获取购物车中的商品总数
int isize=items.size();
//循环浏览项目
//如果有超过20个项目,则设置信贷数量和
//返回而不添加
//否则什么也不做
对于(int i=0;iif(isize)是否有需要数组的原因?能否更详细地解释addItem()是什么方法正在做什么?@fdon您可能不应该在问题标题中包含JSP,因为这与JSP无关。这是纯Java。此外,您应该只包含给您带来问题的代码。您好,非常感谢您抽出时间回复我认为wat u说的是对的,所以我尝试了这个if(isize)
// 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);
if (items.size() < 20) {
  items.add(item);
}