Java教程

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

本文主要是介绍同级目录排序问题,置顶、置底、上移、下移 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的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!