同级目录排序问题,置顶、置底、上移、下移 Java
2021/9/16 22:04:54
本文主要是介绍同级目录排序问题,置顶、置底、上移、下移 Java,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
参考代码实现如下(还需优化):
```java public class SortUtils { // 当前数据节点 private BusinessSystemFunctionData current; // 兄弟节点数据 private List<BusinessSystemFunctionData> brother; //以当前节点分割,当前之前的数据 private List<BusinessSystemFunctionData> beforeList; //以当前节点分割,当前之后的数据 private List<BusinessSystemFunctionData> afterList; // 当前节点的下标 private int currintIndex; // 兄弟节点是否包含当前节点 private boolean constan; public SortUtils(BusinessSystemFunctionData businessSystemFunctionData, List<BusinessSystemFunctionData> list) { this.current = businessSystemFunctionData; this.brother = list; check(); } /** * 以当前节点,把兄弟数据分为两部分,分为前后,方便置顶和置顶操作 */ public void split() { this.beforeList = this.brother.subList(0, currintIndex); this.afterList = this.brother.subList(currintIndex + 1, this.brother.size()); } /** * 校验数据的合法性 */ private void check() { if (null == this.current || null == this.brother) { throw new IllegalArgumentException("非法参数,请初始化参数"); } for (int i = 0; i < brother.size(); i++) { if (this.current.getId().equals(brother.get(i).getId())) { currintIndex = i; constan = true; break; } } if (!constan || currintIndex >= this.brother.size()) { throw new IllegalArgumentException("非法参数,初始化参数,有误,兄弟节点不包含当前节点"); } } /** * 置顶 * * @return */ public List<BusinessSystemFunctionData> toTop() { List<BusinessSystemFunctionData> list = new ArrayList<>(brother.size()); list.add(this.current); list.addAll(beforeList); list.addAll(afterList); for (int i = 0; i < list.size(); i++) { list.get(i).setSort((i + 1)); } return list; } /** * 置底 * * @return */ public List<BusinessSystemFunctionData> toBottom() { List<BusinessSystemFunctionData> list = new ArrayList<>(brother.size()); list.addAll(beforeList); list.addAll(afterList); list.add(this.current); for (int i = 0; i < list.size(); i++) { list.get(i).setSort((i + 1)); } return list; } /** * 上移 * * @return */ public List<BusinessSystemFunctionData> toUp() { if (currintIndex > 0) { BusinessSystemFunctionData businessSystemFunctionData = this.brother.get(currintIndex - 1); this.brother.set(currintIndex - 1, current); this.brother.set(currintIndex, businessSystemFunctionData); } for (int i = 0; i < this.brother.size(); i++) { this.brother.get(i).setSort((i + 1)); } return this.brother; } /** * 下移动 * * @return */ public List<BusinessSystemFunctionData> toDown() { if (currintIndex < brother.size() - 1) { BusinessSystemFunctionData businessSystemFunctionData = this.brother.get(currintIndex + 1); this.brother.set(currintIndex + 1, current); this.brother.set(currintIndex, businessSystemFunctionData); } for (int i = 0; i < this.brother.size(); i++) { this.brother.get(i).setSort((i + 1)); } return this.brother; } }
这篇关于同级目录排序问题,置顶、置底、上移、下移 Java的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-05小米13T Pro系统合集:性能与摄影的极致融合,值得你升级的系统ROM
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求
- 2024-10-01使用 golang 将ETH账户的资产平均分散到其他账户
- 2024-10-01JWT用户校验课程:从入门到实践
- 2024-10-01Server Component课程入门指南