商务合作:179001057@qq.com

java ArrayList排序

xiaoxiao2022-06-24  4


某平台价值19860元的编程课程资料免费领取【点我领取】


http://hi.baidu.com/suofang/blog/item/76cbd962be93f4dae7113ad2.html

 

public class ListSort {

 public static void main(String args[]){  List list=new ArrayList();  Category c=new Category();  c.setCategoryName("服务展示");  c.setCategoryId(1);  c.setParId(0);    list.add(c);  c=new Category();  c.setCategoryId(2);  c.setCategoryName("用户注册");  c.setParId(0);  list.add(c);    c=new Category();  c.setCategoryId(3);  c.setCategoryName("劳动就业"); //二级目录  c.setParId(1);  list.add(c);  c=new Category();  c.setCategoryId(4);  c.setCategoryName("少儿保险");  c.setParId(1);  list.add(c);    c=new Category();  c.setCategoryId(5);  c.setCategoryName("信息发布");  c.setParId(0);  list.add(c);    c=new Category();  c.setCategoryId(7);  c.setCategoryName("新闻中心");  c.setParId(5);  list.add(c);    c=new Category();  c.setCategoryId(8);  c.setCategoryName("养老保险");  c.setParId(1);  list.add(c);  /*c=new Category();  c.setCategoryId(6);  c.setCategoryName("招工信息");//三级目录  c.setParId(3);  list.add(c);*/    CategoryComparator cc=new ListSort().new CategoryComparator();    Collections.sort(list,cc);          // 排序  Collections.reverse(list);    for(Object o:list){   Category ca=(Category)o;   System.out.println(ca.getParId()+"------"+ca.getCategoryName());  } }  private class CategoryComparator implements Comparator{       //实现排序算法

   public int compare(Object o1, Object o2) {   Category c1=(Category)o1;   Category c2=(Category)o2;     if(c1.getParId()==c2.getParId()){    return 0;   }   else if(c1.getParId()>c2.getParId()){    return c1.getParId();   }  return c2.getParId(); //实现了二级目录,三级目录就不行了 }}}

 

---------------------------

public class ListSort {       List sortList=new ArrayList(); //用递归会增加一培的内存     static  List  list=new ArrayList(); public static void main(String args[]){  ListSort ls=new ListSort();  Category c=new Category();  c.setCategoryName("服务展示");  c.setCategoryId(1);  c.setParId(0);    list.add(c);  c=new Category();  c.setCategoryId(2);  c.setCategoryName("用户注册");  c.setParId(0);  list.add(c);    c=new Category();  c.setCategoryId(3);  c.setCategoryName("劳动就业"); //二级目录  c.setParId(1);  list.add(c);  c=new Category();  c.setCategoryId(4);  c.setCategoryName("少儿保险");  c.setParId(1);  list.add(c);    c=new Category();  c.setCategoryId(5);  c.setCategoryName("信息发布");  c.setParId(0);  list.add(c);    c=new Category();  c.setCategoryId(7);  c.setCategoryName("新闻中心");  c.setParId(5);  list.add(c);    c=new Category();  c.setCategoryId(8);  c.setCategoryName("养老保险");  c.setParId(1);  list.add(c);  c=new Category();  c.setCategoryId(6);  c.setCategoryName("招工信息");//三级目录  c.setParId(3);  list.add(c);

ls.tree(0);  for(Object o:ls.sortList){   Category cc=(Category)o;   System.out.println("id: "+cc.getCategoryId()+"  par: "+cc.getParId()+"  name: "+cc.getCategoryName());  } }

 public void tree(int n){   int size=list.size();  for(int i=0;i<size;i++){   Category c=(Category)list.get(i);   if(c.getParId()==n){    sortList.add(c);    tree(c.getCategoryId());               //自调用,递归   }  } }}

显示如下

  id: 1  par: 0  name: 服务展示id: 3  par: 1  name: 劳动就业id: 6  par: 3  name: 招工信息id: 4  par: 1  name: 少儿保险id: 8  par: 1  name: 养老保险id: 2  par: 0  name: 用户注册id: 5  par: 0  name: 信息发布id: 7  par: 5  name: 新闻中心

 

相关资源:java的arraylist排序示例(arraylist用法)

最新回复(0)