forked from JetBrains/kotlin-web-site
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
doc(docs.topics.arrays): add notes and examples
- Loading branch information
alfredo-toledano
committed
Aug 28, 2024
1 parent
94b4650
commit cf1c27e
Showing
2 changed files
with
40 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
fun main() { | ||
// 1. arrays are fixed size -> if you try to add an element -> new array is created | ||
var riversArray = arrayOf("Nile", "Amazon", "Yangtze") | ||
val originalReference = riversArray.hashCode() | ||
println("Original Array: ${riversArray.joinToString()}") | ||
println("Original Array Reference: ${originalReference}") | ||
|
||
// += -> creates a new riversArray / copies over the original elements & adds the element | ||
riversArray += "Mississippi" | ||
val newReference = riversArray.hashCode() | ||
println("New Array: ${riversArray.joinToString()}") | ||
println("New Array Reference: ${newReference}") | ||
println("originalReference $originalReference vs newReference $newReference are different ${originalReference!=newReference}") | ||
|
||
// 2. == compare references | ||
val array1 = arrayOf(1, 2, 3) | ||
val array2 = arrayOf(1, 2, 3) | ||
println("array1 == array2 ${array1 == array2}") // false, because it compares references | ||
|
||
// 3. contentEquals if you want to compare array's content | ||
println("array1.contentEquals(array2) ${array1.contentEquals(array2)}") // true, because it compares the content | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters