根据过滤器将数组分为两个 [英] Divide an Array into two according to a filter
本文介绍了根据过滤器将数组分为两个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的销售书籍的应用程序,当用户购买书籍时,会将pdf文件下载到手机的内存中.我有两类书籍,并且pdf文件的名称中有一个用于识别它的代码.
My app selling books, when a user buys a book, a pdf file is downloaded to the internal memory of the phone. I have two categories of books and the name of the pdf file has a code to identify it.
在所有已购买并因此下载的文件中,我具有以下代码来获取所有已购买书籍的数组:
Of all the files purchased and therefore downloaded, I have the following code to obtain an array of all the books purchased:
val purchasedBooks: ArrayList<String> = ArrayList()
val directoryFiles = context?.filesDir
val files = directoryFiles?.listFiles()
if (files != null) {
for (aFile in files) {
purchasedBooks.add(aFile.name)
}
}
此代码在purchasedBooks
中返回我:
[ssbook1.pdf, ssbook2.pdf, ssbook3.pdf, aabook1.pdf, aabook2.pdf]
我需要这样:
val ssbooks = [ssbook1, ssbook2, ssbook3] // without .pdf
val aabooks = [aabook1, aabook2]
然后按部分在recyclerView中显示它:
To then show it in a recyclerView by sections:
________________
MY SS BOOK
________________
ssbook1
----------------
ssbook2
----------------
ssbook3
________________
MY AABOOK
________________
aabook1
----------------
aabook2
任何建议,我将不胜感激.
Any advice I will be grateful.
推荐答案
希望以下代码帮助.
val arr = listOf("ssbook1.pdf", "ssbook2.pdf", "ssbook3.pdf", "aabook1.pdf", "aabook2.pdf")
val result = arr.map {it.substringBefore(".pdf")}
.groupBy {it.contains("ssbook")}
.map { it.value}
println(result) // [[ssbook1, ssbook2, ssbook3], [aabook1, aabook2]]
您可以使用结果2D数组在回收站视图中显示部分
You can use the result 2D array to show sections in recycler view
这篇关于根据过滤器将数组分为两个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文