Java Simple infinite-pole classification

Source: Internet
Author: User

This is a relatively simple infinite-pole classification, suitable for a variety of programming languages.

This principle is a one-time database of all categories read out and then according to the PID to sort.

Relatively simple, good understanding.

public static list<category> cates;private static list<category>  Tmpcates = new arraylist<category> ();p Ublic static void main (String[]  args)  {    init ();    list<category> clist  = tree ( cates , 0 , 0 );    for  (category c: CList)  {        StringBuffer sb = new  StringBuffer ();        for  (int i = 0; i  < c.getlevel ();  i++) {             sb.append ("---");        }         system.out.println ( sb.tostring ()  + c.getname ()  );     }} Public static list<category> tree ( list<category> cs , integer pid , integer level  ) {    for  (CATEGORY&NBSP;C:&NBSP;CS)  {         if ( c.getparent_id ()  == pid ) {             c.setlevel ( level );             tmpcates.add (c);             tree ( cs , c.getid ()  , level+1 );         }     }    return tmpcates;} Public static void init () {    cates = new arraylist< Category> ();     category c1 = new category ();     c1.setid (1);     c1.setname ("Guangdong");     c1.setparent_id (0);     cates.add (C1);     c1 =  new category ();     c1.setid (2);     c1.setname ("Shaanxi");     c1.setparent_id (0);     cates.add (C1);     c1 =  new category ();     c1.setid (3);     c1.setname ("Sichuan");     c1.setparent_id (0);     cates.add (C1);     c1 =  new category ();     c1.setid (5);     c1.setname ("Shenzhen");  &NBSP;&NBSP;&NBSP;C1.SETPARENT_ID (1);     cates.add (C1);     c1 =  new category ();     c1.setid (6);     c1.setname ("Shaanxi");  &NBSP;&NBSP;&NBSP;C1.SETPARENT_ID (2);     cates.add (C1);     c1 =  new categorY ();     c1.setid (8);     c1.setname ("Chengdu");     C1.SETPARENT_ID (3);     cates.add (C1);    c1 = new  Category ();     c1.setid (9);     c1.setname ("Baoji");    &NBSP;C1.SETPARENT_ID (2);     cates.add (C1);}

The main method is the tree () function

Java Simple infinite-pole classification

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.