Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从Java ArrayList一次显示一个项目_Java_Jsp_Arraylist - Fatal编程技术网

如何从Java ArrayList一次显示一个项目

如何从Java ArrayList一次显示一个项目,java,jsp,arraylist,Java,Jsp,Arraylist,我想知道是否有人可以帮助我,我想在某个按钮的帮助下,一次只迭代一项ArrayList,这样当我按下该按钮时,它会显示列表中的下一项,以此类推。或者如果有别的办法,我很乐意接受任何建议。我正试图设计一个测试,让学生一次只得到一个问题,然后提交答案,然后转到下一个问题。这就是我在JSP上显示的完整列表。 我的Servlet String vassId = request.getParameter("vassId"); List<Assessment> qL

我想知道是否有人可以帮助我,我想在某个按钮的帮助下,一次只迭代一项ArrayList,这样当我按下该按钮时,它会显示列表中的下一项,以此类推。或者如果有别的办法,我很乐意接受任何建议。我正试图设计一个测试,让学生一次只得到一个问题,然后提交答案,然后转到下一个问题。这就是我在JSP上显示的完整列表。 我的Servlet

        String vassId = request.getParameter("vassId");
        List<Assessment> qList = new ArrayList<Assessment>();
        Assessment qObj = null;
        DbConnection dbConn = null;
        Connection conn = null;
        CallableStatement proc = null;
        response.setContentType("text/html");
        ResultSet rs = null;

        try {
            dbConn = new DbConnection();
            conn = DbConnection.connection();

            String dbCall = "{ ? = call pa_customer_admin.fn_list_question(?) }";
            proc = DbConnection.connection().prepareCall(dbCall);
            proc.registerOutParameter(1, OracleTypes.CURSOR);
            proc.setInt(2, Integer.parseInt(vassId));
            proc.execute();
            rs = (ResultSet) proc.getObject(1);
            while (rs.next()) {
                qObj = new Assessment();
                qObj.setVassId(Integer.parseInt(rs.getString(1)));
                qObj.setDescr(rs.getString(2));
                qObj.setQuesStatus(rs.getString(3));
                qObj.setQuesTypeCode(rs.getString(4));
                qObj.setCreatedDate(rs.getString(6));
                qObj.setQuestion(rs.getString(7));
                qObj.setMark(rs.getString(8));
                qObj.setTimeLimit(rs.getInt(9));
                qList.add(qObj);
            }
            request.setAttribute("qObj", qObj);
            request.setAttribute("qList", qList);
            proc.close();
String vassId=request.getParameter(“vassId”);
List qList=new ArrayList();
评估qObj=null;
dbConn=null;
连接conn=null;
CallableStatement proc=null;
response.setContentType(“text/html”);
结果集rs=null;
试一试{
dbConn=新的DbConnection();
conn=DbConnection.connection();
String dbCall=“{?=call pa_customer_admin.fn_list_question(?)}”;
proc=DbConnection.connection().prepareCall(dbCall);
进程registerOutParameter(1,OracleTypes.CURSOR);
proc.setInt(2,Integer.parseInt(vassId));
proc.execute();
rs=(ResultSet)proc.getObject(1);
while(rs.next()){
qObj=新评估();
setVassId(Integer.parseInt(rs.getString(1));
qObj.setDescr(rs.getString(2));
qObj.setQuesStatus(rs.getString(3));
qObj.setQuesTypeCode(rs.getString(4));
setCreatedDate(rs.getString(6));
qObj.setQuestion(rs.getString(7));
qObj.setMark(rs.getString(8));
qObj.setTimeLimit(rs.getInt(9));
qList.add(qObj);
}
setAttribute(“qObj”,qObj);
setAttribute(“qList”,qList);
过程关闭();
JSP


空列表
问题:
${q.question}
答复1

答复2

ArrayList实现了Iterable接口,并对其成员具有基于索引的访问权限。您只需阅读一些有关用法示例的Java文档即可。

尝试类似(未测试)的方法:

final List questions=Arrays.asList(新字符串[]{“question1”、“question2”、“question3”});
JButton b=新JButton(“按我”);
最终JLabel标签=新的JLabel();
最终碳纳米管=0;
b、 addActionListener(新ActionListener(){
已执行的公共无效行动(行动事件e){
label.setText(question.get(cnt++));
如果(cnt>questions.size())cnt=0;
}
});
JFrame=新JFrame();
frame.getConentPane().add(b);
frame.getConentPane().add(标签);
框架。设置尺寸(800600);
frame.setVisible(true);

因为它是一个ArrayList,所以您可以使用索引。当按下按钮时,增加索引并从ArrayList中获取下一项

范例

package items;

import java.util.List;

public class GetNextItem {

    private static int itemIndex;

    private List<String> itemList;

    // Constructor with itemList
    public GetNextItem(List<String> itemList) {
        this.itemList = itemList;
        itemIndex = 0; // start at the first item.
    }

    /**
     * Get the current Item from the itemList 
     * @return the current item from the itemList or NULL if all the items are processed.
     */
    public String getItem() {
        if (itemIndex >= itemList.size()) {
            return null;    // end of list
        }
        return itemList.get(itemIndex);
    }

    /**
     * Get the next Item from the itemList 
     * @return the next item from the itemList or NULL if all the items are processed.
     */
    public String getNextItem() {
        itemIndex ++;
        return getItem();
    }
}
包装项目;
导入java.util.List;
公共类GetNextItem{
私有静态int项索引;
私人清单项目清单;
//具有itemList的构造函数
公共GetNextItem(列表项列表){
this.itemList=itemList;
itemIndex=0;//从第一项开始。
}
/**
*从itemList中获取当前项
*@从itemList返回当前项目,如果所有项目都已处理,则返回NULL。
*/
公共字符串getItem(){
如果(itemIndex>=itemList.size()){
返回null;//列表结束
}
返回itemList.get(itemIndex);
}
/**
*从itemList中获取下一项
*@返回itemList中的下一个项目,如果所有项目都已处理,则返回NULL。
*/
公共字符串getNextItem(){
itemIndex++;
返回getItem();
}
}

可以通过索引轻松访问
数组列表的元素

从网页(由JSP构建)中,您可以使用查询参数请求特定问题,例如
showquestion.JSP?question=4


然后,JSP生成的页面可以包含一个按钮,通过生成一个新链接(
)来显示下一个问题使用下一个更高的索引,只要有更多的问题。

谢谢你的回答,我需要一次显示一个项目。对此有什么想法吗?就像我说的,都在Java文档中。@DmytroZnaiko你可能想添加注释而不是答案?听起来这并不是关于ArrayList数据结构的问题,但是一个关于JSP页面或web会话中状态管理的问题(问题的文本没有提到JSP,但它被标记为JSP)是的,Simon我正在使用Servlet,从数据库创建一个ArrayList并在JSP上显示它。存储当前索引,并在数组列表的索引上显示内容,下一步只需执行index++;回答很好,但问题被标记为
JSP
。哦,我的错……不过我保留了答案,因为逻辑保持不变
final List<String> questions = Arrays.asList(new String[]{"question1", "question2", "question3"});
JButton b = new JButton("Press Me");
final JLabel label = new JLabel();
final cnt = 0;
b.addActionListener(new ActionListener(){
    pubic void actionPerformed(ActionEvent e) {
        label.setText(question.get(cnt++));
        if(cnt > questions.size()) cnt = 0; 
    }
});
JFrame frame = new JFrame();
frame.getConentPane().add(b);
frame.getConentPane().add(label);
frame.setSize(800,600);
frame.setVisible(true);
package items;

import java.util.List;

public class GetNextItem {

    private static int itemIndex;

    private List<String> itemList;

    // Constructor with itemList
    public GetNextItem(List<String> itemList) {
        this.itemList = itemList;
        itemIndex = 0; // start at the first item.
    }

    /**
     * Get the current Item from the itemList 
     * @return the current item from the itemList or NULL if all the items are processed.
     */
    public String getItem() {
        if (itemIndex >= itemList.size()) {
            return null;    // end of list
        }
        return itemList.get(itemIndex);
    }

    /**
     * Get the next Item from the itemList 
     * @return the next item from the itemList or NULL if all the items are processed.
     */
    public String getNextItem() {
        itemIndex ++;
        return getItem();
    }
}