1. Array

 

 - array에서 size는 변경할 수 없지만, 각 element는 변경이 가능합니다.

val cars = arrayOf("Volvo", "BMW", "Ford", "Mazda")
cars[0] = "Opel"
println(cars[0])
// Now outputs Opel instead of Volvo

println(cars.size)
// Outputs 4


val testArray: array<Int> = arrayOf("apple", "banana", "cat")
testArray.get(0)    // apple
testArray.set(1, "bottle")    // banana -> bottle


val array1 = arrayOf(1, 2, 3)
val array2 = arrayOf(4, 5, 6)
val combinedArray = array1 + array2 // plus operator
println(combinedArray.contentToString()) // Prints [1, 2, 3, 4, 5, 6]

 - 그래서 size를 변경할 수 없으므로, array를 변경한 후 덮어씌워주거나 새로운 데이터를 활용합니다.

val array1 = arrayOf(1, 2, 3)
val array2 = arrayOf(4, 5, 6)
val array3 = arrayOf(7, 8 ,9)
var combinedArray = array1 + array2 + array3 // Combine multiple arrays
println(combinedArray.contentToString()) // [1, 2, 3, 4, 5 ,6 ,7 ,8 ,9]

combinedArray[0] = 10 // Modify data at index 
println(combinedArray.contentToString()) // [10, 2, 3, ...,8 ,9]

// Array does not directly support removing elements. You need to create a new one without the element.
combinedArray = combinedArray.filter { it !=10 }.toTypedArray() 
println(combinedArray.contentToString()) //[2 ,3 ...8 ,9]

 

 

 

 

 

2. List

 - array와 달리 list에서는 각 element는 변경이 불가능합니다.

//Array Example
val arr = arrayOf(1, 2, 3)
arr[0] = 10 // This is valid because arrays are mutable.
println(arr.contentToString()) // Prints [10, 2 ,3]

//List Example : Error 발생합니다.
val list = listOf(1 ,2 ,3)
list[0] = 10 // This will give a compile error because lists are immutable by default
val list1 = listOf(1, 2 ,3)
val list2 = listOf(4 ,5 ,6)
val combinedList = list1 + list2 // plus operator for immutable lists
println(combinedList) // Prints [1 ,2 ,3 ,4 ,5 ,6]

 - 특정 element 변경을 희망한다면 mutable list를 활용해야합니다.

 

 

 

 

 

3. Mutable List

 - mitable list는 array처럼 element 변경이 가능하고, 심지어 array에서는 불가능했던 element 추가 및 삭제를 통한 size 변화가 가능합니다.

 add 보단 += 형태의 plus assign을 활용해 element를 append하는 것을 권장합니다.

fun main() {
  //MutableList example 
  var mutableList= mutableListOf("apple", "banana", "cherry")
  mutableList[0] = "melon" // ["melon", "banana","cherry"]
  mutableList.add("dragonfruit") // [melon, banana, cherry, dragonfruit]
  println(mutableList)
  mutableList.removeAt(mutableList.indexOf("banana"))
  print(mutableList) // ["melon","cherry", "dragonfruit"]
  mutableList += "banana"
  print(mutableList) // ["melon","cherry", "dragonfruit", "banana"]
}
var mutableList1= mutableListOf("apple", "banana")
var mutableList2= mutableListOf("cherry", "dragonfruit")
mutableList1.addAll(mutableList2) // addAll function for mutable lists 
print(mutableList)// ["apple","banana","cherry","dragonfruit"]
val list1 = listOf("a", "b", "c")
val list2 = listOf("d", "e", "f")
var combinedList: MutableList<String> = (list1 + list2).toMutableList() // Combine multiple lists and convert to MutableList for modification and deletion.
println(combinedList) // [a,b,c,d,e,f]

combinedList[0] ="z"// Modify data at index 
println(combinedList) //[z,b,c,d,e,f]

combinedList.removeAt(0) // Remove data at index 
println(combinedList)//[b,c,d,e,f]

 

 

 

 

 

4. Map

 

fun main() {
    val myDict = mapOf(
        "name" to "John",
        "age" to 25,
        "city" to "New York"
    )
    
    println(myDict["name"]) // 출력: John
    println(myDict["age"]) // 출력: 25
}

 - 2개 이상의 map 합치기 : map을 합칠때, 이미 중복된 key가 있다면, 뒤 데이터로 덮어씌워집니다.

val map1 = mapOf("a" to 1, "b" to 2)
val map2 = mapOf("c" to 3, "d" to 4)
val map3 = mapOf("e" to 5, "d" to 6)

// 여러 개의 Map을 + 연산자를 사용하여 합칠 수 있습니다.
val result = map1 + map2 + map3 
println(result) // Prints: {a=1, b=2, c=3, d=6, e=5}

 - map에서 일부 데이터 삭제하기

val mutableMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3, "d" to 4)

mutableMap.remove("b") // 'b' 키와 그에 해당하는 값을 삭제합니다.
println(mutableMap) // Prints: {a=1,c=3,d=4}

 - map에서 일부 데이터 수정하기

val mutableMap = mutableMapOf("a" to 1, "b" to 2,"c",to 3,"d",to ,4)

mutableMap["b"] =20   // 'b' 키의 값을 '20'으로 변경합니다.
println(mutableMap) // Prints: {a=1,b=20,c=3,d=4}

 

 

 

 

 

5. Set

 

fun main() {
   val mySet = setOf(1, 2, 3, 4)
   
   for (item in mySet) {
       println(item)
   }
   // 1 2 3 4
}

val set1 = setOf(10 ,20 ,30)
val set2 = setOf(40 ,50)

// use plus operator to combine sets into a new one.
// Note: duplicate elements will be removed as it's a property

 - 연산자로 쉽게 추가, 삭제가 가능합니다.

val set1 = setOf(10 ,20)
val set2= setOf(30)
val set3= setOf(40)

var resultSet=set1+set2+set3

resultSet+=50

resultSet-=40

println(resultSet) //[10 ,20 ,30 ,50]

 

 

 

 

 

+ Recent posts