簡體   English   中英

如何在java中將字符串數組元素添加到arraylist

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

如何將字符串數組元素添加到數組列表?

  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]));
        }

當您執行budgetEntity= budgetEntityList.get(k);時,您的budgetEntityList為空budgetEntity= budgetEntityList.get(k);

執行new ArrayList<>(capacity)不會用 BudgetList 對象填充 ArrayList,它只會設置 ArrayList 的初始容量。

所以,你需要做的是:

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

然后,設置您的 BudgetEntity 總數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM