文章目录 一、MutableList 可变列表集合 二、修改 MutableList 集合的 mutator 函数 1、添加元素运算符 += 和 删除元素运算符 -= 2、通过 Lambda 表达式筛选要删除的元素 一、MutableList 可变列表集合 ---- 使用 listOf 函数 创建的 List 集合 是 只读列表集合 ; 使用 mutableListOf 函数 创建的 MutableList 集合 // 添加元素 mutableList.add("Jack") // 删除元素 mutableList.remove("Tom") println(mutableList val mutable: MutableList<String> = list.toMutableList() } 执行结果 : [Jerry, Jack] 二、修改 MutableList " 其效果等同于 mutableList.add("Jack") 从 MutableList 集合 中删除一些元素 , 可使用 -= 运算符 : mutableList -= "Tom" 其效果等同于
mutableListOf创建可变list val mutableList = mutableListOf("张三", "李四", "王五") mutableList.remove(" 张三") mutableList.add("赵六") println(mutableList.getOrElse(1) { "empty" }) println(mutableList.getOrNull = mutableListOf("张三", "李四", "王五") mutableList += "孙七" mutableList -= "王五" } 还可以基于lambda表达式指定的条件删除元素 创建可变list val mutableList = mutableListOf("张三", "李四", "王五") for (item in mutableList) { println(item) } mutableList.forEach { println(it) } mutableList.forEachIndexed { index,
(),返回List接口 class TestKotlin { fun function() { // 创建MutableList可以继续添加修改元素 var mutableList : MutableList<Int> = mutableListOf(1, 2) mutableList.add(3) mutableList.remove(4) : MutableList<Int> = mutableListOf(1, 2, 3, 4) mutableList.forEach { println("Mutable List Elements:$it") } var mutableList: MutableList<Int> = mutableListOf(1, 2) class TestKotlin { fun function() { var mutableList: MutableList<Int> = mutableListOf(1,
类型投影(Type projections) 在上一篇文章<<Kotlin 范型之协变、逆变>>中,曾经介绍过 MutableList 是不变的,可读可写,没有使用 in、out 修饰。 如果对 MutableList 的参数类型使用 in 或者 out 修饰,会发生什么情况呢? 此时,list2 和 list3 分别表示一个受限制的 MutableList。在 Kotlin 中,这种行为被称之为类型投影。其主要作用是参数作限定,避免不安全操作。 正是由于 list3 是一个受限制的 MutableList,因此它赋值给 list4 报错也是可以理解了。 例如:MutableList<*> 表示的是 MutableList<out Any?
("Jack") // 删除元素 mutableList.remove("Tom") println(mutableList) // 将 可变列表集合 转为 只读列表集合 val list: List<String> = mutableList.toList() // 将 只读列表集合 转为 可变列表集合 val mutable: MutableList <String> = list.toMutableList() } 执行结果 : [Jerry, Jack] 五、修改 MutableList 集合的 mutator 函数 ---- 修改 MutableList 删除元素运算符 -= ; 向 MutableList 集合 中添加一些元素 , 可使用 += 运算符 : mutableList += "Jack" 其效果等同于 mutableList.add("Jack ") 从 MutableList 集合 中删除一些元素 , 可使用 -= 运算符 : mutableList -= "Tom" 其效果等同于 mutableList.remove("Tom") 2、通过
、MutableMap 1.2、基本操作 //创建一个List容器 var nameList:MutableList = mutableListof("zack", 容器的时候,需要使用关键词to,to前面是键,后面是值 var map:Map<String, String> = mapOf("name" to "zack") //创建可变容器 var list:MutableList remove()方法 //添加元素 set.add("zack") //删除元素,因为Set是无序的,所以不能通过下标删除元素 set.remove("zack") 3、队列List/MutableList 3.1、基本方法 //定义一个队列 var list:MutableList<String> = mutableListOf(); //添加元素,将元素添加到对尾 { it.age } //降序排列 sortByDescending{ it.age } //关于it.age //加入有下列队列,泛型为Person(自定义的类) var humanList:MutableList
= s // MutableList<String> 不是 MutableList<Any> 的子类型,因为MutableList可空 /**----------- * Kotlin中的 MutableList<out T> 和 Java中的 MutableList<? extends T> 是一个意思。 * in 投影的 MutableList<in T> 对应到Java的 MutableList<? <*> 和 MutableList<Any? 的元素 // MutableList<*> 投影成了 MutableList<out Any?
这里不能写listof(1),否则会被优化为SingletonList类型,其set方法是没有被实现的 val l = listOf(1, 2) try { (l as MutableList ).add(0, 2) } catch (e: Exception) { println(e) } try { (l as MutableList 这里 ImmutableList可以强转为 MutableList并修改其中的元素。 ? Kotlin代码要实现100%兼容Java,则无论穿的衣服是 MutableList还是 ImmutableList,卸下伪装后都只能是Java的 List。 下面做一个试验: // A val mutableList = (0..5).toMutableList() Thread { mutableList.forEach { println
Set<String> immutableSet = Set.of("apple", "banana", "orange");传统创建集合的方式List<String> mutableList = new ArrayList<>();mutableList.add("apple");mutableList.add("banana");mutableList.add("orange");Map<String
Observable.just(18, "China", "Ma") .collect(Callable<MutableList<Any>> { arrayListOf() }, BiConsumer<MutableList<Any>, Any> { t1, t2 -> t1.add(t2) }).subscribe(Consumer<MutableList Observable.just(18, "China", "Ma") .collectInto(arrayListOf() , BiConsumer<MutableList<Any>, Any> { t1, t2 -> t1.add(t2) }).subscribe(Consumer<MutableList<Any>> { Log.e("RX"
list 列表 * @param currentPage 当前页,从1开始 * @param pageSize 每页数量 */ fun <T> page(list: MutableList <T>, currentPage: Int, pageSize: Int): Page<T> { val page = Page<T>() var data: MutableList 1 var pageSize: Int = 10 var totalPage: Int = 10 var totalCount: Int = 10 var data: MutableList
: "Unknown") 创建和修改可变list:mutableList val mutableList = mutableListOf("a","d","f") mutableList.removeIf
int count = 0; Method *methodList = class_copyMethodList(self, &count); NSMutableArray *mutableList { Method method = methodList[i]; SEL methodName = method_getName(method); [mutableList Method *methodList = class_copyMethodList(object_getClass(self), &count); NSMutableArray *mutableList protocol = protocolList[i]; const char *protocolName = protocol_getName(protocol); [mutableList addObject:[NSString stringWithUTF8String:protocolName]]; } return [NSArray arrayWithArray:mutableList
var specs: String = "" var batchno: String = "" var qty: Int = 0 override val childNode: MutableList get() = null } Head父级列表继承自BaseExpandNode,其中要定义其明细列表MutableList<BaseNode> class Head : BaseExpandNode () { var Bodys: MutableList<BaseNode>? = null var deptno: String = "" var deptname: String = "" override val childNode: MutableList 插入一条数据和删除一条数据的三个方法,完整的ViewModel代码 class DataViewModel: ViewModel() { val DataList = MutableSharedFlow<MutableList
Set<String> immutableSet = Set.of("apple", "banana", "orange"); 传统创建集合的方式 List<String> mutableList = new ArrayList<>(); mutableList.add("apple"); mutableList.add("banana"); mutableList.add("orange");
val repository: BmobRepository) : ViewModel() { var libraryRecommendData = MutableLiveData<MutableList BmobRepository类中有一个挂起函数getAllRecommendLibrary(libraryRecommendData: MutableLiveData<MutableList>)用来获取云数据库中的数据 中所有推荐开源项目 */ suspend fun getAllRecommendLibrary(libraryRecommendData: MutableLiveData<MutableList bombQuery.findObjects(object : FindListener<AndroidLibrary>() { override fun done(data: MutableList
fun addApple(apple: MutableList<Apple>) { // TODO } fun getApple(apple: MutableList 我们能不能让 MutableList<Fruit> 成为 MutableList<Apple> 的父类型呢? Java泛型中引入了类型通配符的概念来解决这个问题。 也就是说MutableList<? extends Fruit> 是 MutableList<Apple> 的父类型。 Kotlin中使用 MutableList<out Fruit> 来表示。 也就是说MutableList<? super Fruit> 是 MutableList<Object>的父类型。Kotlin中使用 MutableList<in Fruit> 来表示。 对应地在Kotlin中并不存在MutableList<Fruit>::class, 而只有 MutableList::class 。
静态导入使用依然很麻烦,如果能给list类添加扩展函数就好了: list.swap(list.binarySearch(otherList.max()), list.max()) 2.类-扩展函数 1.定义 为MutableList 类扩展一个swap函数: fun MutableList<Int>.swap(index1: Int, index2: Int) { val tmp = this[index1] //this: 当前MutableList对象 this[index1] = this[index2] this[index2] = tmp } 对 MutableList对象调用swap函数: val list = mutableListOf(1, 2, 3) list.swap(0, 2) MutableList泛化类型: //为在表达式中使用泛型 fun <T> MutableList<T>.swap(index1: Int, index2: Int) { val tmp = this[index1] this[index1
unsigned int count = 0; Ivar *ivarList = class_copyIvarList(class, &count); NSMutableArray *mutableList ivarType]; dic[@"ivarName"] = [NSString stringWithUTF8String: ivarName]; [mutableList addObject:dic]; } free(ivarList); return [NSArray arrayWithArray:mutableList]; } 在OC中的给类添加成员属性其实就是添加了一个成员变量和
首先看原始版本,拷贝一个列表到另一个: fun <T> copyDataVersion1(source: MutableList<T>, destination: MutableList<T>) { 那么根据这个问题有了下面的改进: fun <T: R, R> copyDataVersion2(source: MutableList<T>, destination: MutableList<R>) 当然没有, Kotlin 提供了一个更加优雅的解决方案,不信你看看下面的代码: fun <T> copyDataVersion3(source: MutableList<out T>, destination : MutableList<T>) { for (item in source) { destination.add(item) } } 什么叫做优化?