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?










share|improve this question






















  • How about mapNotNull().toArray()?
    – EpicPandaForce
    Nov 21 at 12:32















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?










share|improve this question






















  • How about mapNotNull().toArray()?
    – EpicPandaForce
    Nov 21 at 12:32













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?










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 at 12:29









Cilenco

2,585948100




2,585948100












  • How about mapNotNull().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




How about mapNotNull().toArray()?
– EpicPandaForce
Nov 21 at 12:32












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>






share|improve this answer























  • 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


















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()





share|improve this answer























  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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>






share|improve this answer























  • 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















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>






share|improve this answer























  • 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













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>






share|improve this answer














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>







share|improve this answer














share|improve this answer



share|improve this answer








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 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


















  • 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
















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












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()





share|improve this answer























  • 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















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()





share|improve this answer























  • 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













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()





share|improve this answer














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()






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 at 14:00

























answered Nov 21 at 12:40









Michi Gysel

57149




57149












  • 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


















  • 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
















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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks