3

I am new in Java and Kotlin.

Recently when I read a tutorial while learning Kotlin.

I found there are some Array/List confusing me.

  1. What is the different between ArrayList, IntArray and Array<Int>?
  2. When should I use them?
Wee Hong
  • 585
  • 2
  • 8
  • 23
  • On the java side there is the fixed sized array: `int[]` (with the primitive type `int`) and dynamically sized `ArrayList` (with the int wrapper Integer). – Joop Eggen Jan 17 '18 at 15:51
  • 1
    This question basically duplicates two other questions: (1) [Difference between List and Array types in Kotlin](https://stackoverflow.com/questions/36262305/difference-between-list-and-array-types-in-kotlin) and (2) [IntArray vs Array in Kotlin](https://stackoverflow.com/questions/45090808/intarray-vs-arrayint-in-kotlin) – hotkey Jan 17 '18 at 15:57
  • @hotkey alright. thank you for pointing it out. – Wee Hong Jan 17 '18 at 16:09

3 Answers3

4

Major differences

ArrayList : resizable, Generic (Objects)

IntArray : primitive, fix length, only Int values

Array<Int> : Generic (Objects), fix length

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
3

I see multiples questions inside of your question and i gonna attempt to help you.

First question: What is the difference between ArrayList and List:

  • Array is static in size, ArrayList is autoresizable.
  • ArrayList can not contains primitive types (like int, char, ...), List can.

Second question: What is the difference between Array<Int> and IntArray

Check this question who respond to this question: IntArray vs Array<Int> in Kotlin

but in summary:

Array<Int> == Integer[] 
IntArray == int[]

That's it !

Valentin Michalak
  • 2,089
  • 1
  • 14
  • 27
0

This will partially answer your question: Difference between List and Array types in Kotlin

Besides, the difference between IntArray and Array<Int> is the same as between Java int[] and Integer[]: the former stores primitive integers without wrapping them while the latter boxes them into java.lang.Integer objects. Consider IntArray an optimized form of Array<Int> that does not introduce memory and boxing-unboxing overheads.

hotkey
  • 140,743
  • 39
  • 371
  • 326