Create Array without nullable types from Array with nullable types
up vote
0
down vote
favorite
In Kotlin we have to distinguish between nullable types and not nullable types. Let's say I have an Array<String?>
fom which I know that every value within it is actually not null. Is there an easy way to create an Array<String>
from the source array without copying it?
kotlin kotlin-null-safety
add a comment |
up vote
0
down vote
favorite
In Kotlin we have to distinguish between nullable types and not nullable types. Let's say I have an Array<String?>
fom which I know that every value within it is actually not null. Is there an easy way to create an Array<String>
from the source array without copying it?
kotlin kotlin-null-safety
How aboutmapNotNull().toArray()
?
– EpicPandaForce
Nov 21 at 12:32
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
In Kotlin we have to distinguish between nullable types and not nullable types. Let's say I have an Array<String?>
fom which I know that every value within it is actually not null. Is there an easy way to create an Array<String>
from the source array without copying it?
kotlin kotlin-null-safety
In Kotlin we have to distinguish between nullable types and not nullable types. Let's say I have an Array<String?>
fom which I know that every value within it is actually not null. Is there an easy way to create an Array<String>
from the source array without copying it?
kotlin kotlin-null-safety
kotlin kotlin-null-safety
asked Nov 21 at 12:29
Cilenco
2,595948100
2,595948100
How aboutmapNotNull().toArray()
?
– EpicPandaForce
Nov 21 at 12:32
add a comment |
How aboutmapNotNull().toArray()
?
– EpicPandaForce
Nov 21 at 12:32
How about
mapNotNull().toArray()
?– EpicPandaForce
Nov 21 at 12:32
How about
mapNotNull().toArray()
?– EpicPandaForce
Nov 21 at 12:32
add a comment |
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
array.requireNoNulls()
returns same array Array<T?>
with non-optional type Array<T>
(But throws IllegalArgmentException
if any of the item found null
).
if you are sure that your array doesn't have null
then you can typecast.
array as Array<String>
array.requireNoNulls() is safer to use in what sense? It throws anIllegalArgumentException
if there are anynull
elements.
– forpas
Nov 21 at 14:04
@forpas Good catch. I have updated the answer. Thanks!
– I Don't Exist
Nov 21 at 14:08
add a comment |
up vote
0
down vote
Array.filterNotNull might be the safer way to do it. But it will create a new Array.
val items: Array<String?> = arrayOf("one", "two", null, "three")
val itemsWithoutNull: List<String> = items.filterNotNull()
question says without copying it.filterNotNull
creates newArrayList
and addsNon-Null
values to that before returning it.
– I Don't Exist
Nov 21 at 13:11
Good point. I mentioned this.
– Michi Gysel
Nov 21 at 14:00
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
array.requireNoNulls()
returns same array Array<T?>
with non-optional type Array<T>
(But throws IllegalArgmentException
if any of the item found null
).
if you are sure that your array doesn't have null
then you can typecast.
array as Array<String>
array.requireNoNulls() is safer to use in what sense? It throws anIllegalArgumentException
if there are anynull
elements.
– forpas
Nov 21 at 14:04
@forpas Good catch. I have updated the answer. Thanks!
– I Don't Exist
Nov 21 at 14:08
add a comment |
up vote
3
down vote
accepted
array.requireNoNulls()
returns same array Array<T?>
with non-optional type Array<T>
(But throws IllegalArgmentException
if any of the item found null
).
if you are sure that your array doesn't have null
then you can typecast.
array as Array<String>
array.requireNoNulls() is safer to use in what sense? It throws anIllegalArgumentException
if there are anynull
elements.
– forpas
Nov 21 at 14:04
@forpas Good catch. I have updated the answer. Thanks!
– I Don't Exist
Nov 21 at 14:08
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
array.requireNoNulls()
returns same array Array<T?>
with non-optional type Array<T>
(But throws IllegalArgmentException
if any of the item found null
).
if you are sure that your array doesn't have null
then you can typecast.
array as Array<String>
array.requireNoNulls()
returns same array Array<T?>
with non-optional type Array<T>
(But throws IllegalArgmentException
if any of the item found null
).
if you are sure that your array doesn't have null
then you can typecast.
array as Array<String>
edited Nov 21 at 14:07
answered Nov 21 at 12:37
I Don't Exist
2,32511418
2,32511418
array.requireNoNulls() is safer to use in what sense? It throws anIllegalArgumentException
if there are anynull
elements.
– forpas
Nov 21 at 14:04
@forpas Good catch. I have updated the answer. Thanks!
– I Don't Exist
Nov 21 at 14:08
add a comment |
array.requireNoNulls() is safer to use in what sense? It throws anIllegalArgumentException
if there are anynull
elements.
– forpas
Nov 21 at 14:04
@forpas Good catch. I have updated the answer. Thanks!
– I Don't Exist
Nov 21 at 14:08
array.requireNoNulls() is safer to use in what sense? It throws an
IllegalArgumentException
if there are any null
elements.– forpas
Nov 21 at 14:04
array.requireNoNulls() is safer to use in what sense? It throws an
IllegalArgumentException
if there are any null
elements.– forpas
Nov 21 at 14:04
@forpas Good catch. I have updated the answer. Thanks!
– I Don't Exist
Nov 21 at 14:08
@forpas Good catch. I have updated the answer. Thanks!
– I Don't Exist
Nov 21 at 14:08
add a comment |
up vote
0
down vote
Array.filterNotNull might be the safer way to do it. But it will create a new Array.
val items: Array<String?> = arrayOf("one", "two", null, "three")
val itemsWithoutNull: List<String> = items.filterNotNull()
question says without copying it.filterNotNull
creates newArrayList
and addsNon-Null
values to that before returning it.
– I Don't Exist
Nov 21 at 13:11
Good point. I mentioned this.
– Michi Gysel
Nov 21 at 14:00
add a comment |
up vote
0
down vote
Array.filterNotNull might be the safer way to do it. But it will create a new Array.
val items: Array<String?> = arrayOf("one", "two", null, "three")
val itemsWithoutNull: List<String> = items.filterNotNull()
question says without copying it.filterNotNull
creates newArrayList
and addsNon-Null
values to that before returning it.
– I Don't Exist
Nov 21 at 13:11
Good point. I mentioned this.
– Michi Gysel
Nov 21 at 14:00
add a comment |
up vote
0
down vote
up vote
0
down vote
Array.filterNotNull might be the safer way to do it. But it will create a new Array.
val items: Array<String?> = arrayOf("one", "two", null, "three")
val itemsWithoutNull: List<String> = items.filterNotNull()
Array.filterNotNull might be the safer way to do it. But it will create a new Array.
val items: Array<String?> = arrayOf("one", "two", null, "three")
val itemsWithoutNull: List<String> = items.filterNotNull()
edited Nov 21 at 14:00
answered Nov 21 at 12:40
Michi Gysel
57149
57149
question says without copying it.filterNotNull
creates newArrayList
and addsNon-Null
values to that before returning it.
– I Don't Exist
Nov 21 at 13:11
Good point. I mentioned this.
– Michi Gysel
Nov 21 at 14:00
add a comment |
question says without copying it.filterNotNull
creates newArrayList
and addsNon-Null
values to that before returning it.
– I Don't Exist
Nov 21 at 13:11
Good point. I mentioned this.
– Michi Gysel
Nov 21 at 14:00
question says without copying it.
filterNotNull
creates new ArrayList
and adds Non-Null
values to that before returning it.– I Don't Exist
Nov 21 at 13:11
question says without copying it.
filterNotNull
creates new ArrayList
and adds Non-Null
values to that before returning it.– I Don't Exist
Nov 21 at 13:11
Good point. I mentioned this.
– Michi Gysel
Nov 21 at 14:00
Good point. I mentioned this.
– Michi Gysel
Nov 21 at 14:00
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53412077%2fcreate-array-without-nullable-types-from-array-with-nullable-types%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
How about
mapNotNull().toArray()
?– EpicPandaForce
Nov 21 at 12:32