【问题标题】:Why is my array list size zero when i initialised it to 30?当我将它初始化为 30 时,为什么我的数组列表大小为零?
【发布时间】:2013-08-09 17:14:02
【问题描述】:

为什么当我将数组列表初始化为 30 时,它的大小为零?

当 addRecord() 被调用时我得到java.lang.IndexOutOfBoundsException: Index: 1, Size: 0

(注意:从 jsp 调用 setInitialValues 没有帮助。)
(注:ShootRecord 也实现了 Serializable)

Servlet 过程请求方法

        protected void processRequest(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

    PrintWriter pw = resp.getWriter();  
    String address = null;
    HttpSession session = req.getSession();

    ShootMeet meetBean = (ShootMeet)session.getAttribute("shootmeetbean");

    if(meetBean == null){
        pw.print("initialising meet \n");
        meetBean = new ShootMeet();
        meetBean.setInitialValues(30);
    }


    ShootRecord recordSent = (ShootRecord)session.getAttribute("shootrecordbean");
    if(recordSent == null){  
         recordSent = new ShootRecord();
    }

// **record sent created here**

try{            meetBean.addRecord(recordSent);
} ...
}
 // doGet and doPost call processRequest

ShootMeet

public class ShootMeet implements Serializable{

    private ArrayList<ShootRecord> listOfRecords;
    private String date;
    private int numTargets;

    public ShootMeet(){}

    public void setInitialValues(int numTarg){

        numTargets = numTarg;
        listOfRecords = new ArrayList<ShootRecord>(numTargets);


    }

    public void addRecord(ShootRecord s){

        if(listOfRecords.size() == 0){
        System.out.println("size of list of records is zero before adding record");
        }
        listOfRecords.add(s.targetNumber, s);

    }
    //...other setters/getters
}

index.jsp

    <%@page import="servlet.ShootRecord"%>
<%@page import="servlet.ShootRecordMap"%>

<%@ page contentType="text/html" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<jsp:useBean id="shootmeetbean" class="servlet.ShootMeet" scope = "session"/>
    <%--<jsp:setProperty name = "shootmeetbean" property = "initialValues" value = "1"/>
</jsp:useBean>--%>
<jsp:useBean id="shootrecordbean" class="servlet.ShootRecord" scope = "session"/>   


<html>
   // ... jstl tags
</html>

【问题讨论】:

    标签: jsp arraylist initialization jstl javabeans


    【解决方案1】:
        Here 
        may be u r trying to add value in List at Index 1 when the u r Array List Size is Zero.
    
        listOfRecords.add(1, s);
        The above   statement will throw Exception if the listOfRecords.size()==0;
        So try to add values in List from Index 0;
        There is different between Capacity and Size.Capacity is the how many elements u can add in u r list and it gets incremented and size is the how many elements u r list currently poses.
    So if the array list size is zero u cant start adding elements from Index 1. 
    

    【讨论】:

      【解决方案2】:

      我将列表容量与列表大小混淆了,将数组的功能与 ArrayLists 的功能混淆了。

      more detailed answer

      大小是列表中元素的数量; 容量是列表可以容纳多少元素而不重新分配其内部结构。 当您调用 new ArrayList(10) 时,您正在设置列表的初始容量,而不是其大小。换句话说,当以这种方式构造时,数组列表开始它的生命是空的。

      【讨论】:

        猜你喜欢
        • 2017-04-17
        • 1970-01-01
        • 1970-01-01
        • 2015-07-28
        • 2019-06-27
        • 1970-01-01
        • 2012-01-07
        • 1970-01-01
        • 2021-12-27
        相关资源
        最近更新 更多