Kotlin Lists and Arrays [duplicate]
This question already has an answer here:
Difference between List and Array types in Kotlin
3 answers
Just started working with Kotlin and I love it but...
I can not make any sense of Lists and Arrys in this language.
I'm not new to programming and do not need an explanation on what arrays are. What I do not understand is.
What is the difference between a List and an Array? They seem very much the same you access both using a[index] and use them in much the same way. If a list is immutable they are even more the same, so... What is the difference? Assuming the list is not a linked list they both work in O(1) access time.
If I'm using a list; What is the difference between mutable and immutable? When can I edit the content? When can I change the length?
There seem to be many overlapping and confusing names for the same thing. List, ListOf, ArrayList, IntArray, intArray....
Could someone make an exhaustive list of all of them and give some kind of rule of thumb when you would use every one. Specifically, I find the concept of an immutable empty list very perplexing. What on earth would that be used for?How do you initialize these things?
Sorry for the long question,
Thanks.
kotlin
marked as duplicate by JEY, Lino, hotkey
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 14 '18 at 17:10
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Difference between List and Array types in Kotlin
3 answers
Just started working with Kotlin and I love it but...
I can not make any sense of Lists and Arrys in this language.
I'm not new to programming and do not need an explanation on what arrays are. What I do not understand is.
What is the difference between a List and an Array? They seem very much the same you access both using a[index] and use them in much the same way. If a list is immutable they are even more the same, so... What is the difference? Assuming the list is not a linked list they both work in O(1) access time.
If I'm using a list; What is the difference between mutable and immutable? When can I edit the content? When can I change the length?
There seem to be many overlapping and confusing names for the same thing. List, ListOf, ArrayList, IntArray, intArray....
Could someone make an exhaustive list of all of them and give some kind of rule of thumb when you would use every one. Specifically, I find the concept of an immutable empty list very perplexing. What on earth would that be used for?How do you initialize these things?
Sorry for the long question,
Thanks.
kotlin
marked as duplicate by JEY, Lino, hotkey
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 14 '18 at 17:10
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
You are correct but I looked at the answers there and came out the other end as confused as I went in.
– yoni
Nov 14 '18 at 14:13
It answers precisely the questions you have including references to further reading. How would you expect a "better" answer to look like?
– UnholySheep
Nov 14 '18 at 14:17
Coming from C/C++ the multitude of different names is very confusing. The technicality of the implementation behind the scenes (implementing an interface vs being a proper object etc..., will be interesting to me once I understand the logic/point of the different entities their naming). After reading the documentation and the above-referenced answer, I still wouldn't know how to init an array/list or when would be the appropriate place to use an ArrayList vs List<Int> vs IntArray vs intArray..... I think what I need is the big picture if this is irrelevant, please delete the Q.
– yoni
Nov 14 '18 at 15:21
add a comment |
This question already has an answer here:
Difference between List and Array types in Kotlin
3 answers
Just started working with Kotlin and I love it but...
I can not make any sense of Lists and Arrys in this language.
I'm not new to programming and do not need an explanation on what arrays are. What I do not understand is.
What is the difference between a List and an Array? They seem very much the same you access both using a[index] and use them in much the same way. If a list is immutable they are even more the same, so... What is the difference? Assuming the list is not a linked list they both work in O(1) access time.
If I'm using a list; What is the difference between mutable and immutable? When can I edit the content? When can I change the length?
There seem to be many overlapping and confusing names for the same thing. List, ListOf, ArrayList, IntArray, intArray....
Could someone make an exhaustive list of all of them and give some kind of rule of thumb when you would use every one. Specifically, I find the concept of an immutable empty list very perplexing. What on earth would that be used for?How do you initialize these things?
Sorry for the long question,
Thanks.
kotlin
This question already has an answer here:
Difference between List and Array types in Kotlin
3 answers
Just started working with Kotlin and I love it but...
I can not make any sense of Lists and Arrys in this language.
I'm not new to programming and do not need an explanation on what arrays are. What I do not understand is.
What is the difference between a List and an Array? They seem very much the same you access both using a[index] and use them in much the same way. If a list is immutable they are even more the same, so... What is the difference? Assuming the list is not a linked list they both work in O(1) access time.
If I'm using a list; What is the difference between mutable and immutable? When can I edit the content? When can I change the length?
There seem to be many overlapping and confusing names for the same thing. List, ListOf, ArrayList, IntArray, intArray....
Could someone make an exhaustive list of all of them and give some kind of rule of thumb when you would use every one. Specifically, I find the concept of an immutable empty list very perplexing. What on earth would that be used for?How do you initialize these things?
Sorry for the long question,
Thanks.
This question already has an answer here:
Difference between List and Array types in Kotlin
3 answers
kotlin
kotlin
asked Nov 14 '18 at 13:23
yoniyoni
61
61
marked as duplicate by JEY, Lino, hotkey
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 14 '18 at 17:10
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by JEY, Lino, hotkey
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 14 '18 at 17:10
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
You are correct but I looked at the answers there and came out the other end as confused as I went in.
– yoni
Nov 14 '18 at 14:13
It answers precisely the questions you have including references to further reading. How would you expect a "better" answer to look like?
– UnholySheep
Nov 14 '18 at 14:17
Coming from C/C++ the multitude of different names is very confusing. The technicality of the implementation behind the scenes (implementing an interface vs being a proper object etc..., will be interesting to me once I understand the logic/point of the different entities their naming). After reading the documentation and the above-referenced answer, I still wouldn't know how to init an array/list or when would be the appropriate place to use an ArrayList vs List<Int> vs IntArray vs intArray..... I think what I need is the big picture if this is irrelevant, please delete the Q.
– yoni
Nov 14 '18 at 15:21
add a comment |
You are correct but I looked at the answers there and came out the other end as confused as I went in.
– yoni
Nov 14 '18 at 14:13
It answers precisely the questions you have including references to further reading. How would you expect a "better" answer to look like?
– UnholySheep
Nov 14 '18 at 14:17
Coming from C/C++ the multitude of different names is very confusing. The technicality of the implementation behind the scenes (implementing an interface vs being a proper object etc..., will be interesting to me once I understand the logic/point of the different entities their naming). After reading the documentation and the above-referenced answer, I still wouldn't know how to init an array/list or when would be the appropriate place to use an ArrayList vs List<Int> vs IntArray vs intArray..... I think what I need is the big picture if this is irrelevant, please delete the Q.
– yoni
Nov 14 '18 at 15:21
You are correct but I looked at the answers there and came out the other end as confused as I went in.
– yoni
Nov 14 '18 at 14:13
You are correct but I looked at the answers there and came out the other end as confused as I went in.
– yoni
Nov 14 '18 at 14:13
It answers precisely the questions you have including references to further reading. How would you expect a "better" answer to look like?
– UnholySheep
Nov 14 '18 at 14:17
It answers precisely the questions you have including references to further reading. How would you expect a "better" answer to look like?
– UnholySheep
Nov 14 '18 at 14:17
Coming from C/C++ the multitude of different names is very confusing. The technicality of the implementation behind the scenes (implementing an interface vs being a proper object etc..., will be interesting to me once I understand the logic/point of the different entities their naming). After reading the documentation and the above-referenced answer, I still wouldn't know how to init an array/list or when would be the appropriate place to use an ArrayList vs List<Int> vs IntArray vs intArray..... I think what I need is the big picture if this is irrelevant, please delete the Q.
– yoni
Nov 14 '18 at 15:21
Coming from C/C++ the multitude of different names is very confusing. The technicality of the implementation behind the scenes (implementing an interface vs being a proper object etc..., will be interesting to me once I understand the logic/point of the different entities their naming). After reading the documentation and the above-referenced answer, I still wouldn't know how to init an array/list or when would be the appropriate place to use an ArrayList vs List<Int> vs IntArray vs intArray..... I think what I need is the big picture if this is irrelevant, please delete the Q.
– yoni
Nov 14 '18 at 15:21
add a comment |
2 Answers
2
active
oldest
votes
- First difference is that
List
is interface describing some common list operations, whileArray
is a class. From memory perspective,Array
is continuous region in memory which size doesn't change, that is why you can't change the size ofArray
after it is created, but you can change its elements, on other handList
can be implemented in different ways, meaning that memory structure can be different, most common implementations areArrayList
where array is used to store elements, and once array is filled, its changed with bigger array with contents of old one being added to new one, another implementation isLinkedList
, where you have nodes pointing to next element on list. From performance perspectiveArray
is always faster than any implementation ofList
but it is also much more limited. Difference between
List
andMutableList
is that when you useMutableList
you can change elements of that list(add or remove elements from it), while when using immutableList
you can't add or remove elements from it. Both lists allow you to change properties of those elements.
Will divide this answer into three answers:
List
is the interface which extendsCollection
interface, provides basic common list operations,MutableList
extendsList
interface as well asMutableCollection
interface adding methods needed to change elements of that list,listOf
is function which createsList
and fills it with given arguments, by usinglistOf
we don't need to specify which implementation ofList
will be used, for example on JVMList
is backed byjava.util.Arrays.ArrayList
(not same asjava.util.ArrayList
), while on JavaScript side it is probably backed up byArray
(take this statement with grain of salt, as I have never worked with Kotlin for JS)
ArrayList
is typealias tojava.util.ArrayList
, there is nothing special about it, it is implemenentation of Java'sList
interface,MutableList
is backed by this implementation on JVM.
Array
is equivalent to Java's array, nothing special for it either,IntArray
and other primitive array company is used to make up for the lack of primitive types in kotlin,Array<Int>
is same asInteger
in Java, whileIntArray
is same asint
. Same logic is applied to all other variants. Using primitive types you get better performance, but difference can be neglected in most cases on modern computers, still if you have really a lot of data you should go for primitive types where possible.
You can see yourself all collections hierarchy on kotlin repository
- Use built-in Kotlin functions like
listOf
,arrayOf
,mutableListOf
, this isn't a must, but its always good to follow best practices.
Thanks, this was a help
– yoni
Nov 14 '18 at 15:30
1
Would it be correct to say. If it is preformance critical use an array, otherwise use a list. If using a list try to init it usinglistOf
mutableListOf
and let the compiler deside what kind of list to create. So creating a mutable ist of 100 cells init to 0 would look likeval l2 = mutableListOf((1..100).map{0})
– yoni
Nov 14 '18 at 15:44
mentval l2 = (1..100).map{0}.toMutableList()
– yoni
Nov 14 '18 at 15:51
Yes array is better for performance critical tasks, otherwise lists are preferred as they offer a lot more flexibility. For initializing lists yes using those factory functions is the way to go.
– FilipRistic
Nov 14 '18 at 18:45
add a comment |
Coming from C/C++ the multitude of different names is very confusing.
Then maybe this can give C++ analogy specifically:
Array
is likestd::array
(though length doesn't need to be known at compile time), or like C arrays, except it stores the length and all accesses are bounds-checked.ArrayList
is likestd::vector
(again, all accesses are bounds-checked).MutableList
is the interface toArrayList
(likeSequenceContainer
).List
is the read-only part ofMutableList
.
Generics work very differently from C++ templates, in particular there's no specialization: in C++, there is separate code generated for std::vector<int>
and std::vector<std::string>
, in Java and Kotlin there isn't. (Actually, Kotlin has a form of it with reified type parameters, but it doesn't apply here.) So e.g. Array<Int>
and List<Int>
have to work with boxed java.lang.Integer
s instead of primitive types. But Java does have arrays of primitives, and that's what Kotlin calls IntArray
.
Thanks. Every one really helped me a lot
– yoni
Nov 18 '18 at 13:25
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
- First difference is that
List
is interface describing some common list operations, whileArray
is a class. From memory perspective,Array
is continuous region in memory which size doesn't change, that is why you can't change the size ofArray
after it is created, but you can change its elements, on other handList
can be implemented in different ways, meaning that memory structure can be different, most common implementations areArrayList
where array is used to store elements, and once array is filled, its changed with bigger array with contents of old one being added to new one, another implementation isLinkedList
, where you have nodes pointing to next element on list. From performance perspectiveArray
is always faster than any implementation ofList
but it is also much more limited. Difference between
List
andMutableList
is that when you useMutableList
you can change elements of that list(add or remove elements from it), while when using immutableList
you can't add or remove elements from it. Both lists allow you to change properties of those elements.
Will divide this answer into three answers:
List
is the interface which extendsCollection
interface, provides basic common list operations,MutableList
extendsList
interface as well asMutableCollection
interface adding methods needed to change elements of that list,listOf
is function which createsList
and fills it with given arguments, by usinglistOf
we don't need to specify which implementation ofList
will be used, for example on JVMList
is backed byjava.util.Arrays.ArrayList
(not same asjava.util.ArrayList
), while on JavaScript side it is probably backed up byArray
(take this statement with grain of salt, as I have never worked with Kotlin for JS)
ArrayList
is typealias tojava.util.ArrayList
, there is nothing special about it, it is implemenentation of Java'sList
interface,MutableList
is backed by this implementation on JVM.
Array
is equivalent to Java's array, nothing special for it either,IntArray
and other primitive array company is used to make up for the lack of primitive types in kotlin,Array<Int>
is same asInteger
in Java, whileIntArray
is same asint
. Same logic is applied to all other variants. Using primitive types you get better performance, but difference can be neglected in most cases on modern computers, still if you have really a lot of data you should go for primitive types where possible.
You can see yourself all collections hierarchy on kotlin repository
- Use built-in Kotlin functions like
listOf
,arrayOf
,mutableListOf
, this isn't a must, but its always good to follow best practices.
Thanks, this was a help
– yoni
Nov 14 '18 at 15:30
1
Would it be correct to say. If it is preformance critical use an array, otherwise use a list. If using a list try to init it usinglistOf
mutableListOf
and let the compiler deside what kind of list to create. So creating a mutable ist of 100 cells init to 0 would look likeval l2 = mutableListOf((1..100).map{0})
– yoni
Nov 14 '18 at 15:44
mentval l2 = (1..100).map{0}.toMutableList()
– yoni
Nov 14 '18 at 15:51
Yes array is better for performance critical tasks, otherwise lists are preferred as they offer a lot more flexibility. For initializing lists yes using those factory functions is the way to go.
– FilipRistic
Nov 14 '18 at 18:45
add a comment |
- First difference is that
List
is interface describing some common list operations, whileArray
is a class. From memory perspective,Array
is continuous region in memory which size doesn't change, that is why you can't change the size ofArray
after it is created, but you can change its elements, on other handList
can be implemented in different ways, meaning that memory structure can be different, most common implementations areArrayList
where array is used to store elements, and once array is filled, its changed with bigger array with contents of old one being added to new one, another implementation isLinkedList
, where you have nodes pointing to next element on list. From performance perspectiveArray
is always faster than any implementation ofList
but it is also much more limited. Difference between
List
andMutableList
is that when you useMutableList
you can change elements of that list(add or remove elements from it), while when using immutableList
you can't add or remove elements from it. Both lists allow you to change properties of those elements.
Will divide this answer into three answers:
List
is the interface which extendsCollection
interface, provides basic common list operations,MutableList
extendsList
interface as well asMutableCollection
interface adding methods needed to change elements of that list,listOf
is function which createsList
and fills it with given arguments, by usinglistOf
we don't need to specify which implementation ofList
will be used, for example on JVMList
is backed byjava.util.Arrays.ArrayList
(not same asjava.util.ArrayList
), while on JavaScript side it is probably backed up byArray
(take this statement with grain of salt, as I have never worked with Kotlin for JS)
ArrayList
is typealias tojava.util.ArrayList
, there is nothing special about it, it is implemenentation of Java'sList
interface,MutableList
is backed by this implementation on JVM.
Array
is equivalent to Java's array, nothing special for it either,IntArray
and other primitive array company is used to make up for the lack of primitive types in kotlin,Array<Int>
is same asInteger
in Java, whileIntArray
is same asint
. Same logic is applied to all other variants. Using primitive types you get better performance, but difference can be neglected in most cases on modern computers, still if you have really a lot of data you should go for primitive types where possible.
You can see yourself all collections hierarchy on kotlin repository
- Use built-in Kotlin functions like
listOf
,arrayOf
,mutableListOf
, this isn't a must, but its always good to follow best practices.
Thanks, this was a help
– yoni
Nov 14 '18 at 15:30
1
Would it be correct to say. If it is preformance critical use an array, otherwise use a list. If using a list try to init it usinglistOf
mutableListOf
and let the compiler deside what kind of list to create. So creating a mutable ist of 100 cells init to 0 would look likeval l2 = mutableListOf((1..100).map{0})
– yoni
Nov 14 '18 at 15:44
mentval l2 = (1..100).map{0}.toMutableList()
– yoni
Nov 14 '18 at 15:51
Yes array is better for performance critical tasks, otherwise lists are preferred as they offer a lot more flexibility. For initializing lists yes using those factory functions is the way to go.
– FilipRistic
Nov 14 '18 at 18:45
add a comment |
- First difference is that
List
is interface describing some common list operations, whileArray
is a class. From memory perspective,Array
is continuous region in memory which size doesn't change, that is why you can't change the size ofArray
after it is created, but you can change its elements, on other handList
can be implemented in different ways, meaning that memory structure can be different, most common implementations areArrayList
where array is used to store elements, and once array is filled, its changed with bigger array with contents of old one being added to new one, another implementation isLinkedList
, where you have nodes pointing to next element on list. From performance perspectiveArray
is always faster than any implementation ofList
but it is also much more limited. Difference between
List
andMutableList
is that when you useMutableList
you can change elements of that list(add or remove elements from it), while when using immutableList
you can't add or remove elements from it. Both lists allow you to change properties of those elements.
Will divide this answer into three answers:
List
is the interface which extendsCollection
interface, provides basic common list operations,MutableList
extendsList
interface as well asMutableCollection
interface adding methods needed to change elements of that list,listOf
is function which createsList
and fills it with given arguments, by usinglistOf
we don't need to specify which implementation ofList
will be used, for example on JVMList
is backed byjava.util.Arrays.ArrayList
(not same asjava.util.ArrayList
), while on JavaScript side it is probably backed up byArray
(take this statement with grain of salt, as I have never worked with Kotlin for JS)
ArrayList
is typealias tojava.util.ArrayList
, there is nothing special about it, it is implemenentation of Java'sList
interface,MutableList
is backed by this implementation on JVM.
Array
is equivalent to Java's array, nothing special for it either,IntArray
and other primitive array company is used to make up for the lack of primitive types in kotlin,Array<Int>
is same asInteger
in Java, whileIntArray
is same asint
. Same logic is applied to all other variants. Using primitive types you get better performance, but difference can be neglected in most cases on modern computers, still if you have really a lot of data you should go for primitive types where possible.
You can see yourself all collections hierarchy on kotlin repository
- Use built-in Kotlin functions like
listOf
,arrayOf
,mutableListOf
, this isn't a must, but its always good to follow best practices.
- First difference is that
List
is interface describing some common list operations, whileArray
is a class. From memory perspective,Array
is continuous region in memory which size doesn't change, that is why you can't change the size ofArray
after it is created, but you can change its elements, on other handList
can be implemented in different ways, meaning that memory structure can be different, most common implementations areArrayList
where array is used to store elements, and once array is filled, its changed with bigger array with contents of old one being added to new one, another implementation isLinkedList
, where you have nodes pointing to next element on list. From performance perspectiveArray
is always faster than any implementation ofList
but it is also much more limited. Difference between
List
andMutableList
is that when you useMutableList
you can change elements of that list(add or remove elements from it), while when using immutableList
you can't add or remove elements from it. Both lists allow you to change properties of those elements.
Will divide this answer into three answers:
List
is the interface which extendsCollection
interface, provides basic common list operations,MutableList
extendsList
interface as well asMutableCollection
interface adding methods needed to change elements of that list,listOf
is function which createsList
and fills it with given arguments, by usinglistOf
we don't need to specify which implementation ofList
will be used, for example on JVMList
is backed byjava.util.Arrays.ArrayList
(not same asjava.util.ArrayList
), while on JavaScript side it is probably backed up byArray
(take this statement with grain of salt, as I have never worked with Kotlin for JS)
ArrayList
is typealias tojava.util.ArrayList
, there is nothing special about it, it is implemenentation of Java'sList
interface,MutableList
is backed by this implementation on JVM.
Array
is equivalent to Java's array, nothing special for it either,IntArray
and other primitive array company is used to make up for the lack of primitive types in kotlin,Array<Int>
is same asInteger
in Java, whileIntArray
is same asint
. Same logic is applied to all other variants. Using primitive types you get better performance, but difference can be neglected in most cases on modern computers, still if you have really a lot of data you should go for primitive types where possible.
You can see yourself all collections hierarchy on kotlin repository
- Use built-in Kotlin functions like
listOf
,arrayOf
,mutableListOf
, this isn't a must, but its always good to follow best practices.
answered Nov 14 '18 at 14:46
FilipRisticFilipRistic
1,36441221
1,36441221
Thanks, this was a help
– yoni
Nov 14 '18 at 15:30
1
Would it be correct to say. If it is preformance critical use an array, otherwise use a list. If using a list try to init it usinglistOf
mutableListOf
and let the compiler deside what kind of list to create. So creating a mutable ist of 100 cells init to 0 would look likeval l2 = mutableListOf((1..100).map{0})
– yoni
Nov 14 '18 at 15:44
mentval l2 = (1..100).map{0}.toMutableList()
– yoni
Nov 14 '18 at 15:51
Yes array is better for performance critical tasks, otherwise lists are preferred as they offer a lot more flexibility. For initializing lists yes using those factory functions is the way to go.
– FilipRistic
Nov 14 '18 at 18:45
add a comment |
Thanks, this was a help
– yoni
Nov 14 '18 at 15:30
1
Would it be correct to say. If it is preformance critical use an array, otherwise use a list. If using a list try to init it usinglistOf
mutableListOf
and let the compiler deside what kind of list to create. So creating a mutable ist of 100 cells init to 0 would look likeval l2 = mutableListOf((1..100).map{0})
– yoni
Nov 14 '18 at 15:44
mentval l2 = (1..100).map{0}.toMutableList()
– yoni
Nov 14 '18 at 15:51
Yes array is better for performance critical tasks, otherwise lists are preferred as they offer a lot more flexibility. For initializing lists yes using those factory functions is the way to go.
– FilipRistic
Nov 14 '18 at 18:45
Thanks, this was a help
– yoni
Nov 14 '18 at 15:30
Thanks, this was a help
– yoni
Nov 14 '18 at 15:30
1
1
Would it be correct to say. If it is preformance critical use an array, otherwise use a list. If using a list try to init it using
listOf
mutableListOf
and let the compiler deside what kind of list to create. So creating a mutable ist of 100 cells init to 0 would look like val l2 = mutableListOf((1..100).map{0})
– yoni
Nov 14 '18 at 15:44
Would it be correct to say. If it is preformance critical use an array, otherwise use a list. If using a list try to init it using
listOf
mutableListOf
and let the compiler deside what kind of list to create. So creating a mutable ist of 100 cells init to 0 would look like val l2 = mutableListOf((1..100).map{0})
– yoni
Nov 14 '18 at 15:44
ment
val l2 = (1..100).map{0}.toMutableList()
– yoni
Nov 14 '18 at 15:51
ment
val l2 = (1..100).map{0}.toMutableList()
– yoni
Nov 14 '18 at 15:51
Yes array is better for performance critical tasks, otherwise lists are preferred as they offer a lot more flexibility. For initializing lists yes using those factory functions is the way to go.
– FilipRistic
Nov 14 '18 at 18:45
Yes array is better for performance critical tasks, otherwise lists are preferred as they offer a lot more flexibility. For initializing lists yes using those factory functions is the way to go.
– FilipRistic
Nov 14 '18 at 18:45
add a comment |
Coming from C/C++ the multitude of different names is very confusing.
Then maybe this can give C++ analogy specifically:
Array
is likestd::array
(though length doesn't need to be known at compile time), or like C arrays, except it stores the length and all accesses are bounds-checked.ArrayList
is likestd::vector
(again, all accesses are bounds-checked).MutableList
is the interface toArrayList
(likeSequenceContainer
).List
is the read-only part ofMutableList
.
Generics work very differently from C++ templates, in particular there's no specialization: in C++, there is separate code generated for std::vector<int>
and std::vector<std::string>
, in Java and Kotlin there isn't. (Actually, Kotlin has a form of it with reified type parameters, but it doesn't apply here.) So e.g. Array<Int>
and List<Int>
have to work with boxed java.lang.Integer
s instead of primitive types. But Java does have arrays of primitives, and that's what Kotlin calls IntArray
.
Thanks. Every one really helped me a lot
– yoni
Nov 18 '18 at 13:25
add a comment |
Coming from C/C++ the multitude of different names is very confusing.
Then maybe this can give C++ analogy specifically:
Array
is likestd::array
(though length doesn't need to be known at compile time), or like C arrays, except it stores the length and all accesses are bounds-checked.ArrayList
is likestd::vector
(again, all accesses are bounds-checked).MutableList
is the interface toArrayList
(likeSequenceContainer
).List
is the read-only part ofMutableList
.
Generics work very differently from C++ templates, in particular there's no specialization: in C++, there is separate code generated for std::vector<int>
and std::vector<std::string>
, in Java and Kotlin there isn't. (Actually, Kotlin has a form of it with reified type parameters, but it doesn't apply here.) So e.g. Array<Int>
and List<Int>
have to work with boxed java.lang.Integer
s instead of primitive types. But Java does have arrays of primitives, and that's what Kotlin calls IntArray
.
Thanks. Every one really helped me a lot
– yoni
Nov 18 '18 at 13:25
add a comment |
Coming from C/C++ the multitude of different names is very confusing.
Then maybe this can give C++ analogy specifically:
Array
is likestd::array
(though length doesn't need to be known at compile time), or like C arrays, except it stores the length and all accesses are bounds-checked.ArrayList
is likestd::vector
(again, all accesses are bounds-checked).MutableList
is the interface toArrayList
(likeSequenceContainer
).List
is the read-only part ofMutableList
.
Generics work very differently from C++ templates, in particular there's no specialization: in C++, there is separate code generated for std::vector<int>
and std::vector<std::string>
, in Java and Kotlin there isn't. (Actually, Kotlin has a form of it with reified type parameters, but it doesn't apply here.) So e.g. Array<Int>
and List<Int>
have to work with boxed java.lang.Integer
s instead of primitive types. But Java does have arrays of primitives, and that's what Kotlin calls IntArray
.
Coming from C/C++ the multitude of different names is very confusing.
Then maybe this can give C++ analogy specifically:
Array
is likestd::array
(though length doesn't need to be known at compile time), or like C arrays, except it stores the length and all accesses are bounds-checked.ArrayList
is likestd::vector
(again, all accesses are bounds-checked).MutableList
is the interface toArrayList
(likeSequenceContainer
).List
is the read-only part ofMutableList
.
Generics work very differently from C++ templates, in particular there's no specialization: in C++, there is separate code generated for std::vector<int>
and std::vector<std::string>
, in Java and Kotlin there isn't. (Actually, Kotlin has a form of it with reified type parameters, but it doesn't apply here.) So e.g. Array<Int>
and List<Int>
have to work with boxed java.lang.Integer
s instead of primitive types. But Java does have arrays of primitives, and that's what Kotlin calls IntArray
.
answered Nov 14 '18 at 17:09
Alexey RomanovAlexey Romanov
107k25212353
107k25212353
Thanks. Every one really helped me a lot
– yoni
Nov 18 '18 at 13:25
add a comment |
Thanks. Every one really helped me a lot
– yoni
Nov 18 '18 at 13:25
Thanks. Every one really helped me a lot
– yoni
Nov 18 '18 at 13:25
Thanks. Every one really helped me a lot
– yoni
Nov 18 '18 at 13:25
add a comment |
You are correct but I looked at the answers there and came out the other end as confused as I went in.
– yoni
Nov 14 '18 at 14:13
It answers precisely the questions you have including references to further reading. How would you expect a "better" answer to look like?
– UnholySheep
Nov 14 '18 at 14:17
Coming from C/C++ the multitude of different names is very confusing. The technicality of the implementation behind the scenes (implementing an interface vs being a proper object etc..., will be interesting to me once I understand the logic/point of the different entities their naming). After reading the documentation and the above-referenced answer, I still wouldn't know how to init an array/list or when would be the appropriate place to use an ArrayList vs List<Int> vs IntArray vs intArray..... I think what I need is the big picture if this is irrelevant, please delete the Q.
– yoni
Nov 14 '18 at 15:21