Jump to content

Collections


G+_Dean T
 Share

Recommended Posts

Collections... So a little while back I asked if arrays ever really get used as I didnt see a point for them. Well after studying and programming for a bit I actually found that when working with a lot of data (objects for example) they come in handy. Well now I am on to collections and again I dont see the use of them when we have arrays. I know they have a couple benefits but from what I gather most languages dont use collections they use arrays. So if I start using collections more than arrays isnt this going to make it difficult for me moving over to other languages in the future. Plus I really like arrays now.

Link to comment
Share on other sites

Well Im still a beginner but the only real benefit I can see is that you dont need to declare a size for a list while you do an array. But Im still only a few pages in on them and currently using some test code using mp3 objects to see which I prefer. Im assuming until shown otherwise its more a personal preference type of thing. 

Link to comment
Share on other sites

I have also found another use. With an array for example you have 3 objects in an array and you remove the second object then the index would become null. This is beneficial if you need to have info at a certain indexpoint however collections (lists at the moment) if you remove an object it moves all other objects up so that there is no null index which is beneficial if youre making a list.

Link to comment
Share on other sites

Ok so after a good few hours studying and messing around to get the hang of collections I also found out that collections dont need an index amount to be set so we can keep adding objects regardless of knowing how many we need while an array you need to set how the index and cant change it while running. 

 

I created some example code here that shows what I mean. 

 

http://pastebin.com/6TSEGfsb

Link to comment
Share on other sites

You're still thinking too language-specific. The art of computer programming is, in large part, figuring out the data structure that best matches your data and access model. Some languages, like Java, may have a rich set of already-built structures to choose from. Some, like C, have only the basic array and require you to implement everything else yourself. A simple array is a low level thing that closely matches the hardware: memory is a big sequence of linearly addressed cells. Collections like hash tables, vectors, and such are implemented under-the-hood using arrays and extra variables specific to the job at hand. But their high-level abstraction is useful for making a program understandable and maintainable.?

Link to comment
Share on other sites

Thanks Lee Crocker, Im sure your views are something I will eventually start to see as I keep learning and get a wider understanding and grasp on programming. Since I am still on the foundation building blocks its all new and I dont have the experience to compare with other languages or to see the greater picture. One thing I am noticing is that a lot of things when I start to read up on them I seem to question their needs / uses. At the start I thought this was a bad thing but instead because I am questioning it, its making me start up VS and starting testing out theories and usage and making my learning curve a lot faster. All starts with a simple step but can always get their faster if you run. Doing seems to be the best way to learn I guess.

Link to comment
Share on other sites

Collections are more flexible but have more processing overhead than arrays so they are slower.  Older languages don't directly support them but in languages like Pascal and C that have Pointers you can do the same thing with code by creating Linked Lists, Sparse Matrix's and other classic data structures.

Link to comment
Share on other sites

It's not true that arrays are "faster" than collections. You do lots of different things to a collection: access items by index, by key, by sequence; insert items, delete items, search by value, etc. You choose a collection that performs well for the actions you need for your application. Arrays, for example, are very fast at retrieving by index, but slow at searching by value, inserting, and deleting. Hash tables are fast at searching by value and inserting, but often slow at deletion or sequential access. Trees have generally mediocre, but never bad, performance at everything.

Link to comment
Share on other sites

Good point Lee. I was thinking in terms of memory because a collection has to allocate memory when something is added to it and deallocate it when the collection is freed but an array (static ones at least) are allocated by the compiler. Generally I use an array when I can access it with a know index and a collection when I need something sortable, searchable, etc.

Link to comment
Share on other sites

 Share

×
×
  • Create New...