ArrayList shallow copy clone, Kotlin

ArrayList shallow copy clone, Kotlin

import kotlin.collections.ArrayList

fun main() {
    var list = ArrayList<MyData>()
    for (i in 0..<3) {
        list.add(MyData(i, 0))
    }

    //浅拷贝list到copyList
    var copyList = list.clone()

    println(list)
    println(copyList)

    println("---")

    //改变list的值,然后观察copyList的值变化。
    list.forEachIndexed { index, myData ->
        myData.pos = index
    }

    list.removeAt(list.size - 1)

    println(list)
    println(copyList) //copyList里面的对象MyData的值和list一致,如果该元素存在。
}

data class MyData(var index: Int, var pos: Int)

[MyData(index=0, pos=0), MyData(index=1, pos=0), MyData(index=2, pos=0)]
[MyData(index=0, pos=0), MyData(index=1, pos=0), MyData(index=2, pos=0)]
---
[MyData(index=0, pos=0), MyData(index=1, pos=1)]
[MyData(index=0, pos=0), MyData(index=1, pos=1), MyData(index=2, pos=2)]

Java ConcurrentLinkedQueue queue thread-safe operation_zhangphil's blog-CSDN blogJava ConcurrentLinkedQueue queue thread-safe operation code example: package async;import java.util.ArrayList;import java.util.List;import java.util.Queue;import java .util.concurrent.ConcurrentLinkedQueue;/** * * Thread Safety Team https://blog.csdn.net/zhangphil/article/details/65936066

Java's Vector, Stack, ArrayList, LinkedList similarities and differences_zhangphil's blog-CSDN blog What are the implementation subtypes of Java's Vector, Stack, ArrayList, LinkedList similarities and differences Collection, they all support the iterator() function, which returns an iterator, This iterator can traverse and access each element in the Collection. The two interface classes List and Set derived from Collection. List is an ordered Collection. Unlike Set, List allows identical elements. List interface implementation class... https://blog.csdn.net/zhangphil/article/details/91822423

Java reverses a List or ArrayList_zhangphil's blog-CSDN blog package demo;import java.util.ArrayList;import java.util.Collections;/** * * To reverse a List, the key is to use the Collections tool class* * @author Phil * */public class Demo {public static void main(String[] args) https://blog.csdn.net/zhangphil/article/details/54406817

Guess you like

Origin blog.csdn.net/zhangphil/article/details/132087401