kotlin - 列表的结构和非结构之间的区别

标签 kotlin

如 Kotlin 文档中所述,特别是 List::subList 方法 docs :

The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa.

Structural changes in the base list make the behavior of the view undefined.

对于上述结构和非结构变化,我找不到任何进一步的解释或确切定义。直觉如下:


非结构性

改变列表的元素,而不是列表本身。例如,将列表第一个元素的值从 1 更改为 2。另一个例子是将列表转换为其他类型。

结构:

改变列表结构:删除或添加元素,改变元素的顺序。


我的问题主要是:我的假设是否正确?我们可以为这些“定义”添加一些内容吗?

返回的子列表中的结构 更改如何,它们是否使基本 ListView 未定义?最后,undefined 是否意味着基础或子列表中的更改可能会或可能不会反射(reflect)在相应的 View 中?

在此先感谢您的讨论和您的见解!

最佳答案

这与 the Java version 的方法相同及其文档回答了您的问题(从技术上讲,Kotlin/JS 和 Kotlin/Native 的行为可能有所不同,但这会非常令人惊讶):

  1. 结构性与非结构性:

    (Structural modifications are those that change the size of this list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.)

    “另一个例子是将列表转换为其他类型。”转换名单根本不会改变它。

  2. What about structural changes in the returned sub-list, do they make the base list view undefined?

    否:

    The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is structurally modified in any way other than via the returned list.

  3. does undefined mean that changes in the base or sub-list may or may not be reflected in the corresponding views?

    这没有直接回答,但不,它没有。它可以例如对任何后续访问抛出异常。

关于kotlin - 列表的结构和非结构之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59107018/

相关文章:

Android Kotlin 协程 : await might silently drop exceptions

generics - 如何在实现中继承泛型类型而在 Kotlin 中没有泛型接口(interface)?

int - kotlin int 盒装标识

android - 如何在 Kotlin 中的 RecyclerView.Adapter 中使用 SharedPreferences?

java - Kotlin 中内部类的变量阴影 : how variables are resolved?

android - 具有附加下拉菜单的 Material 设计TextInputLayout?

kotlin - 如何在 Kotlin 中使用 DatePickerDialog?

java - 用 Kotlin 数据类替换 Java DTO 类的问题

java - Spring Facebook 模板将 fetchObject 映射到 PagedList

android - 如何使用 Context.getSystemService(Class) 在 Kotlin 中获取实例