这句语句可以把request,和resqonse对象传送到acception.jsp页面。
现在有
Booksession booksession=new Booksession();
List list=booksession.BookSelect(bookname);
其中booksession.BookSelect(bookname)方法返回一个ArrayList对象。对象里的数据是从数据库里select到的多条数据。
请问怎么将list对象里的多条数据从servlet传送到jsp页面。
7 个解决方案
#1
request.setAttribute("list",list);
在jsp
List list = request.getAttribute("list");
在jsp
List list = request.getAttribute("list");
#2
request.setAttribute("list", list)或session.setAttribute("list", list)
在jsp页面
ArrayList list = (ArrayList) request.getAttribute("list")或ArrayList list = (ArrayList) session.getAttribute("list")
在jsp页面
ArrayList list = (ArrayList) request.getAttribute("list")或ArrayList list = (ArrayList) session.getAttribute("list")
#3
request.getSession().setAttribute("listall",listall);
在jsp页面用:
<c:forEach items="${sessionScope.list}" var="arraylist">
……
……
</c:forEach>
在jsp页面用:
<c:forEach items="${sessionScope.list}" var="arraylist">
……
……
</c:forEach>
#4
在 跳转前 添加一个属性
request.setAttribute("属性名",list);
如果你的Booksession 是一个bean的话,你可以在jsp中用
<c:forEach var="item" items="${属性名}">
<tr>
<td>${item.性质}</td>
...
....
</tr>
</c:forEach>
另外还可以这样
<c:forEach var="item" items="${属性名}" >
<tr>
<c:forEach var="colValue" items="${item}">
<td>${colValue}</td>
</c:forEach>
</tr>
</c:forEach>
你先看看这个可以不,我改过。我感觉第二个比较通用吧!
request.setAttribute("属性名",list);
如果你的Booksession 是一个bean的话,你可以在jsp中用
<c:forEach var="item" items="${属性名}">
<tr>
<td>${item.性质}</td>
...
....
</tr>
</c:forEach>
另外还可以这样
<c:forEach var="item" items="${属性名}" >
<tr>
<c:forEach var="colValue" items="${item}">
<td>${colValue}</td>
</c:forEach>
</tr>
</c:forEach>
你先看看这个可以不,我改过。我感觉第二个比较通用吧!
#5
同意2楼的做法!
#6
OpenBlogAction.java文件,产生的数据放置在ArrayList中,存储数据
package cn.com.blogonline.blogmain;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import cn.com.blogonline.common.Constants;
import cn.com.blogonline.common.DbOperate;
import cn.com.blogonline.form.Blog;
public class OpenBlogAction extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
}
/*
* 处理<GET> 请求方法.
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置接收信息的字符集
request.setCharacterEncoding("UTF-8");
//接收浏览器端提交的信息
String blogId = request.getParameter("blogid");
int iBlogId = Integer.parseInt(blogId);
/*
*左侧框架页信息设置
*/
HttpSession session = request.getSession(true);
DbOperate db=new DbOperate();
Blog blog = db.getBlog(iBlogId);
blog.setVisitcount(blog.getVisitcount() + 1);
db.update(blog);
session.setAttribute(Constants.VISIT_BLOG_KEY,blog);
List sortList=db.getBlogSorts(iBlogId);
session.setAttribute(Constants.SORT_LIST_KEY,sortList);
List linkList=db.getBlogLinks(iBlogId);
session.setAttribute(Constants.LINK_LIST_KEY,linkList);
List articleList=db.getBlogArticles(iBlogId,0);
/*
* 分页显示
*/
List dispList=new ArrayList();
for (int i=0;i<articleList.size();i++){
if (i<Constants.ARTICLE_PAGE_SIZE){
dispList.add(articleList.get(i));
}
}
String forward = request.getContextPath()+Constants.BLOG_MAIN;
session.setAttribute(Constants.ARTICLE_LIST_KEY,dispList);
//session.setAttribute(Constants.CUR_BLOGID_KEY,new Integer(iBlogId));
session.setAttribute(Constants.CUR_PAGEID_KEY,new Integer(0));
session.setAttribute(Constants.CUR_SORTID_KEY,new Integer(0));
response.sendRedirect(forward);
}
}
jsp页面接受
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ page import="cn.com.blogonline.common.Constants"%>
<%@ page import="cn.com.blogonline.form.Article"%>
<%@ page import="java.util.*"%>
<%
List articleList = (List)session.getAttribute(Constants.ARTICLE_LIST_KEY);
Article article = null;
Integer tpage=(Integer)session.getAttribute(Constants.CUR_PAGEID_KEY);
int pageId=tpage.intValue();
Integer tSortId=(Integer)session.getAttribute(Constants.CUR_SORTID_KEY);
int sortId=tSortId.intValue();
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>显示主页面</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="StyleSheet" type="text/css" href="../css/mystyle.css">
</head>
<body>
<table border="1" width="550" bordercolor="#FF9900">
<tr>
<td width="100%">
<p align="right">
<b><font color="#FF0000"><a href="newArticle.jsp">发表文章</a></font></b>
</p>
</td>
</tr>
<%if(articleList != null ){
System.out.println(articleList.size());
for(int i = 0;i < articleList.size();i++)
{
article = (Article)articleList.get(i);
%>
<tr>
<td width="100%">
<b><font color="#FF0000"><%=article.getTitle()%></font></b>
<p>
<b><font color="#FF0000"><%=article.getContent()%></font></b>
</p>
<p align="right">
<b><font color="#FF0000"><a href="../showFeedback?articleid=<%=article.getId()%>">查看评论</a></font></b>
</p>
</td>
</tr>
<%
}
} %>
<tr>
<td width="100%">
<p align="center">
| <font color="#999999"> <a href="../blogOperate?pageid=<%=pageId-1%>">上页</a></font> | <a href="../blogOperate?pageid=<%=pageId+1%>">下页</a> |
</p>
</td>
</tr>
</table>
</body>
</html>
package cn.com.blogonline.blogmain;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import cn.com.blogonline.common.Constants;
import cn.com.blogonline.common.DbOperate;
import cn.com.blogonline.form.Blog;
public class OpenBlogAction extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
}
/*
* 处理<GET> 请求方法.
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置接收信息的字符集
request.setCharacterEncoding("UTF-8");
//接收浏览器端提交的信息
String blogId = request.getParameter("blogid");
int iBlogId = Integer.parseInt(blogId);
/*
*左侧框架页信息设置
*/
HttpSession session = request.getSession(true);
DbOperate db=new DbOperate();
Blog blog = db.getBlog(iBlogId);
blog.setVisitcount(blog.getVisitcount() + 1);
db.update(blog);
session.setAttribute(Constants.VISIT_BLOG_KEY,blog);
List sortList=db.getBlogSorts(iBlogId);
session.setAttribute(Constants.SORT_LIST_KEY,sortList);
List linkList=db.getBlogLinks(iBlogId);
session.setAttribute(Constants.LINK_LIST_KEY,linkList);
List articleList=db.getBlogArticles(iBlogId,0);
/*
* 分页显示
*/
List dispList=new ArrayList();
for (int i=0;i<articleList.size();i++){
if (i<Constants.ARTICLE_PAGE_SIZE){
dispList.add(articleList.get(i));
}
}
String forward = request.getContextPath()+Constants.BLOG_MAIN;
session.setAttribute(Constants.ARTICLE_LIST_KEY,dispList);
//session.setAttribute(Constants.CUR_BLOGID_KEY,new Integer(iBlogId));
session.setAttribute(Constants.CUR_PAGEID_KEY,new Integer(0));
session.setAttribute(Constants.CUR_SORTID_KEY,new Integer(0));
response.sendRedirect(forward);
}
}
jsp页面接受
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ page import="cn.com.blogonline.common.Constants"%>
<%@ page import="cn.com.blogonline.form.Article"%>
<%@ page import="java.util.*"%>
<%
List articleList = (List)session.getAttribute(Constants.ARTICLE_LIST_KEY);
Article article = null;
Integer tpage=(Integer)session.getAttribute(Constants.CUR_PAGEID_KEY);
int pageId=tpage.intValue();
Integer tSortId=(Integer)session.getAttribute(Constants.CUR_SORTID_KEY);
int sortId=tSortId.intValue();
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>显示主页面</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="StyleSheet" type="text/css" href="../css/mystyle.css">
</head>
<body>
<table border="1" width="550" bordercolor="#FF9900">
<tr>
<td width="100%">
<p align="right">
<b><font color="#FF0000"><a href="newArticle.jsp">发表文章</a></font></b>
</p>
</td>
</tr>
<%if(articleList != null ){
System.out.println(articleList.size());
for(int i = 0;i < articleList.size();i++)
{
article = (Article)articleList.get(i);
%>
<tr>
<td width="100%">
<b><font color="#FF0000"><%=article.getTitle()%></font></b>
<p>
<b><font color="#FF0000"><%=article.getContent()%></font></b>
</p>
<p align="right">
<b><font color="#FF0000"><a href="../showFeedback?articleid=<%=article.getId()%>">查看评论</a></font></b>
</p>
</td>
</tr>
<%
}
} %>
<tr>
<td width="100%">
<p align="center">
| <font color="#999999"> <a href="../blogOperate?pageid=<%=pageId-1%>">上页</a></font> | <a href="../blogOperate?pageid=<%=pageId+1%>">下页</a> |
</p>
</td>
</tr>
</table>
</body>
</html>
#7
ArrayList里面存储的数据,第一种,单个的String类型的字符串或者Object对象。
第二种,二维数组
第三种,hashmap或者hashtable,hashtable的存取要是用iterator迭代器。
关于hashmap的存取的实例我写的代码如下,希望对搂主有所帮助。
package com.sytdc.cxl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class HashTableTest {
public static void main(String args[]){
List<HashMap> st;
HashMap<String,Integer> sh ;
Set se;
st = getSel();
for(int i=0;i<st.size();i++){
sh =new HashMap<String,Integer>();
sh = st.get(i);
se = sh.keySet();
Iterator ito = se.iterator();
while(ito.hasNext()){
String key = (String)ito.next();
int value = sh.get(key);
System.out.println("key --- "+key + ", value = "+value);
}
}
}
public static List<HashMap> getSel(){
List<HashMap> st = new ArrayList<HashMap>();
HashMap<String,Integer> sh =new HashMap<String,Integer>();
sh.put("tom", 4);
sh.put("jeny", 5);
sh.put("sanm", 5);
st.add(sh);
return st;
}
}
第二种,二维数组
第三种,hashmap或者hashtable,hashtable的存取要是用iterator迭代器。
关于hashmap的存取的实例我写的代码如下,希望对搂主有所帮助。
package com.sytdc.cxl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class HashTableTest {
public static void main(String args[]){
List<HashMap> st;
HashMap<String,Integer> sh ;
Set se;
st = getSel();
for(int i=0;i<st.size();i++){
sh =new HashMap<String,Integer>();
sh = st.get(i);
se = sh.keySet();
Iterator ito = se.iterator();
while(ito.hasNext()){
String key = (String)ito.next();
int value = sh.get(key);
System.out.println("key --- "+key + ", value = "+value);
}
}
}
public static List<HashMap> getSel(){
List<HashMap> st = new ArrayList<HashMap>();
HashMap<String,Integer> sh =new HashMap<String,Integer>();
sh.put("tom", 4);
sh.put("jeny", 5);
sh.put("sanm", 5);
st.add(sh);
return st;
}
}
#1
request.setAttribute("list",list);
在jsp
List list = request.getAttribute("list");
在jsp
List list = request.getAttribute("list");
#2
request.setAttribute("list", list)或session.setAttribute("list", list)
在jsp页面
ArrayList list = (ArrayList) request.getAttribute("list")或ArrayList list = (ArrayList) session.getAttribute("list")
在jsp页面
ArrayList list = (ArrayList) request.getAttribute("list")或ArrayList list = (ArrayList) session.getAttribute("list")
#3
request.getSession().setAttribute("listall",listall);
在jsp页面用:
<c:forEach items="${sessionScope.list}" var="arraylist">
……
……
</c:forEach>
在jsp页面用:
<c:forEach items="${sessionScope.list}" var="arraylist">
……
……
</c:forEach>
#4
在 跳转前 添加一个属性
request.setAttribute("属性名",list);
如果你的Booksession 是一个bean的话,你可以在jsp中用
<c:forEach var="item" items="${属性名}">
<tr>
<td>${item.性质}</td>
...
....
</tr>
</c:forEach>
另外还可以这样
<c:forEach var="item" items="${属性名}" >
<tr>
<c:forEach var="colValue" items="${item}">
<td>${colValue}</td>
</c:forEach>
</tr>
</c:forEach>
你先看看这个可以不,我改过。我感觉第二个比较通用吧!
request.setAttribute("属性名",list);
如果你的Booksession 是一个bean的话,你可以在jsp中用
<c:forEach var="item" items="${属性名}">
<tr>
<td>${item.性质}</td>
...
....
</tr>
</c:forEach>
另外还可以这样
<c:forEach var="item" items="${属性名}" >
<tr>
<c:forEach var="colValue" items="${item}">
<td>${colValue}</td>
</c:forEach>
</tr>
</c:forEach>
你先看看这个可以不,我改过。我感觉第二个比较通用吧!
#5
同意2楼的做法!
#6
OpenBlogAction.java文件,产生的数据放置在ArrayList中,存储数据
package cn.com.blogonline.blogmain;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import cn.com.blogonline.common.Constants;
import cn.com.blogonline.common.DbOperate;
import cn.com.blogonline.form.Blog;
public class OpenBlogAction extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
}
/*
* 处理<GET> 请求方法.
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置接收信息的字符集
request.setCharacterEncoding("UTF-8");
//接收浏览器端提交的信息
String blogId = request.getParameter("blogid");
int iBlogId = Integer.parseInt(blogId);
/*
*左侧框架页信息设置
*/
HttpSession session = request.getSession(true);
DbOperate db=new DbOperate();
Blog blog = db.getBlog(iBlogId);
blog.setVisitcount(blog.getVisitcount() + 1);
db.update(blog);
session.setAttribute(Constants.VISIT_BLOG_KEY,blog);
List sortList=db.getBlogSorts(iBlogId);
session.setAttribute(Constants.SORT_LIST_KEY,sortList);
List linkList=db.getBlogLinks(iBlogId);
session.setAttribute(Constants.LINK_LIST_KEY,linkList);
List articleList=db.getBlogArticles(iBlogId,0);
/*
* 分页显示
*/
List dispList=new ArrayList();
for (int i=0;i<articleList.size();i++){
if (i<Constants.ARTICLE_PAGE_SIZE){
dispList.add(articleList.get(i));
}
}
String forward = request.getContextPath()+Constants.BLOG_MAIN;
session.setAttribute(Constants.ARTICLE_LIST_KEY,dispList);
//session.setAttribute(Constants.CUR_BLOGID_KEY,new Integer(iBlogId));
session.setAttribute(Constants.CUR_PAGEID_KEY,new Integer(0));
session.setAttribute(Constants.CUR_SORTID_KEY,new Integer(0));
response.sendRedirect(forward);
}
}
jsp页面接受
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ page import="cn.com.blogonline.common.Constants"%>
<%@ page import="cn.com.blogonline.form.Article"%>
<%@ page import="java.util.*"%>
<%
List articleList = (List)session.getAttribute(Constants.ARTICLE_LIST_KEY);
Article article = null;
Integer tpage=(Integer)session.getAttribute(Constants.CUR_PAGEID_KEY);
int pageId=tpage.intValue();
Integer tSortId=(Integer)session.getAttribute(Constants.CUR_SORTID_KEY);
int sortId=tSortId.intValue();
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>显示主页面</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="StyleSheet" type="text/css" href="../css/mystyle.css">
</head>
<body>
<table border="1" width="550" bordercolor="#FF9900">
<tr>
<td width="100%">
<p align="right">
<b><font color="#FF0000"><a href="newArticle.jsp">发表文章</a></font></b>
</p>
</td>
</tr>
<%if(articleList != null ){
System.out.println(articleList.size());
for(int i = 0;i < articleList.size();i++)
{
article = (Article)articleList.get(i);
%>
<tr>
<td width="100%">
<b><font color="#FF0000"><%=article.getTitle()%></font></b>
<p>
<b><font color="#FF0000"><%=article.getContent()%></font></b>
</p>
<p align="right">
<b><font color="#FF0000"><a href="../showFeedback?articleid=<%=article.getId()%>">查看评论</a></font></b>
</p>
</td>
</tr>
<%
}
} %>
<tr>
<td width="100%">
<p align="center">
| <font color="#999999"> <a href="../blogOperate?pageid=<%=pageId-1%>">上页</a></font> | <a href="../blogOperate?pageid=<%=pageId+1%>">下页</a> |
</p>
</td>
</tr>
</table>
</body>
</html>
package cn.com.blogonline.blogmain;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import cn.com.blogonline.common.Constants;
import cn.com.blogonline.common.DbOperate;
import cn.com.blogonline.form.Blog;
public class OpenBlogAction extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
}
/*
* 处理<GET> 请求方法.
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置接收信息的字符集
request.setCharacterEncoding("UTF-8");
//接收浏览器端提交的信息
String blogId = request.getParameter("blogid");
int iBlogId = Integer.parseInt(blogId);
/*
*左侧框架页信息设置
*/
HttpSession session = request.getSession(true);
DbOperate db=new DbOperate();
Blog blog = db.getBlog(iBlogId);
blog.setVisitcount(blog.getVisitcount() + 1);
db.update(blog);
session.setAttribute(Constants.VISIT_BLOG_KEY,blog);
List sortList=db.getBlogSorts(iBlogId);
session.setAttribute(Constants.SORT_LIST_KEY,sortList);
List linkList=db.getBlogLinks(iBlogId);
session.setAttribute(Constants.LINK_LIST_KEY,linkList);
List articleList=db.getBlogArticles(iBlogId,0);
/*
* 分页显示
*/
List dispList=new ArrayList();
for (int i=0;i<articleList.size();i++){
if (i<Constants.ARTICLE_PAGE_SIZE){
dispList.add(articleList.get(i));
}
}
String forward = request.getContextPath()+Constants.BLOG_MAIN;
session.setAttribute(Constants.ARTICLE_LIST_KEY,dispList);
//session.setAttribute(Constants.CUR_BLOGID_KEY,new Integer(iBlogId));
session.setAttribute(Constants.CUR_PAGEID_KEY,new Integer(0));
session.setAttribute(Constants.CUR_SORTID_KEY,new Integer(0));
response.sendRedirect(forward);
}
}
jsp页面接受
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ page import="cn.com.blogonline.common.Constants"%>
<%@ page import="cn.com.blogonline.form.Article"%>
<%@ page import="java.util.*"%>
<%
List articleList = (List)session.getAttribute(Constants.ARTICLE_LIST_KEY);
Article article = null;
Integer tpage=(Integer)session.getAttribute(Constants.CUR_PAGEID_KEY);
int pageId=tpage.intValue();
Integer tSortId=(Integer)session.getAttribute(Constants.CUR_SORTID_KEY);
int sortId=tSortId.intValue();
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>显示主页面</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="StyleSheet" type="text/css" href="../css/mystyle.css">
</head>
<body>
<table border="1" width="550" bordercolor="#FF9900">
<tr>
<td width="100%">
<p align="right">
<b><font color="#FF0000"><a href="newArticle.jsp">发表文章</a></font></b>
</p>
</td>
</tr>
<%if(articleList != null ){
System.out.println(articleList.size());
for(int i = 0;i < articleList.size();i++)
{
article = (Article)articleList.get(i);
%>
<tr>
<td width="100%">
<b><font color="#FF0000"><%=article.getTitle()%></font></b>
<p>
<b><font color="#FF0000"><%=article.getContent()%></font></b>
</p>
<p align="right">
<b><font color="#FF0000"><a href="../showFeedback?articleid=<%=article.getId()%>">查看评论</a></font></b>
</p>
</td>
</tr>
<%
}
} %>
<tr>
<td width="100%">
<p align="center">
| <font color="#999999"> <a href="../blogOperate?pageid=<%=pageId-1%>">上页</a></font> | <a href="../blogOperate?pageid=<%=pageId+1%>">下页</a> |
</p>
</td>
</tr>
</table>
</body>
</html>
#7
ArrayList里面存储的数据,第一种,单个的String类型的字符串或者Object对象。
第二种,二维数组
第三种,hashmap或者hashtable,hashtable的存取要是用iterator迭代器。
关于hashmap的存取的实例我写的代码如下,希望对搂主有所帮助。
package com.sytdc.cxl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class HashTableTest {
public static void main(String args[]){
List<HashMap> st;
HashMap<String,Integer> sh ;
Set se;
st = getSel();
for(int i=0;i<st.size();i++){
sh =new HashMap<String,Integer>();
sh = st.get(i);
se = sh.keySet();
Iterator ito = se.iterator();
while(ito.hasNext()){
String key = (String)ito.next();
int value = sh.get(key);
System.out.println("key --- "+key + ", value = "+value);
}
}
}
public static List<HashMap> getSel(){
List<HashMap> st = new ArrayList<HashMap>();
HashMap<String,Integer> sh =new HashMap<String,Integer>();
sh.put("tom", 4);
sh.put("jeny", 5);
sh.put("sanm", 5);
st.add(sh);
return st;
}
}
第二种,二维数组
第三种,hashmap或者hashtable,hashtable的存取要是用iterator迭代器。
关于hashmap的存取的实例我写的代码如下,希望对搂主有所帮助。
package com.sytdc.cxl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class HashTableTest {
public static void main(String args[]){
List<HashMap> st;
HashMap<String,Integer> sh ;
Set se;
st = getSel();
for(int i=0;i<st.size();i++){
sh =new HashMap<String,Integer>();
sh = st.get(i);
se = sh.keySet();
Iterator ito = se.iterator();
while(ito.hasNext()){
String key = (String)ito.next();
int value = sh.get(key);
System.out.println("key --- "+key + ", value = "+value);
}
}
}
public static List<HashMap> getSel(){
List<HashMap> st = new ArrayList<HashMap>();
HashMap<String,Integer> sh =new HashMap<String,Integer>();
sh.put("tom", 4);
sh.put("jeny", 5);
sh.put("sanm", 5);
st.add(sh);
return st;
}
}