andrew203 2014-03-31 07:21:05
List<A> List<B> A集合里面有 int id ,String dairytime, String title三个属性
B集合里面有int id,String pictime,String path三个属性
新List<C> C中包含了 String time String title String path
要求 比较2个dairytime 和pictime的大小 如果 dairytime小的话 就将C c.settime(dairytime) c.settile(title) c.setpath("");
如果 pictime小的话 就将C c.settime(pictime) c.settitle("") c.setpath(path);
依次类推形成新的list<C>集合。。。。。不知道描述的各位懂了没有
...全文
358 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
温文骏 2014-04-01
  • 打赏
  • 举报
回复
直接粘贴就可以用,已经测试过
温文骏 2014-04-01
  • 打赏
  • 举报
回复
package demo; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.Random; public class Baidu { public static void main(String[] args) { Random random = new Random(); ArrayList<A> alist = setA(random,"atitle"); ArrayList<B> blist = setB(random,"D:\\b.txt"); try { for (C c : getCList(alist, blist)) { System.out.println("Time:" + c.getTime() + "\n" + "Path:" + c.getPath() + "\n" + "Title:" + c.getTitle()); System.out.println(); } } catch (Exception e) { e.printStackTrace(); } } // 获得相同id的A,B对象,比较他们的时间,从来获取C集合 public static ArrayList<C> getCList(ArrayList<A> alist, ArrayList<B> blist) throws Exception { ArrayList<C> clist = new ArrayList<C>(); for (int i = 0; i < alist.size(); i++) { for (int j = 0; j < blist.size(); j++) { if (alist.get(i).getId() == blist.get(j).getId()) { String dairytime = alist.get(i).getDairytime(); String pictime = blist.get(j).getPictime(); C c = new C(); // 如果A的dairytime更小 如:2014-03-25 15:37:38 if (stringToDate(dairytime).before(stringToDate(pictime))) { c.setTime(dairytime); c.setTitle(alist.get(i).getTitle()); c.setPath(""); // 如果B的pictime更小 如:2014-03-26 03:03:33 } else if (stringToDate(dairytime).after( stringToDate(pictime))) { c.setTime(pictime); c.setTitle(""); c.setPath(blist.get(j).getPath()); } clist.add(c); } } } return clist; } // 设置A集合 public static ArrayList<A> setA(Random random,String title) { ArrayList<A> alist = new ArrayList<A>(); // 执行十遍,像alist添加十个元素 for (int i = 0; i < 10; i++) { A a = new A(); // 将A对象的dairytime都设置为随机生成的,不过生成的时间有可能不符合逻辑 a.setId(i); a.setDairytime((2000+random.nextInt(16)) + "-" + (1+random.nextInt(12)) + "-"// 年 月 + random.nextInt(31) + " " + random.nextInt(25) + ":"//日 时 + random.nextInt(61) + ":" + random.nextInt(61));//分 秒 a.setTitle(title); alist.add(a); } return alist; } // 设置B集合 public static ArrayList<B> setB(Random random,String path) { ArrayList<B> blist = new ArrayList<B>(); // 执行十遍,像alist添加十个元素 for (int i = 0; i < 10; i++) { B b = new B(); // 将B对象的id pictime都设置为随机生成的,不过生成的时间有可能不符合逻辑 b.setId(i); b.setPictime((2000+random.nextInt(15)) + "-" + (1+random.nextInt(12)) + "-"// 年 月 + random.nextInt(31) + " " + random.nextInt(25) + ":"//日 时 + random.nextInt(61) + ":" + random.nextInt(61));//分 秒 b.setPath(path); blist.add(b); } return blist; } // 将时间字符串转换为Date,用于比较 public static Date stringToDate(String time) throws Exception { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.parse(time); } } class A { private int id; private String dairytime; private String title; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getDairytime() { return dairytime; } public void setDairytime(String dairytime) { this.dairytime = dairytime; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } } class B { private int id; private String pictime; private String path; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getPictime() { return pictime; } public void setPictime(String pictime) { this.pictime = pictime; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } } class C { private String time; private String title; private String path; public String getTime() { return time; } public void setTime(String time) { this.time = time; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } }
机器学习之禅 2014-04-01
  • 打赏
  • 举报
回复
都描述这么清楚,lz是什么意思
andrew203 2014-04-01
  • 打赏
  • 举报
回复
//===================尝试一下生成新的list================= listPd=new ArrayList<PicDai>();//生成的新list list = dao.queryDairy(username);//日志的list photolist=pdao.queryPhoto(username);//照片的list //===========判断第一种可能性当日记不为空,照片为空================ if(photolist.size()==0&&list.size()!=0){ for(Dairy ds:list){ PicDai pd1=new PicDai(); pd1.setTitle(ds.getTitle()); pd1.setPath(""); pd1.setTime(ds.getDsdate()); pd1.setUsername(ds.getUsername()); listPd.add(pd1); } } //判断第二种可能性日记为空照片不为空 else if(photolist.size()!=0&&list.size()==0){ for(photobean pbs:photolist){ PicDai pd1=new PicDai(); pd1.setTitle(""); pd1.setPath(pbs.getPath()); pd1.setTime(pbs.getName()); pd1.setUsername(pbs.getUsername()); listPd.add(pd1); } } else{ for(Dairy dairys:list){ for(photobean ps:photolist){ //根据时间去判断从而生成新的list String pictime=ps.getName();//获取照片的时间 String titletime=dairys.getDsdate(); PicDai pds=new PicDai(); try { Date picdate=sd1.parse(pictime); Date titledate=sd1.parse(titletime); long comparer=titledate.getTime()-picdate.getTime(); if(comparer<0){ pds.setTitle(dairys.getTitle());//将title放入新的list集合 pds.setTime(dairys.getDsdate()); pds.setUsername(dairys.getUsername()); pds.setPath(""); // criticl.setText(info.getDsdate()); // 获取心情日记的title // content.setText(info.getTitle()); } else{ pds.setPath(ps.getPath()); pds.setTime(ps.getName()); pds.setUsername(ps.getUsername()); pds.setTitle(""); // criticl.setText(info1.getName()); //iv_picture.setImageBitmap(bmap); } } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } listPd.add(pds); } } }
軒轅劍 2014-04-01
  • 打赏
  • 举报
回复
引用 4 楼 huxiweng 的回复:
没有快捷方法的。 按照你描述的思路循环判断即可。 如 for for if(dairytime > pictime){ xxx }else{ xxxx }
循环判断吧
teemai 2014-04-01
  • 打赏
  • 举报
回复
没有快捷方法的。 按照你描述的思路循环判断即可。 如 for for if(dairytime > pictime){ xxx }else{ xxxx }
hh_ll 2014-04-01
  • 打赏
  • 举报
回复
你这不是明白了吗? 描述的挺明白了
wangcheng1983913 2014-03-31
  • 打赏
  • 举报
回复
集合是用哪个java类声明的,请说明一下。
tony4geek 2014-03-31
  • 打赏
  • 举报
回复
你都理解 。安需求做。比较 填数据 。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧