ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

同级目录排序问题,置顶、置底、上移、下移 Java

2021-09-16 21:34:28  阅读:327  来源: 互联网

标签:Java List list brother current currintIndex 置底 置顶 size


在这里插入图片描述

参考代码实现如下(还需优化):


```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,List,list,brother,current,currintIndex,置底,置顶,size
来源: https://blog.csdn.net/qq_30801117/article/details/120337760

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有