简体   繁体   English

如何在java中将字符串数组元素添加到arraylist

[英]how to add string array elements to arraylist in java

How do I add string array elements to an arraylist?如何将字符串数组元素添加到数组列表?

  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    List<BudgetEntity> budgetEntityList = new ArrayList<>(20); 
    BudgetEntity budgetEntity = new BudgetEntity();

    String budgetID[] = request.getParameterValues("budgetID");

for(int i=0;   i < budgetID.length ;   i++) {

      for(int k=0;   k < budgetEntityList.size() ;   k++) {

          budgetEntity=  budgetEntityList.get(k);
      }

            budgetEntity.setTotal(Integer.valueOf(budgetID[i]));
        }

Your budgetEntityList is empty when you do budgetEntity= budgetEntityList.get(k);当您执行budgetEntity= budgetEntityList.get(k);时,您的budgetEntityList为空budgetEntity= budgetEntityList.get(k);

Doing a new ArrayList<>(capacity) does not fill your ArrayList with BudgetList objects, it only sets the initial capacity of your ArrayList.执行new ArrayList<>(capacity)不会用 BudgetList 对象填充 ArrayList,它只会设置 ArrayList 的初始容量。

So, what you need to do is:所以,你需要做的是:

 for(int k=0;   k < budgetEntityList.size() ;   k++) {
      budgetEntity = new BudgetEntity();
      budgetEntityList.add(budgetEntity);
  }

And then, set the total of your BudgetEntity然后,设置您的 BudgetEntity 总数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM