• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is the purpose of the arrays?  RSS feed

 
Daniel Mestre
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can somebody explain me the purpose of the arrays? I don't think they are useful and if they are explain to me please. If it's useful for game development also tell me.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Mestre wrote:Can somebody explain me the purpose of the arrays? I don't think they are useful and if they are explain to me please. If it's useful for game development also tell me.



Let's say that you have a game with a 100 by 100 battlefield, or something simpler like a 8 by 8 chess board. Arrays would be a good option to represent it.

Henry
 
Daniel Mestre
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Daniel Mestre wrote:Can somebody explain me the purpose of the arrays? I don't think they are useful and if they are explain to me please. If it's useful for game development also tell me.



Let's say that you have a game with a 100 by 100 battlefield, or something simpler like a 8 by 8 chess board. Arrays would be a good option to represent it.

Henry

Then if I have a 2D plataformer game and I like to put the blocks in that position I would call upon the arrays right?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My favorite analogy for arrays are egg cartons.

What is the purpose of an egg carton? to hold a bunch of eggs. it would be unwieldy to try and carry 12 (or 24 or 96) eggs in your bare hands. You don't want to have to hire 12 people who each hold an egg. You certainly COULD, and then say "Bob, give me your egg....Peter give me your egg...Mary - do you have an egg?"

An array is like a carton that can hold 12 'things'. You can then just reference the carton, and say "give me the 3rd egg in the carton...give me the 9th egg in the carton...is there something in the 2nd spot?" etc.

Since the positions are numbered, it is easy to use a loop to examine each egg. If you had to explicitly name each spot, your code would be VERY fragile if you needed to add another item/slot.
 
rohit chavan
Ranch Hand
Posts: 133
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
perfect explaination it is given by Fred.

Arrays make it easier to ACCESS the ojects you have KEPT in it.
 
Daniel Mestre
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:My favorite analogy for arrays are egg cartons.

What is the purpose of an egg carton? to hold a bunch of eggs. it would be unwieldy to try and carry 12 (or 24 or 96) eggs in your bare hands. You don't want to have to hire 12 people who each hold an egg. You certainly COULD, and then say "Bob, give me your egg....Peter give me your egg...Mary - do you have an egg?"

An array is like a carton that can hold 12 'things'. You can then just reference the carton, and say "give me the 3rd egg in the carton...give me the 9th egg in the carton...is there something in the 2nd spot?" etc.

Since the positions are numbered, it is easy to use a loop to examine each egg. If you had to explicitly name each spot, your code would be VERY fragile if you needed to add another item/slot.

I thought it was egg cartoons. xD Btw great explanation thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!