Difference between Arrays and Collections

Arrays
Collections
Can hold only homogeneous elements.
All elements should of same data type, either all are int or String etc.
Can hold heterogeneous elements.
All the elements can be of mix of data types, some can be int and rest can be of String etc.
Array size is fixed, once declared we can’t alter the size,
If we want to enter more elements than the size declared, we can’t
If we are storing elements less than the size, then we have memory wastage.
Size can be dynamic, as we enter, it changes the size.
Performance is more in arrays. (condition: we should know the size prior)
Performance is low,
When we insert new element to collections, it recreates the collections object with more size and copies the old content and then append the new element, then throws the old object variable to garbage collection.
[see below table, explanation]
There are not much predefined methods to handle Arrays.
e.g-Sorting, Inserting element in some order, Searching an element (for all this operations we need to write our own logics)
There are different predefined methods to act on collections.
Logics are available in predefined methods.
Array can hold both primitives and Object types.
Array can hold only Object types.
Obj[]  – XYZ location
0
1
2
3
4
If we insert an element, it doesn’t append to the above, rather it creates another space and copies whole content.
Obj[] – ABC location
0
1
2
3
4
5
Then, it disposes the obj[] from the XYZ location –
0
1
2
3
4

Content posted is based on learning or working experience, Share the post with others if find useful, Any queries?, leave comments or discuss on our facebook:qavalidation.com, Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: