I have created an empty lists using
var lift = mutableListOf<Any(mutableListOf<Any(),mutableListOf<Any>())
But
lift[0].add(1)
Gives me error.
How to add elements to list inside list
The problem is that you have upcast the list type to Any instead of MutableList<Any>.
If you just typed:
mutableListOf(mutableListOf<Any>(),mutableListOf<Any>())
the type of items in the list is implicitly MutableList<Any>. But since you added <Any> like this:
mutableListOf<Any>(mutableListOf<Any>(),mutableListOf<Any>())
the compiler treats retrieved items as type Any instead of the specific type MutableList<Any> so you can't call functions on them.
You could also explicitly state the type like this, but it is not necessary:
mutableListOf<MutableList<Any>>(mutableListOf<Any>(),mutableListOf<Any>())
Try this:
val lift: MutableList<MutableList<Any>> = mutableListOf<MutableList<Any>>(mutableListOf<Any>(),mutableListOf<Any>())
Related
What I'm doing now:
I have a table with one field that is a json value that is stored as a super type in my staging schema.
the field containing the json is called elements
In my clean table, I typecast this field to VARCHAR in order to search it and use string functions
I want to search for the string net within that json in order to determine the key/value that I want to use for my filter
I tried the following:
select
elements
, elements_raw
from clean.events
where 1=1
and lower(elements) like '%net%'
or strpos(elements,'net')
My output
When running the above query, I keep getting an empty set returned.
My issue
I tried running the above code and using the elements_raw value instead but I got an issue :ERROR: function strpos(super, "unknown") does not exist Hint: No function matches the given name and argument types. You may need to add explicit type casts.
I checked the redshift super page and it doesn't list any specifics on searching strings within super types
Desired result:
Perform string operations on super field
Cast super field to a string type
There are some super related idiosyncrasies that are being run into here:
You cannot change the type of a super field via :: or cast()
String functions like and strpos do not work on super types
To address both of these issues, you can use the function json_serialize to return your super as a string.
Given a list of arbitrary objects
input = listOf(a, b, c, a)
... is there a function (with one non-collection argument) in the Kotlin standard library that I can use to make a copy of this list, removing all instances of ONE object?
Something like:
val filtered = input.removeAllInstancesOf(a)
To clarify, I'm aware of other (potential) solutions to this task:
Using the filter function to do this. → val output = input.filterNot { it == a }
Using the minus function with a collection → val output = input.minus(listOf(a))
Using the minus function with a non-collection argument → val output = input.minus(a) ← Only removes the first instance of a!
Removing all instances from a mutable list.
Writing such a function. → Wrap any of the above.
... but I'm wondering why I can't find a function which takes just ONE, non-collection value.
but I'm wondering why I can't find a function which takes just ONE, non-collection value.
Because that's a hyper-specific use-case of the already existing filter function. As you yourself showed it can be done in one line, and is probably the first thing a Kotlin dev would try to do (at least I would). So adding new function to the standard library probably doesn't add much value.
In my Kotlin code, I have a variable that is the Type interface from
java.lang.reflect
var type: Type
But I need to cast this to:
List<UserInfo>
If I was not casting to a List, I would just do this:
var type = UserInfo::class.java
and this works. But I don't know how to cast it using a List. The closest I found is this:
var type = Array<UserInfo>::class.java
This would compile if my UserInfo was an Array but it's a List.
The issue (as #Miha_x64 says) is type erasure.
The Java Virtual Machine knows nothing about type parameters. So although the source specified a List<UserInfo>, it compiles down to a plain List.
So this works:
var type = List::class.java
(Your Array example works because arrays are a special case: they're directly supported in the JVM, and keep their types at runtime.)
Java's use of type erasure is at least partly for historical reasons; when generics were added to Java 5, they wanted to preserve compatibility with existing source and bytecode. For all the gory details (much of which is inherited by Kotlin), see here.
Maybe its late. Try to use KTypeProjection and createType() with KClass instead. Then get the value in KType or convert it back to java Type.
val kClass = YourClass::class
val kTypeProjection = KTypeProjection.invariant(entity.starProjectedType)
val kType = List::class.createType(listOf(kTypeProjection))
val type = kType.javaType
result:
kType: kotlin.collections.List<YourClass>
type: java.util.List<YourClass>
can you please tell me what does the second parameter passed to the method listOf means? why do we need to pass null? why is the purpose of it?
code:
val listWithNulls: List<String?> = listOf("Kotlin", null)
listOf accepts a vararg of elements. Your second null is just one of many elements you passed to the list. Your list then consists of "Kotlin" and null.
So to answer your other questions:
why do we need to pass null? you don't need to...
what is the purpose of it? the person who wrote that code wanted to add a null element to the list... if it isn't clear from the context, why it is there, you should better ask the one who wrote that code. If it just consists of the line you showed, it's probably to demonstrate how lists with a nullable type can be constructed/used.
What does dijit.registry.remove() do? How does it handle invalid parameters?
The dijit.registry reference is an instance of dijit.WidgetSet, which is a collection of widgets.
The remove() function accepts an input ID and removes that Widget with the corresponding ID from the collection, if found.
In Dojo 1.4, WidgetSet is defined within dijit/_base/manager.js.
The passed ID is used internally as the key looked in an associative array. It's used like: this._hash[id], so passing it garbage will result in nothing being found or removed.