Hi all, I do not understand why do we need both array and collection in Java ? I mean, aren't both of them the same - "a group of objects as a single unit" ? Why do we still need array (to exist) when the collection framework is so much better... giving us so many facilities to manipulate "a group of objects" ?
Arrays and Collections are complementary. Each have their own advantages and drawbacks. In some case, it is better to simply use arrays instead of Collections. For instance, say you want to store a sequence of integer primitives. If we only had collections, we would have to wrap each integer primitive in an java.lang.Integer. Imagine you have to store 2 millions (or more) ints. You would create lots of objects for nothing. Arrays further provide type safety. You know that an array declared as Component comp = new Component; will only contain objects whose type is Component or a subclass thereof. You don't have that guarantee will Collections (yet, see JSR 14: Add Generic Types To The JavaTM Programming Language). Collections are generic, that is, everything you get out of them will be an Object that you will have to cast to the correct type. Frankly, choosing between Collections and Arrays is not always a piece of cake, your application may suffer from bad choices. Check out Java tutorial: What are the Benefits of a Collections Framework? to see what advantages the Collections framework provide. I hope this clears some of your doubts and if not let's just continue this dicussion