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