Kotlin Lists and Arrays [duplicate]












0
















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.




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


  2. 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?


  3. 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?


  4. How do you initialize these things?



Sorry for the long question,



Thanks.










share|improve this question













marked as duplicate by JEY, Lino, hotkey kotlin
Users with the  kotlin badge can single-handedly close kotlin questions as duplicates and reopen them as needed.

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


















0
















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.




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


  2. 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?


  3. 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?


  4. How do you initialize these things?



Sorry for the long question,



Thanks.










share|improve this question













marked as duplicate by JEY, Lino, hotkey kotlin
Users with the  kotlin badge can single-handedly close kotlin questions as duplicates and reopen them as needed.

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
















0












0








0









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.




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


  2. 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?


  3. 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?


  4. How do you initialize these things?



Sorry for the long question,



Thanks.










share|improve this question















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.




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


  2. 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?


  3. 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?


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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 13:23









yoniyoni

61




61




marked as duplicate by JEY, Lino, hotkey kotlin
Users with the  kotlin badge can single-handedly close kotlin questions as duplicates and reopen them as needed.

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 kotlin
Users with the  kotlin badge can single-handedly close kotlin questions as duplicates and reopen them as needed.

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





















  • 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














2 Answers
2






active

oldest

votes


















1















  1. First difference is that List is interface describing some common list operations, while Array 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 of Array after it is created, but you can change its elements, on other hand List can be implemented in different ways, meaning that memory structure can be different, most common implementations are ArrayList 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 is LinkedList, where you have nodes pointing to next element on list. From performance perspective Array is always faster than any implementation of List but it is also much more limited.

  2. Difference between List and MutableList is that when you use MutableList you can change elements of that list(add or remove elements from it), while when using immutable List you can't add or remove elements from it. Both lists allow you to change properties of those elements.



  3. Will divide this answer into three answers:





    • List is the interface which extends Collection interface, provides basic common list operations, MutableList extends List interface as well as MutableCollection interface adding methods needed to change elements of that list, listOf is function which creates List and fills it with given arguments, by using listOf we don't need to specify which implementation of List will be used, for example on JVM List is backed by java.util.Arrays.ArrayList(not same as java.util.ArrayList), while on JavaScript side it is probably backed up by Array(take this statement with grain of salt, as I have never worked with Kotlin for JS)


    • ArrayList is typealias to java.util.ArrayList, there is nothing special about it, it is implemenentation of Java's List 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 as Integer in Java, while IntArray is same as int. 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




  1. Use built-in Kotlin functions like listOf, arrayOf, mutableListOf, this isn't a must, but its always good to follow best practices.






share|improve this answer
























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











  • 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





















0















Coming from C/C++ the multitude of different names is very confusing.




Then maybe this can give C++ analogy specifically:




  1. Array is like std::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.


  2. ArrayList is like std::vector (again, all accesses are bounds-checked).


  3. MutableList is the interface to ArrayList (like SequenceContainer).


  4. List is the read-only part of MutableList.



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.Integers instead of primitive types. But Java does have arrays of primitives, and that's what Kotlin calls IntArray.






share|improve this answer
























  • Thanks. Every one really helped me a lot

    – yoni
    Nov 18 '18 at 13:25


















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1















  1. First difference is that List is interface describing some common list operations, while Array 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 of Array after it is created, but you can change its elements, on other hand List can be implemented in different ways, meaning that memory structure can be different, most common implementations are ArrayList 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 is LinkedList, where you have nodes pointing to next element on list. From performance perspective Array is always faster than any implementation of List but it is also much more limited.

  2. Difference between List and MutableList is that when you use MutableList you can change elements of that list(add or remove elements from it), while when using immutable List you can't add or remove elements from it. Both lists allow you to change properties of those elements.



  3. Will divide this answer into three answers:





    • List is the interface which extends Collection interface, provides basic common list operations, MutableList extends List interface as well as MutableCollection interface adding methods needed to change elements of that list, listOf is function which creates List and fills it with given arguments, by using listOf we don't need to specify which implementation of List will be used, for example on JVM List is backed by java.util.Arrays.ArrayList(not same as java.util.ArrayList), while on JavaScript side it is probably backed up by Array(take this statement with grain of salt, as I have never worked with Kotlin for JS)


    • ArrayList is typealias to java.util.ArrayList, there is nothing special about it, it is implemenentation of Java's List 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 as Integer in Java, while IntArray is same as int. 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




  1. Use built-in Kotlin functions like listOf, arrayOf, mutableListOf, this isn't a must, but its always good to follow best practices.






share|improve this answer
























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











  • 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


















1















  1. First difference is that List is interface describing some common list operations, while Array 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 of Array after it is created, but you can change its elements, on other hand List can be implemented in different ways, meaning that memory structure can be different, most common implementations are ArrayList 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 is LinkedList, where you have nodes pointing to next element on list. From performance perspective Array is always faster than any implementation of List but it is also much more limited.

  2. Difference between List and MutableList is that when you use MutableList you can change elements of that list(add or remove elements from it), while when using immutable List you can't add or remove elements from it. Both lists allow you to change properties of those elements.



  3. Will divide this answer into three answers:





    • List is the interface which extends Collection interface, provides basic common list operations, MutableList extends List interface as well as MutableCollection interface adding methods needed to change elements of that list, listOf is function which creates List and fills it with given arguments, by using listOf we don't need to specify which implementation of List will be used, for example on JVM List is backed by java.util.Arrays.ArrayList(not same as java.util.ArrayList), while on JavaScript side it is probably backed up by Array(take this statement with grain of salt, as I have never worked with Kotlin for JS)


    • ArrayList is typealias to java.util.ArrayList, there is nothing special about it, it is implemenentation of Java's List 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 as Integer in Java, while IntArray is same as int. 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




  1. Use built-in Kotlin functions like listOf, arrayOf, mutableListOf, this isn't a must, but its always good to follow best practices.






share|improve this answer
























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











  • 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
















1












1








1








  1. First difference is that List is interface describing some common list operations, while Array 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 of Array after it is created, but you can change its elements, on other hand List can be implemented in different ways, meaning that memory structure can be different, most common implementations are ArrayList 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 is LinkedList, where you have nodes pointing to next element on list. From performance perspective Array is always faster than any implementation of List but it is also much more limited.

  2. Difference between List and MutableList is that when you use MutableList you can change elements of that list(add or remove elements from it), while when using immutable List you can't add or remove elements from it. Both lists allow you to change properties of those elements.



  3. Will divide this answer into three answers:





    • List is the interface which extends Collection interface, provides basic common list operations, MutableList extends List interface as well as MutableCollection interface adding methods needed to change elements of that list, listOf is function which creates List and fills it with given arguments, by using listOf we don't need to specify which implementation of List will be used, for example on JVM List is backed by java.util.Arrays.ArrayList(not same as java.util.ArrayList), while on JavaScript side it is probably backed up by Array(take this statement with grain of salt, as I have never worked with Kotlin for JS)


    • ArrayList is typealias to java.util.ArrayList, there is nothing special about it, it is implemenentation of Java's List 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 as Integer in Java, while IntArray is same as int. 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




  1. Use built-in Kotlin functions like listOf, arrayOf, mutableListOf, this isn't a must, but its always good to follow best practices.






share|improve this answer














  1. First difference is that List is interface describing some common list operations, while Array 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 of Array after it is created, but you can change its elements, on other hand List can be implemented in different ways, meaning that memory structure can be different, most common implementations are ArrayList 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 is LinkedList, where you have nodes pointing to next element on list. From performance perspective Array is always faster than any implementation of List but it is also much more limited.

  2. Difference between List and MutableList is that when you use MutableList you can change elements of that list(add or remove elements from it), while when using immutable List you can't add or remove elements from it. Both lists allow you to change properties of those elements.



  3. Will divide this answer into three answers:





    • List is the interface which extends Collection interface, provides basic common list operations, MutableList extends List interface as well as MutableCollection interface adding methods needed to change elements of that list, listOf is function which creates List and fills it with given arguments, by using listOf we don't need to specify which implementation of List will be used, for example on JVM List is backed by java.util.Arrays.ArrayList(not same as java.util.ArrayList), while on JavaScript side it is probably backed up by Array(take this statement with grain of salt, as I have never worked with Kotlin for JS)


    • ArrayList is typealias to java.util.ArrayList, there is nothing special about it, it is implemenentation of Java's List 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 as Integer in Java, while IntArray is same as int. 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




  1. Use built-in Kotlin functions like listOf, arrayOf, mutableListOf, this isn't a must, but its always good to follow best practices.







share|improve this answer












share|improve this answer



share|improve this answer










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











  • 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






  • 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











  • 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



















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















0















Coming from C/C++ the multitude of different names is very confusing.




Then maybe this can give C++ analogy specifically:




  1. Array is like std::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.


  2. ArrayList is like std::vector (again, all accesses are bounds-checked).


  3. MutableList is the interface to ArrayList (like SequenceContainer).


  4. List is the read-only part of MutableList.



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.Integers instead of primitive types. But Java does have arrays of primitives, and that's what Kotlin calls IntArray.






share|improve this answer
























  • Thanks. Every one really helped me a lot

    – yoni
    Nov 18 '18 at 13:25
















0















Coming from C/C++ the multitude of different names is very confusing.




Then maybe this can give C++ analogy specifically:




  1. Array is like std::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.


  2. ArrayList is like std::vector (again, all accesses are bounds-checked).


  3. MutableList is the interface to ArrayList (like SequenceContainer).


  4. List is the read-only part of MutableList.



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.Integers instead of primitive types. But Java does have arrays of primitives, and that's what Kotlin calls IntArray.






share|improve this answer
























  • Thanks. Every one really helped me a lot

    – yoni
    Nov 18 '18 at 13:25














0












0








0








Coming from C/C++ the multitude of different names is very confusing.




Then maybe this can give C++ analogy specifically:




  1. Array is like std::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.


  2. ArrayList is like std::vector (again, all accesses are bounds-checked).


  3. MutableList is the interface to ArrayList (like SequenceContainer).


  4. List is the read-only part of MutableList.



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.Integers instead of primitive types. But Java does have arrays of primitives, and that's what Kotlin calls IntArray.






share|improve this answer














Coming from C/C++ the multitude of different names is very confusing.




Then maybe this can give C++ analogy specifically:




  1. Array is like std::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.


  2. ArrayList is like std::vector (again, all accesses are bounds-checked).


  3. MutableList is the interface to ArrayList (like SequenceContainer).


  4. List is the read-only part of MutableList.



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.Integers instead of primitive types. But Java does have arrays of primitives, and that's what Kotlin calls IntArray.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 17:09









Alexey RomanovAlexey Romanov

107k26213353




107k26213353













  • 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





Thanks. Every one really helped me a lot

– yoni
Nov 18 '18 at 13:25



Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Lugert, Oklahoma