Collection is the very hot topic in java. Every one would have experienced that, collection will play the important role in interview. Now we will discuss about need of collection. If we want to represent a value in program, we can declare a variable int x = 10; If we want to represent two values in program, we can declare two variables int x = 10; int y = 20; If we want to represent three values in program, we can declare three variables int x = 10; int y = 20; int z = 30; Suppose there is requirement to represent thousand values, declaring thousand variables, is not a good coding practise and readability of the code will be down. So, to overcome the above said problem, we should go for an Arrays concept int[] s = new int[1000]; s[0] = 10; s[1] = 20; s[2] = 30; . . . s[999] = 10000; Advantage in Array is, we can represent group of individual object in a single variable, but there are some disadvantages in Arrays,
- Arrays are Fixed in size: Once Array is declared with some size, there is no option to increase or decrease size at run time.
- Arrays can hold only Homogeneous data type elements: In above example, we have created an int Array 's', so variable 's' can hold only int values.
- Array concept is not implemented based on any data structure: We can not expect any predefined methods from Arrays, for every requirement we have to write the code.
- Collections are growable in nature: Collection size can be increased or decreased at run time
- Collections can hold both Homogeneous and Heterogeneous data type elements: Collection can hold any data type elements
- Collections are implemented based on some standard data structure: Collections are providing predefined method support
No comments:
Post a Comment