Skip to content

Commit

Permalink
test(samples): add new samples
Browse files Browse the repository at this point in the history
  • Loading branch information
kotlin-samples-pusher-bot authored and AlexanderPrendota committed Sep 21, 2023
1 parent 682e40e commit 56bdcaf
Show file tree
Hide file tree
Showing 14 changed files with 160 additions and 3 deletions.
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
}
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
}
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
}
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
}
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
}
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
}
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
}
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
}
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
}
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)
}
}
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
}
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
}
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
}
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]
}

0 comments on commit 56bdcaf

Please sign in to comment.