读思码

日常记录


深度遍历和广度遍历

<p>深度遍历?</p> <pre><code> public List&amp;lt;Category&amp;gt; tracedCategoryList(long categoryId) { List&amp;lt;Category&amp;gt; list = new ArrayList&amp;lt;Category&amp;gt;(); Category bean = categoryDao.getCategoryById(categoryId); list.add(bean); long pid = bean.getParent().getId(); while (pid &amp;gt; 0) { Category temp = categoryDao.getCategoryById(pid); list.add(temp); pid = temp.getParent().getId(); } return list; } /** * 获取子分类 * @param categoryId * @param recursive 是否递归 * @return */ public List&amp;lt;Category&amp;gt; getSubCategoryList(long categoryId, boolean recursive) { List&amp;lt;Category&amp;gt; list = new ArrayList&amp;lt;Category&amp;gt;(); List&amp;lt;Category&amp;gt; beans = categoryDao.getCategoriesByParent(categoryId); if (beans != null &amp;amp;&amp;amp; beans.size() &amp;gt; 0) { list.addAll(beans); if (recursive) { for (Category c : beans) { List&amp;lt;Category&amp;gt; ss = this.getSubCategoryList(c.getId(), true); if (!ss.isEmpty()) { list.addAll(ss); } } } } return list; }</code></pre> <p>广度遍历?</p> <pre><code> List&amp;lt;Long&amp;gt; list = new ArrayList&amp;lt;Long&amp;gt;(); list.add(id); for (int i = 0; i &amp;lt; list.size(); ++i) { List&amp;lt;Category&amp;gt; beans = categoryDao.getCategoriesByParent(list.get(i)); for (Category bean : beans) { if (newParentId == bean.getId()) throw new ServiceException(ResourceProperty .getCannotMoveCategoryString()); list.add(bean.getId()); } }</code></pre>

页面列表

ITEM_HTML