-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
682e40e
commit 56bdcaf
Showing
14 changed files
with
160 additions
and
3 deletions.
There are no files selected for viewing
7 changes: 4 additions & 3 deletions
7
...ources/test-compile-data/jvm/kotlin-web-site/arrays/23031df00534726bea3f25bea3d46032.1.kt
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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
fun main() { | ||
//sampleStart | ||
// Creates an Array<String> with values ["0", "1", "4", "9", "16"] | ||
val asc = Array(5) { i -> (i * i).toString() } | ||
asc.forEach { println(it) } | ||
// Creates an array with values [1, 2, 3] | ||
val simpleArray = arrayOf(1, 2, 3) | ||
println(simpleArray.joinToString()) | ||
// 1, 2, 3 | ||
//sampleEnd | ||
} |
13 changes: 13 additions & 0 deletions
13
...urces/test-compile-data/jvm/kotlin-web-site/arrays/23031df00534726bea3f25bea3d46032.10.kt
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,13 @@ | ||
fun main() { | ||
//sampleStart | ||
val simpleArray = arrayOf("a", "b", "c", "c") | ||
|
||
// Converts to a Set | ||
println(simpleArray.toSet()) | ||
// [a, b, c] | ||
|
||
// Converts to a List | ||
println(simpleArray.toList()) | ||
// [a, b, c, c] | ||
//sampleEnd | ||
} |
13 changes: 13 additions & 0 deletions
13
...urces/test-compile-data/jvm/kotlin-web-site/arrays/23031df00534726bea3f25bea3d46032.11.kt
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,13 @@ | ||
fun main() { | ||
//sampleStart | ||
val pairArray = arrayOf("apple" to 120, "banana" to 150, "cherry" to 90, "apple" to 140) | ||
|
||
// Converts to a Map | ||
// The keys are fruits and the values are their number of calories | ||
// Note how keys must be unique, so the latest value of "apple" | ||
// overwrites the first | ||
println(pairArray.toMap()) | ||
// {apple=140, banana=150, cherry=90} | ||
|
||
//sampleEnd | ||
} |
8 changes: 8 additions & 0 deletions
8
...urces/test-compile-data/jvm/kotlin-web-site/arrays/23031df00534726bea3f25bea3d46032.12.kt
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,8 @@ | ||
fun main() { | ||
//sampleStart | ||
// Creates an array of Int of size 5 with values | ||
val exampleArray = IntArray(5) | ||
println(exampleArray.joinToString()) | ||
// 0, 0, 0, 0, 0 | ||
//sampleEnd | ||
} |
11 changes: 11 additions & 0 deletions
11
...urces/test-compile-data/jvm/kotlin-web-site/arrays/23031df00534726bea3f25bea3d46032.13.kt
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,11 @@ | ||
fun main() { | ||
//sampleStart | ||
var riversArray = arrayOf("Nile", "Amazon", "Yangtze") | ||
|
||
// Using the += assignment operation creates a new riversArray, | ||
// copies over the original elements and adds "Mississippi" | ||
riversArray += "Mississippi" | ||
println(riversArray.joinToString()) | ||
// Nile, Amazon, Yangtze, Mississippi | ||
//sampleEnd | ||
} |
8 changes: 8 additions & 0 deletions
8
...ources/test-compile-data/jvm/kotlin-web-site/arrays/23031df00534726bea3f25bea3d46032.2.kt
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,8 @@ | ||
fun main() { | ||
//sampleStart | ||
// Creates an array with values [null, null, null] | ||
val nullArray: Array<Int?> = arrayOfNulls(3) | ||
println(nullArray.joinToString()) | ||
// null, null, null | ||
//sampleEnd | ||
} |
13 changes: 13 additions & 0 deletions
13
...ources/test-compile-data/jvm/kotlin-web-site/arrays/23031df00534726bea3f25bea3d46032.3.kt
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,13 @@ | ||
fun main() { | ||
//sampleStart | ||
// Creates an Array<Int> that initializes with zeros [0, 0, 0] | ||
val initArray = Array<Int>(3) { 0 } | ||
println(initArray.joinToString()) | ||
// 0, 0, 0 | ||
|
||
// Creates an Array<String> with values ["0", "1", "4", "9", "16"] | ||
val asc = Array(5) { i -> (i * i).toString() } | ||
asc.forEach { print(it) } | ||
// 014916 | ||
//sampleEnd | ||
} |
13 changes: 13 additions & 0 deletions
13
...ources/test-compile-data/jvm/kotlin-web-site/arrays/23031df00534726bea3f25bea3d46032.4.kt
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,13 @@ | ||
fun main() { | ||
//sampleStart | ||
// Creates a two-dimensional array | ||
val twoDArray = Array(2) { Array<Int>(2) { 0 } } | ||
println(twoDArray.contentDeepToString()) | ||
// [[0, 0], [0, 0]] | ||
|
||
// Creates a three-dimensional array | ||
val threeDArray = Array(3) { Array(3) { Array<Int>(3) { 0 } } } | ||
println(threeDArray.contentDeepToString()) | ||
// [[[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0]]] | ||
//sampleEnd | ||
} |
14 changes: 14 additions & 0 deletions
14
...ources/test-compile-data/jvm/kotlin-web-site/arrays/23031df00534726bea3f25bea3d46032.5.kt
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,14 @@ | ||
fun main() { | ||
//sampleStart | ||
val simpleArray = arrayOf(1, 2, 3) | ||
val twoDArray = Array(2) { Array<Int>(2) { 0 } } | ||
|
||
// Accesses the element and modifies it | ||
simpleArray[0] = 10 | ||
twoDArray[0][0] = 2 | ||
|
||
// Prints the modified element | ||
println(simpleArray[0].toString()) // 10 | ||
println(twoDArray[0][0].toString()) // 2 | ||
//sampleEnd | ||
} |
11 changes: 11 additions & 0 deletions
11
...ources/test-compile-data/jvm/kotlin-web-site/arrays/23031df00534726bea3f25bea3d46032.6.kt
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,11 @@ | ||
fun main() { | ||
val lettersArray = arrayOf("c", "d") | ||
printAllStrings("a", "b", *lettersArray) | ||
// abcd | ||
} | ||
|
||
fun printAllStrings(vararg strings: String) { | ||
for (string in strings) { | ||
print(string) | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...ources/test-compile-data/jvm/kotlin-web-site/arrays/23031df00534726bea3f25bea3d46032.7.kt
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,16 @@ | ||
fun main() { | ||
//sampleStart | ||
val simpleArray = arrayOf(1, 2, 3) | ||
val anotherArray = arrayOf(1, 2, 3) | ||
|
||
// Compares contents of arrays | ||
println(simpleArray.contentEquals(anotherArray)) | ||
// true | ||
|
||
// Using infix notation, compares contents of arrays after an element | ||
// is changed | ||
simpleArray[0] = 10 | ||
println(simpleArray contentEquals anotherArray) | ||
// false | ||
//sampleEnd | ||
} |
9 changes: 9 additions & 0 deletions
9
...ources/test-compile-data/jvm/kotlin-web-site/arrays/23031df00534726bea3f25bea3d46032.8.kt
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,9 @@ | ||
fun main() { | ||
//sampleStart | ||
val sumArray = arrayOf(1, 2, 3) | ||
|
||
// Sums array elements | ||
println(sumArray.sum()) | ||
// 6 | ||
//sampleEnd | ||
} |
13 changes: 13 additions & 0 deletions
13
...ources/test-compile-data/jvm/kotlin-web-site/arrays/23031df00534726bea3f25bea3d46032.9.kt
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,13 @@ | ||
fun main() { | ||
//sampleStart | ||
val simpleArray = arrayOf(1, 2, 3) | ||
|
||
// Shuffles elements [3, 2, 1] | ||
simpleArray.shuffle() | ||
println(simpleArray.joinToString()) | ||
|
||
// Shuffles elements again [2, 3, 1] | ||
simpleArray.shuffle() | ||
println(simpleArray.joinToString()) | ||
//sampleEnd | ||
} |
14 changes: 14 additions & 0 deletions
14
...pile-data/jvm/kotlin-web-site/collections-overview/f42863ed2f6912c75d4cf647a4408652.12.kt
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,14 @@ | ||
fun main() { | ||
val deque = ArrayDeque(listOf(1, 2, 3)) | ||
|
||
deque.addFirst(0) | ||
deque.addLast(4) | ||
println(deque) // [0, 1, 2, 3, 4] | ||
|
||
println(deque.first()) // 0 | ||
println(deque.last()) // 4 | ||
|
||
deque.removeFirst() | ||
deque.removeLast() | ||
println(deque) // [1, 2, 3] | ||
} |