Skip to content

Instantly share code, notes, and snippets.

@theaspect
Created April 4, 2020 05:40
Show Gist options
  • Save theaspect/f87c46492e65b0b85e14c3ce5606f4a1 to your computer and use it in GitHub Desktop.
Save theaspect/f87c46492e65b0b85e14c3ce5606f4a1 to your computer and use it in GitHub Desktop.
Generics demo
package com
class AnyList (val size: Int){
val data: Array<Any?> = Array(size) { null }
fun get(i: Int): Any? {
return data[i]
}
fun put(i: Any?, where: Int) {
data[where] = i
}
fun copy(): AnyList{
val new = AnyList(size)
for(i in 0 until size){
new.put(data[i], i)
}
return new
}
override fun toString(): String {
return "AnyList(data=${data.contentToString()})"
}
}
class IntList (val size: Int){
val data: Array<Int?> = Array(size) { null }
fun get(i: Int): Int? {
return data[i]
}
fun put(i: Int?, where: Int) {
data[where] = i
}
fun copy(): IntList{
val new = IntList(size)
for(i in 0 until size){
new.put(data[i], i)
}
return new
}
override fun toString(): String {
return "IntList(data=${data.contentToString()})"
}
}
class StringList (val size: Int){
val data: Array<String?> = Array(size) { null }
fun get(i: Int): String? {
return data[i]
}
fun put(i: String?, where: Int) {
data[where] = i
}
fun copy(): StringList{
val new = StringList(size)
for(i in 0 until size){
new.put(data[i], i)
}
return new
}
override fun toString(): String {
return "StringList(data=${data.contentToString()})"
}
}
class GenericList<T> (private val size: Int){
private val data = Array<Any?>(size) { null }
fun get(i: Int): T {
@Suppress("UNCHECKED_CAST")
return data[i] as T
}
fun put(i: T, where: Int) {
data[where] = i
}
fun copy(): GenericList<T>{
val new = GenericList<T>(size)
for(i in 0 until size){
@Suppress("UNCHECKED_CAST")
new.put(data[i] as T, i)
}
return new
}
override fun toString(): String {
return "GenericList(data=${data.contentToString()})"
}
}
fun mainOld() {
val intList = IntList(4)
intList.put(0, 0)
intList.put(1, 1)
intList.put(2, 2)
println(intList.toString())
val intListCopy = intList.copy()
println(intListCopy.toString())
val stringList = StringList(4)
stringList.put("a", 0)
stringList.put("b", 1)
stringList.put("c", 2)
println(stringList.toString())
val stringListCopy = stringList.copy()
println(stringListCopy.toString())
val anyList = AnyList(3)
anyList.put(0,0)
anyList.put(1,0)
//anyList.put("a",1)
println(anyList.toString())
val first: Int = anyList.get(0) as Int
val second: Int = anyList.get(1) as Int
}
data class MyClass(val a: Int, val b: String)
fun main(){
val intList = GenericList<Int>(4)
intList.put(0, 0)
intList.put(1, 1)
intList.put(2, 2)
println(intList.toString())
val intListCopy = intList.copy()
println(intListCopy.toString())
val stringList = GenericList<String>(4)
stringList.put("a", 0)
stringList.put("b", 1)
stringList.put("c", 2)
println(stringList.toString())
val stringListCopy = stringList.copy()
println(stringListCopy.toString())
val myList = GenericList<MyClass>(2)
myList.put(MyClass(1, "a"),0)
myList.put(MyClass(2, "b"),1)
println(myList.toString())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment