嘿,我有一个对象列表,其中我想将title property值组合成对象列表。我做得很成功,没有任何问题。我的主要问题是,在kotlin中有什么更好的方法可以做,这有助于高效、更好的内存管理,以及当eventList更大时的一切。
CombineList.kt
fun main() {
val newList = mutableListOf<String>()
val eventList = createData()
eventList.forEachIndexed { _, event ->
val title = event.tit
fun forLoopListItems() {
val items = listOf("apple", "banana", "kiwifruit")
for (i in items) {
if (i.equals("banana")) {
println("position is ${i.indices}")
}
}
}
这是我的Kotlin代码,与For循环一起使用。我试图打印索引
Kotlin中有提取集合中项的父对象列表的方法吗?例如,如果我们有mutableMaps:
val AllPersons = mutableMapOf<Long,Person>()
val ClassAttendants = mutableMapOf<Long,Person>()
在以下情况下:
data class Person( PID : Long
Name: String
Surname: String
Father: Person?)
有没有办法从班长中挑选出所有(不同的)祖父?
我是Kotlin新手,正在尝试将一个用Scala编写的应用程序转换为Kotlin。在Scala中,有来自scala.collection.mutable.ListBuffer的ListBuffer。在Kotlin中有类似的东西吗?我尝试导入com.sun.tools.javac.util.ListBuffer,但在尝试编译时出现错误:Kotlin: Symbol is declared in module 'jdk.compiler' which does not export package 'com.sun.tools.javac.util'
我有一个Java/Kotlin互操作的问题。一个Kotlin不可变的列表被编译成一个正常的java.util.ArrayList,它是可变的!
Kotlin (库):
class A {
val items: List<Item> = ArrayList()
}
Java (消费者):
A a = new A();
a.getItems().add(new Item()); // Compiles and runs but I wish to fail or throw
如何让我的Kotlin类也从Java的角度完全不可变?
首先,对我糟糕的英语感到抱歉。我是android和kotlin的newby。我试图在xml布局中从片段访问到底层。当我创建膨胀是可以的,因为Kotlin识别de片段。但是当我声明视图时,我无法从布局中找到任何id
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.in
我有一个平面的产品文件,看上去像这样:
public List<Product> Products = new List<Product>{
new Product{Name = "OIS Swap", AssetType="Rates",ProductType="Swap",Currency="USD"},
new Product{Name = "Libor Swap", AssetType ="Rates",ProductType="Swap",Cu
我有以下代码,我需要在视图中显示列表的元素,然后从列表中删除这些元素。我一直在研究kotlin中的filter vs map,但没有找到解决方案。
var mutableList: MutableList<Object> = myImmutableList.toMutableList()
for (x in mutableList.indices)
{
val tile = row!!.getChildAt(x % 4)
val label = tile.findViewById(android.R.id.text1) as TextView
label.t
在Kotlin中通过属于该对象的属性来搜索ObjectArray的最简单方法是什么? 例如,我有一个数据类 data class Cat(
var name: String,
var age: Int,
var type: String,
) 我有一个Array<Cat>,想找出第一次出现的带有age == 4的猫。
我有映射列表,并希望获得id = 2的所有映射。当我将列表包装在holder对象中时,所有映射都正常工作(kotlin代码):
class Holder(val value : Any)
val list = listOf(mapOf("id" to 2), mapOf("id" to 3))
val context = Holder(list)
val xpath = "value[id = 2]"
JXPathContext.newContext(context).iterate(xpath)
但当我试图摆脱持有对象时:
val contex