• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Array vs Collection

 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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" ?

Ioow Gneb
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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[18];
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
 
Timothy Toe
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Valentin Crettaz:
I hope this clears some of your doubts


It sure does, thanks !
[ October 20, 2002: Message edited by: Ioow Gneb ]
 
You guys haven't done this much, have ya? I suggest you study this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic