请教各位大大,关于使用collection的问题。

wzy19514 2005-03-23 10:03:41
我想在collection里在装一些collection,请问我怎么把装在collection里的collection取出来,这是我装collection的代码:
Collection Rere = new ArrayList();
while (rst.next()) {
Collection Redata = new ArrayList();
ItemName ItemNa = new ItemName();
ItemNa.setIName(rst.getString(2));
Redata.add(ItemNa);
}
Rere.addAll(Redata);}请问我怎么把装在collection(Rere)里的collection(Redata)取出来,麻烦写一下代码,辛苦了,谢谢
...全文
58 点赞 收藏 3
写回复
3 条回复
wzy19514 2005年03月23日
谢谢,大哥大
回复 点赞
jzcbz 2005年03月23日
老大,你类型不一致,当然会出错呢
public Collection find(int NewsID){
PreparedStatement ps=null;
ResultSet rs=null;
ArrayList list = new ArrayList();
String sql = "select a.Id,a.content,a.reviewtime,b.UserName,c.Subject,a.publish "+
"from news_review a,users b,news c "+
"where a.writer = b.LoginID and a.NewsID = c.NewsID and a.NewsID=? and a.writer <> 'guest' " +
"union "+
"select a.Id,a.content,a.reviewtime,a.writer as UserName,c.Subject,a.publish "+
"from news_review a,news c "+
"where a.NewsID = c.NewsID and a.NewsID=? and a.writer = 'guest' " ;
try{
if (con.isClosed()){
throw new IllegalStateException("error.unexpected");
}
ps = con.prepareStatement(sql);
ps.setInt(1,NewsID);
ps.setInt(2,NewsID);
rs = ps.executeQuery();
while (rs.next()){
NewsReview newsreview = new NewsReview();
newsreview.setId(rs.getInt("Id"));
newsreview.setSubject(rs.getString("subject"));
newsreview.setReviewtime(rs.getString("reviewtime"));
newsreview.setContent(rs.getString("content"));
newsreview.setUsername(rs.getString("UserName"));
newsreview.setPublish(rs.getString("publish"));
list.add(newsreview);
}
return list;
}catch(SQLException e){
throw new RuntimeException("error.unexpected");
}finally{
try{
if (ps!=null){
ps.close();
}
if (rs!=null){
rs.close();
}
}catch(SQLException e){
throw new RuntimeException("error.unexpected");
}
}
}
回复 点赞
awaysrain 2005年03月23日

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class Test {

public static void main(String[] args) {
Collection Rere = new ArrayList();
Collection tmp = new ArrayList();
tmp.add("1");
tmp.add("2");
tmp.add("3");
Rere.add(tmp);
Iterator it = Rere.iterator();
while(it.hasNext()){
Collection Redata = (Collection)it.next();
Iterator itRedata = Redata.iterator();
while(itRedata.hasNext()){
System.out.println(itRedata.next());

}
}

}
}
回复 点赞
发动态
发帖子
Web 开发
创建于2007-09-28

5.2w+

社区成员

34.1w+

社区内容

Java Web 开发
社区公告
暂无公告