Kotlin将Arraylist拆分为多个索引处的部分
假设我有一个ArraylistKotlin将Arraylist拆分为多个索引处的部分,arraylist,kotlin,split,slice,Arraylist,Kotlin,Split,Slice,假设我有一个Arraylist fun main(){ var myarrayList=ArrayList() myarrayList.add(“+23”) myarrayList.add(“-25”) myarrayList.add(“+125”) myarrayList.add(“+455”) myarrayList.add(“”) myarrayList.add(“*230”) myarrayList.add(“-293”) myarrayList.add(“/6”) myarrayLis
fun main(){
var myarrayList=ArrayList()
myarrayList.add(“+23”)
myarrayList.add(“-25”)
myarrayList.add(“+125”)
myarrayList.add(“+455”)
myarrayList.add(“”)
myarrayList.add(“*230”)
myarrayList.add(“-293”)
myarrayList.add(“/6”)
myarrayList.add(“”)
myarrayList.add(“+293”)
myarrayList.add(“”)
myarrayList.add(“+21”)
for(索引为0..myarrayList.size-1){
打印(索引)
打印(“=”)
打印(myarrayList.get(索引))
打印(“\n”)
} }
我想在每个索引的值为“”的地方将其拆分为更小的ArrayList。我知道我可以使用indexOf()和lastIndexOf对其进行切片,但我只能得到2个切片。我需要拆分成多个数组您肯定可以使用。arrayList的子列表函数如下:
fun subList(fromIndex: Int, toIndex: Int): MutableList<E>
fun子列表(从index:Int到index:Int):可变列表
您可以使用多种收集功能组合来解决问题。 首先,您应该找到带有空字符串()的索引: 接下来,您可以使用从列表中连续获取两个项目。要获取列表的第一部分和最后一部分,您需要添加-1和
数组列表的大小。现在,您可以使用和提取独立部分:
val splittedList =
(listOf(-1) + splitPoints + myarrayList.size)
.windowed(2, 1)
.map { (start, end) -> myarrayList.subList(start + 1, end) }
我对Kotlin/Anko很陌生,我知道你的答案涉及RXKotlin。我从来没有听说过《窗户》杂志,但我查了一下。要学的更多。然而,我已经能够通过以下代码实现同样的功能。我非常感谢大家的帮助
主要内容(){
var myarrayList=ArrayList()
var emptyIndex=ArrayList()
EmptyIndex.add(0)
myarrayList.add(“+23”)
myarrayList.add(“-25”)
myarrayList.add(“+125”)
myarrayList.add(“+455”)
myarrayList.add(“”)
myarrayList.add(“*230”)
myarrayList.add(“-293”)
myarrayList.add(“”)
myarrayList.add(“+293”)
myarrayList.add(“”)
myarrayList.add(“+21”)
for(索引为0..myarrayList.size-1){
打印(索引)
打印(“=”)
打印(myarrayList.get(索引))
打印(“\n”)
///查找空索引
if(myarrayList.get(index)==“”){
EmptyIndex.add(索引)
}
}
EmptyIndex.add(myarrayList.size)
打印(“\n”)
对于(EmptyIndex中的项目){
打印(“空索引”+项+”)
}
for(索引为0..emptyIndex.size-1){
//println(“开始索引”+索引)
如果(index==emptyIndex.size-1){break}
//println(“结束索引”+(索引+1))
//打印(“\n\nemptyindix大小)”+emptyIndex.size+“\n\n”)
var subcls=myarrayList.subList(emptyIndexes[index],emptyIndexes[(index+1)])
//println(子类)
var stToCal=“”
用于(子类别中的项目){
stToCal=stToCal+items.toString()
}
}
}我无法想象没有更简单的方法来实现这一点。。。实际上有很多方法可以解决这个问题,但都有一个或另一个“缺陷”(例如,你需要在前面或后面添加一些索引……或者你需要注意不要丢失最后一个条目,因为没有最终的“
”,等等)。如果您一开始就不需要列表,那么它可以简单到:
myarrayList.joinToString("") {
if (it == "") "\n" else it
}
它基本上只返回一个字符串
,如下所示:
+23-25+125+455
*230-293/6
+293
+21
也许这给了你们一个提示,还有什么其他的可能已经有了。。。现在仍在使用,但只是相应地将其拆分(显然是一种解决方法):
另一种方法,没有-解决方法,而是使用与以前相同的打印方法:
myarrayList.mapIndexedNotNull { i, s -> i.takeIf { s == "" } }
.let {
listOf(-1) + it + myarrayList.size // ugly, but we need beginning and end too, not only the delimiting indices
}
.asSequence()
.zipWithNext { from, upto -> myarrayList.subList(from + 1, upto) }
.forEach(::println)
你也可以在适当的地方建造一些东西。。。但是,请确保同时收集列表的最后一个条目;-) 我在搜索同样的东西,结果我为列表创建了自己的扩展函数
fun <E> List<E>.split(separator: E): List<List<E>> =
mutableListOf<List<E>>().also { result ->
var end = -1
var remaining = this
while (end < remaining.size) {
remaining = remaining.drop(end + 1)
end = remaining.indexOf(separator).let {
if (it == -1) remaining.size else it
}
result.add(remaining.subList(0, end))
}
}
fun List.split(分隔符:E):List=
mutableListOf()。同时{result->
var end=-1
var剩余=此
while(结束<剩余大小){
剩余=剩余。下降(结束+1)
end=剩余的.indexOf(分隔符).let{
如果(it==-1)剩余,则调整大小,否则调整
}
结果.添加(剩余的.子列表(0,结束))
}
}
我实际上使用了一个粗略版本的子列表来管理它,但它非常混乱。我想可能有更干净的方法。顺便说一句,谢谢你的回复,罗兰,你的回答非常清楚。我曾经考虑过使用JoinToString,我认为这可能是最干净的方法。在上面的例子中,我并不是只使用一个数字。这是一种计算器应用程序,有一个运行列表,就像老式桌面上的纸卷一样。Arraylist中的每一行将表示计算中的一行。我e数据类行项(行号、值、注释)以及。。。可能是最可读的,但如果您首先不需要连接字符串,这也是一种解决方法;-)这应该是一个公认的答案。我添加了一个可空的分隔符:funlist.split(分隔符:E?):List。现在,您可以使用它来分隔空元素处的列表。例如:val list=listOf(1,2,null,4,5,6)list。拆分(null)打印:[[1,2],[4,5,6]]
+23-25+125+455
*230-293/6
+293
+21
myarrayList.joinToString("|") {
if (it == "") "FORM_END" else it
}.splitToSequence("|FORM_END|") // use split if List<*> is ok for you
.map {
it.splitToSequence("|") // use split if *<List<String>> is ok for you
}
[+23, -25, +125, +455]
[*230, -293, /6]
[+293]
[+21]
myarrayList.mapIndexedNotNull { i, s -> i.takeIf { s == "" } }
.let {
listOf(-1) + it + myarrayList.size // ugly, but we need beginning and end too, not only the delimiting indices
}
.asSequence()
.zipWithNext { from, upto -> myarrayList.subList(from + 1, upto) }
.forEach(::println)
fun <E> List<E>.split(separator: E): List<List<E>> =
mutableListOf<List<E>>().also { result ->
var end = -1
var remaining = this
while (end < remaining.size) {
remaining = remaining.drop(end + 1)
end = remaining.indexOf(separator).let {
if (it == -1) remaining.size else it
}
result.add(remaining.subList(0, end))
}
}