Why is arrayOf() the same as mutableListOf() but limited?
Trying to understand what the point was when making arrayOf()
the same as mutableListOf()
but removing features such as add()
and remove()
?
android kotlin
|
show 2 more comments
Trying to understand what the point was when making arrayOf()
the same as mutableListOf()
but removing features such as add()
and remove()
?
android kotlin
Do you meanmutableListOf()
?
– CommonsWare
Jan 19 at 21:11
YesmutableListOf()
, edited
– Zorgan
Jan 19 at 21:11
It feels like this is a duplicate of your question. Is there something specific that you are interested in that is not covered in that question and its answers?
– CommonsWare
Jan 19 at 21:15
That's a good explanation thanks. So mainly the only reason to use arrays are for performance enhancements (searching for objects in the array quicker)?. Also I couldn't seem to find the advantage of using aList
over aMutableList
?
– Zorgan
Jan 19 at 21:22
1
In a lot of modern programming, we try to use immutable objects where possible. Kotlin emphasizes this (val
versusvar
,MutableList
versusList
, etc.). Immutability has a lot of positive benefits (e.g., two threads can access the object simultaneously without conflict).
– CommonsWare
Jan 19 at 21:26
|
show 2 more comments
Trying to understand what the point was when making arrayOf()
the same as mutableListOf()
but removing features such as add()
and remove()
?
android kotlin
Trying to understand what the point was when making arrayOf()
the same as mutableListOf()
but removing features such as add()
and remove()
?
android kotlin
android kotlin
edited Jan 19 at 21:12
CommonsWare
770k13818791931
770k13818791931
asked Jan 19 at 21:10
ZorganZorgan
78611340
78611340
Do you meanmutableListOf()
?
– CommonsWare
Jan 19 at 21:11
YesmutableListOf()
, edited
– Zorgan
Jan 19 at 21:11
It feels like this is a duplicate of your question. Is there something specific that you are interested in that is not covered in that question and its answers?
– CommonsWare
Jan 19 at 21:15
That's a good explanation thanks. So mainly the only reason to use arrays are for performance enhancements (searching for objects in the array quicker)?. Also I couldn't seem to find the advantage of using aList
over aMutableList
?
– Zorgan
Jan 19 at 21:22
1
In a lot of modern programming, we try to use immutable objects where possible. Kotlin emphasizes this (val
versusvar
,MutableList
versusList
, etc.). Immutability has a lot of positive benefits (e.g., two threads can access the object simultaneously without conflict).
– CommonsWare
Jan 19 at 21:26
|
show 2 more comments
Do you meanmutableListOf()
?
– CommonsWare
Jan 19 at 21:11
YesmutableListOf()
, edited
– Zorgan
Jan 19 at 21:11
It feels like this is a duplicate of your question. Is there something specific that you are interested in that is not covered in that question and its answers?
– CommonsWare
Jan 19 at 21:15
That's a good explanation thanks. So mainly the only reason to use arrays are for performance enhancements (searching for objects in the array quicker)?. Also I couldn't seem to find the advantage of using aList
over aMutableList
?
– Zorgan
Jan 19 at 21:22
1
In a lot of modern programming, we try to use immutable objects where possible. Kotlin emphasizes this (val
versusvar
,MutableList
versusList
, etc.). Immutability has a lot of positive benefits (e.g., two threads can access the object simultaneously without conflict).
– CommonsWare
Jan 19 at 21:26
Do you mean
mutableListOf()
?– CommonsWare
Jan 19 at 21:11
Do you mean
mutableListOf()
?– CommonsWare
Jan 19 at 21:11
Yes
mutableListOf()
, edited– Zorgan
Jan 19 at 21:11
Yes
mutableListOf()
, edited– Zorgan
Jan 19 at 21:11
It feels like this is a duplicate of your question. Is there something specific that you are interested in that is not covered in that question and its answers?
– CommonsWare
Jan 19 at 21:15
It feels like this is a duplicate of your question. Is there something specific that you are interested in that is not covered in that question and its answers?
– CommonsWare
Jan 19 at 21:15
That's a good explanation thanks. So mainly the only reason to use arrays are for performance enhancements (searching for objects in the array quicker)?. Also I couldn't seem to find the advantage of using a
List
over a MutableList
?– Zorgan
Jan 19 at 21:22
That's a good explanation thanks. So mainly the only reason to use arrays are for performance enhancements (searching for objects in the array quicker)?. Also I couldn't seem to find the advantage of using a
List
over a MutableList
?– Zorgan
Jan 19 at 21:22
1
1
In a lot of modern programming, we try to use immutable objects where possible. Kotlin emphasizes this (
val
versus var
, MutableList
versus List
, etc.). Immutability has a lot of positive benefits (e.g., two threads can access the object simultaneously without conflict).– CommonsWare
Jan 19 at 21:26
In a lot of modern programming, we try to use immutable objects where possible. Kotlin emphasizes this (
val
versus var
, MutableList
versus List
, etc.). Immutability has a lot of positive benefits (e.g., two threads can access the object simultaneously without conflict).– CommonsWare
Jan 19 at 21:26
|
show 2 more comments
1 Answer
1
active
oldest
votes
They are not the same:
arrayOf()
creates an Array
and mutableListOf()
an ArrayList
under the hood.
Which one you choose depends on your application.
An array is a fixed size data structure. In order to add/remove values you need to create a copy of the old array with the new changes (which is expensive), but no matter you large the array, accessing an element has constant time complexity. You need to do that manually though.
The ArrayList
on the other hand uses an array for internal representation as well, but provides you with a mutable interface. So, adding/removing an element will still causes the internal array to be copied, but this process is abstracted away from you.
Conclusion:
- If you need to initialize the list once and after hat only access it, you should use an
Array
. - If you need to add/remove elements in very few instances and the majority is reading, you should use an
ArrayList
. - If you need to constantly add/remove values, use a
LinkedList
.
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
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%2f54271416%2fwhy-is-arrayof-the-same-as-mutablelistof-but-limited%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
They are not the same:
arrayOf()
creates an Array
and mutableListOf()
an ArrayList
under the hood.
Which one you choose depends on your application.
An array is a fixed size data structure. In order to add/remove values you need to create a copy of the old array with the new changes (which is expensive), but no matter you large the array, accessing an element has constant time complexity. You need to do that manually though.
The ArrayList
on the other hand uses an array for internal representation as well, but provides you with a mutable interface. So, adding/removing an element will still causes the internal array to be copied, but this process is abstracted away from you.
Conclusion:
- If you need to initialize the list once and after hat only access it, you should use an
Array
. - If you need to add/remove elements in very few instances and the majority is reading, you should use an
ArrayList
. - If you need to constantly add/remove values, use a
LinkedList
.
add a comment |
They are not the same:
arrayOf()
creates an Array
and mutableListOf()
an ArrayList
under the hood.
Which one you choose depends on your application.
An array is a fixed size data structure. In order to add/remove values you need to create a copy of the old array with the new changes (which is expensive), but no matter you large the array, accessing an element has constant time complexity. You need to do that manually though.
The ArrayList
on the other hand uses an array for internal representation as well, but provides you with a mutable interface. So, adding/removing an element will still causes the internal array to be copied, but this process is abstracted away from you.
Conclusion:
- If you need to initialize the list once and after hat only access it, you should use an
Array
. - If you need to add/remove elements in very few instances and the majority is reading, you should use an
ArrayList
. - If you need to constantly add/remove values, use a
LinkedList
.
add a comment |
They are not the same:
arrayOf()
creates an Array
and mutableListOf()
an ArrayList
under the hood.
Which one you choose depends on your application.
An array is a fixed size data structure. In order to add/remove values you need to create a copy of the old array with the new changes (which is expensive), but no matter you large the array, accessing an element has constant time complexity. You need to do that manually though.
The ArrayList
on the other hand uses an array for internal representation as well, but provides you with a mutable interface. So, adding/removing an element will still causes the internal array to be copied, but this process is abstracted away from you.
Conclusion:
- If you need to initialize the list once and after hat only access it, you should use an
Array
. - If you need to add/remove elements in very few instances and the majority is reading, you should use an
ArrayList
. - If you need to constantly add/remove values, use a
LinkedList
.
They are not the same:
arrayOf()
creates an Array
and mutableListOf()
an ArrayList
under the hood.
Which one you choose depends on your application.
An array is a fixed size data structure. In order to add/remove values you need to create a copy of the old array with the new changes (which is expensive), but no matter you large the array, accessing an element has constant time complexity. You need to do that manually though.
The ArrayList
on the other hand uses an array for internal representation as well, but provides you with a mutable interface. So, adding/removing an element will still causes the internal array to be copied, but this process is abstracted away from you.
Conclusion:
- If you need to initialize the list once and after hat only access it, you should use an
Array
. - If you need to add/remove elements in very few instances and the majority is reading, you should use an
ArrayList
. - If you need to constantly add/remove values, use a
LinkedList
.
edited Jan 21 at 19:39
answered Jan 20 at 9:39
Willi MentzelWilli Mentzel
9,758114670
9,758114670
add a comment |
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.
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%2f54271416%2fwhy-is-arrayof-the-same-as-mutablelistof-but-limited%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
Do you mean
mutableListOf()
?– CommonsWare
Jan 19 at 21:11
Yes
mutableListOf()
, edited– Zorgan
Jan 19 at 21:11
It feels like this is a duplicate of your question. Is there something specific that you are interested in that is not covered in that question and its answers?
– CommonsWare
Jan 19 at 21:15
That's a good explanation thanks. So mainly the only reason to use arrays are for performance enhancements (searching for objects in the array quicker)?. Also I couldn't seem to find the advantage of using a
List
over aMutableList
?– Zorgan
Jan 19 at 21:22
1
In a lot of modern programming, we try to use immutable objects where possible. Kotlin emphasizes this (
val
versusvar
,MutableList
versusList
, etc.). Immutability has a lot of positive benefits (e.g., two threads can access the object simultaneously without conflict).– CommonsWare
Jan 19 at 21:26