Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

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 ]
 
He does not suffer fools gladly. But this tiny ad does:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic