jsp数组列表问题
我正在尝试创建一个数组,如果值/大小超过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我只是想在尝试回答之前澄清您的代码。
只添加一项而不再添加的原因如下:
第一次调用此方法时,“isize”等于 0,因此跳过 for 循环。 第二次调用此方法时,“isize”等于零,因此循环执行一次并在添加之前返回。
我不确定你想在 for 循环内部做什么,因为它确实没有意义。 如果您只想将 items 数组限制为 20 个或更少,只需添加
到您的代码中即可。 如果您正在尝试一些更复杂的逻辑,请解释您的目标。
I just want to clarify your code before I attempt to answer.
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
to your code. If there is some more complex logic that you are attempting then explain your goals.
在将项目添加到列表之前,您将从
addItem
方法返回。You're returning from the
addItem
method before adding the item to the list.