Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 在ArrayList中添加项目<;对象>;在一些项目之后_Arrays_Algorithm_Sorting_Kotlin - Fatal编程技术网

Arrays 在ArrayList中添加项目<;对象>;在一些项目之后

Arrays 在ArrayList中添加项目<;对象>;在一些项目之后,arrays,algorithm,sorting,kotlin,Arrays,Algorithm,Sorting,Kotlin,我在Kotlin上编程 我从sql中获得一个ArrayList(子项对象的ArrayList),其中包含以下数据,始终按路径索引排序: 我正试图处理这个问题,以便在ArraList中添加特定的寄存器。我需要将它始终放在lastRowFromParent等于1的下一行中,但如果下一行的字段lvl大于当前值,我必须在字段lvl变小时这样做,为每个值为1的前一个lastRowFromParent添加一个寄存器,如下图所示: 我目前的代码是: var antLevel = 0 var

我在Kotlin上编程

我从sql中获得一个ArrayList(子项对象的ArrayList),其中包含以下数据,始终按路径索引排序:

我正试图处理这个问题,以便在ArraList中添加特定的寄存器。我需要将它始终放在lastRowFromParent等于1的下一行中,但如果下一行的字段lvl大于当前值,我必须在字段lvl变小时这样做,为每个值为1的前一个lastRowFromParent添加一个寄存器,如下图所示:

我目前的代码是:

    var antLevel = 0
    var level = 0
    var counterNewRow = 0
    var fullSubItemsList = ArrayList<SubItems>() //variable where I'm trying to write the objects as in second image
    var fullSubItemsSizeAux = 0

    // subItemsList contains all the data from sql, like in first image.

    subItemsList.forEachIndexed { index, subItems ->
        if (!subItems.prioritizeButton) {
            level = subItems.lvl

            fullSubItemsList.add(subItems) 
            if (subItems.lastRowChild) {
                counterNewRow++
                if (antLevel > level) {
                    while (counterNewRow>0) {
                        fullSubItemsList.add(
                            index+fullSubItemsSizeAux,   //  I did it to fix index position while the fullSubItemsList is incresing more than subItemsList
                            SubItems(
                                subItems.parentId,
                                subItems.lvl,
                                subItems.path_index.replaceAfterLast('.', "X")
                            ) //it creates an object in that format: id=0, item_name='', parentId=[prev.], lvl=[prev], path_index=[prev], lastRowFromParent=0
                        )
                        fullSubItemsSizeAux++
                        counterNewRow--
                    }
                }
            }
            antLevel = level
        }
    }
var antLevel=0
变量级别=0
var counterNewRow=0
var fullSubItemsList=ArrayList()//变量,我试图在其中写入第二个映像中的对象
var fullSubItemsSizeAux=0
//subItemsList包含来自sql的所有数据,如第一个图像中所示。
subItemsList.foreachinedexed{索引,子项->
如果(!子项.优先级按钮){
级别=子项.lvl
fullSubItemsList.add(子项)
if(子项.lastRowChild){
counterNewRow++
如果(水平>水平){
while(counterNewRow>0){
fullSubItemsList.add(
index+fullsubitemsizeaux,//我这样做是为了固定索引位置,而fullSubItemsList的增量大于subItemsList
分项(
subItems.parentId,
subItems.lvl,
subItems.path_index.replaceAfterLast('.',X“)
)//它以这种格式创建一个对象:id=0,item_name='',parentId=[prev.],lvl=[prev],path_index=[prev],lastRowFromParent=0
)
fullSubItemsSizeAux++
counterNewRow--
}
}
}
水平
}
}

如何才能做到这一点?

您需要更改现有方法,以依赖parentId列来标识.X行。下面是一个分步算法:

  • 维护ParentID的集合和堆栈

  • 读取每个parentId并检查其是否在集合中:

    案例编号-不在集合中,将其添加到集合中并推送堆栈

    案例是-它在集合中,不断从堆栈弹出,直到到达parentId。继续从集合中删除弹出的id,并继续将“.X”添加到列表中

  • 下面是一个试运行:

    parentId 0 - add to stack, add current record to your list
    
    parentId 17 - add to stack, add current record to your list
    
    parentId 18 - add to stack, add current record to your list
    
    parentId 20 - add to stack, add current record to your list
    
    parentId 24 - add to stack, add current record to your list
    
    parentId 32 - add to stack, add current record to your list
    
    parentId 33 - add to stack, add current record to your list
    
    parentId 53 - add to stack, add current record to your list
    
    parentId 54 - add to stack, add current record to your list
    
    parentId 55 - add to stack, add current record to your list
    
    parentId 18 - already in stack, start popping until you reach 18. Pop 55, 54, 53, 33, 32, 24 and 20 add them to your list with ".X". Then add current record to your list
    
    parentId 25 - add to stack, add current record to your list
    
    parentId 18 - already in stack, start popping until you reach 18. Pop 25, add it to your list with ".X". Then add current record to your list
    
    parentId 17 - already in stack, start popping until you reach 17. Pop 18, add it to your list with ".X". Then add current record to your list
    
    parentId 17 - already in stack, start popping until you reach 17. No items to pop so add current record to your list
    
    parentId 0 - already in stack, start popping until you reach 0. Pop 17, add it to your list with ".X". Then add current record to your list
    
    parentId 22 - add to stack, add current record to your list.
    
    parentId 0 - already in stack, start popping until you reach 0. Pop 22, add it to your list with ".X". Then add current record to your list
    
    parentId 0 - already in stack, start popping until you reach 0. No items to pop so add current record to your list
    
    parentId 0 - already in stack, start popping until you reach 0. No items to pop so add current record to your list
    
    parentId 0 - already in stack, start popping until you reach 0. No items to pop so add current record to your list
    
    parentId 0 - already in stack, start popping until you reach 0. No items to pop so add current record to your list
    
    parentId 0 - already in stack, start popping until you reach 0. No items to pop so add current record to your list
    
    parentId 0 - already in stack, start popping until you reach 0. No items to pop so add current record to your list
    
    parentId 0 - already in stack, start popping until you reach 0. No items to pop so add current record to your list
    
    parentId 0 - already in stack, start popping until you reach 0. No items to pop so add current record to your list
    
    Add "X" record when end.
    

    对不起,我不明白。。。我必须只使用parentId创建arraylist,并与sql中的arraylist进行比较?按照你告诉我的方式,我不需要lastRowFromParent专栏?谢谢你的编辑和帮助!在您的解释中可能还包括另一件事,即在运行“我的所有列表”后添加一个新循环,因为如果我在final中有子项,它们将保留在堆栈上。;)