 | Distinguished Member with 3,877 posts. | | Join Date: Sep 2005 Location: NC, USA Experience: Learning everyday :) | | Solved: Arrays help Hello again,
I've just recently been reading some tutorials on W3C, and wanting to get back into some programming. I took some classes in college on programming, and understand the basics, for the most part.
I've never really been able to understand arrays, however. I guess, I just don't know how to implement them, what they are for, etc.
Everytime I see one, it just throws me off how they are used.
Anyone out there know of a good way to explain them, or a good tutorial? | | Distinguished Member with 2,994 posts. | | Join Date: Aug 2005 Experience: Advanced | | I like to think of arrays as collections of similar--not identical, mind--things. You can use dynamic arrays to manipulate objects on the fly, like sorting a set of jumbled numbers; or, you can use static arrays as handy tools to associate an object with an index, like creating an array of month names so that when you needed "February" you'd look for array(1) (since array indices are zero-based).
Here are a couple of simple analogies to help you understand the idea of arrays.
Most people own a set of keys--a house key, a car key, maybe an office key, a P.O. Box key, etc. All these keys are kept on a keychain. You can think of the keychain as an array containing several objects of class key. When you want to use a specific key, you single it out of the array of keys. When you're done with it, you can put it back into the array of keys, or if you'll never use it again, you can remove it from the array (take it off the keychain). If you have a new key, you can add it to the array (put it on the keychain). This is a lot like a dynamic array.
Here's another example. Most people live on a street with multiple residences. The street probably has a fixed number of houses on it. You can think of the street as the array, and each house as an array object of class house. Because the houses don't move, you always know to go to the same index (house number) when you want a particular house. This is a lot like a static array.
Using that same example, each house has a tenant. Unlike the houses, the tenants can change--someone might move out and another tenant moves in. You know the tenants by what house they live in, and can find a tenant by going to the right house number. This is a lot like an associative array, or multi-dimension array; an array in which a thing or set of things is associated with a particular index.
Their versatility in programming is almost boundless.
I hope this at least gets you started thinking.
chris. | | Distinguished Member with 3,877 posts. | | Join Date: Sep 2005 Location: NC, USA Experience: Learning everyday :) | | Thanks Chris, that's helps out a lot with the understanding what they are doing.
So, using the month example:
Say I had the array set up like this:
month(0)=january;
month(1)=february;
month(2)=march;
month(3)=april;
month(4)=may;
month(5)=june;
month(6)=july;
month(7)=august;
month(8)=september;
month(9)=october;
month(10)=november;
month(11)=december;
Now, to call that one, I would just call month(11) in the code for december, correct?
Now, my question would be, I've heard that they save coding, but I just don't seem to grasp how. I still have to define 12 elements for the months, seems like it would be just as good to just have a seperate variable for each month, named according to month, as in, naming the variable january, instead of month(0).
Is the reason so that you can group them together, as you said with the keys? I'm just wondering if it's mainly for grouping objects together, making it easier to reference later, or does it save having to code more? | | Distinguished Member with 2,994 posts. | | Join Date: Aug 2005 Experience: Advanced | | Well, careful there. Remember that writing good code doesn't necessarily mean writing short code. Arrays are much easier to maintain, for one, and because they are collections, every object in the array can be referenced through the common array name.
A few notes about your sample code. First of all, never in any language name an object month. Now, depending on the language you're using, you can declare and dimension arrays in several ways. Of course, you can always dimension arrays as you did--explicitly assigning each index a value. However, every language with which I'm familiar has a "short-hand" method. In VBA, for example (a language with which I think you're familiar), that array could be dimensioned like so: Code: Dim saMonth() As String
saMonth = Split("January,February,March,April," & _
"May,June,July,August," & _
"September,October,November,December", ",")
And that's just one way to do it.
One neat thing about arrays is that, because the indices are integer numbers, you can use mathematical calculations to reference an index. So, for example, if you wanted every fourth month, you could do something like this: Code: Dim i As Integer
For i = 0 To UBound(saMonth)
If i Mod 4 = 0 Then Debug.Print saMonth(i)
Next i
This is just a really limited example. Most of the time, once you really start using arrays correctly, you won't necessarily know what object is at a specific index, nor will you have to. That's the beauty of collections like arrays: because they're all hooked to one reference (the array name in this case), you can perform an action on the reference and loop through its objects instead of explicitly performing the action on each object.
In your example of having a separate variable for each month, if you wanted to perform an action on each month--say, adding -MM to the end of each name (where MM represents the month's count, 01-12)--you'd have to do it literally twelve times. With an array, you could just loop through the indices and add the iterated value to the end of each object.
Arrays are almost intrinsically bound to control blocks (think: loops). If you don't need a control block, you probably don't need an array, either.
Does this make more sense?
chris. | | Distinguished Member with 3,877 posts. | | Join Date: Sep 2005 Location: NC, USA Experience: Learning everyday :) | | Ahh, I see now.
So when you had this code: Code: Dim saMonth() As String
saMonth = Split("January,February,March,April," & _
"May,June,July,August," & _
"September,October,November,December", ",")
It adds the array index, based on the order they are in the Split statement? As in, the first one is 0, the last being 11?
I definitely understand more how they would be useful now. I never thought of it as making it easier to customize the variables of a group, thus shortening the code in the long run, since you could have a set of code for the array, instead of having to write that code for each individual variable.
Now, it's a matter of learning more about the languages themselves. I took come C classes, and took some VB(although I don't remember much about it), and been working with VBA(thanks for you help with it  ), and am thinking about getting more into PHP. | | Distinguished Member with 2,994 posts. | | Join Date: Aug 2005 Experience: Advanced | | That's correct, in a dynamic array or an ad hoc array, the indices are assigned automatically (depending on the language, I suppose). That's why I said when you really get into appropriate array usage, you won't know which object resides at a particular index.
Just so you know, arrays are extremely more complex and versatile than I've made them out to be in this thread. Different languages follow different algorithms for array implementation--for example, first-in, first-out versus a stack or last-in, first-out. Some languages limit an array's dimensions whereas--at least theoretically--other languages an infinite amount of nested arrays (I think...).
I learned a lot about arrays in my Computer Programming Fundamentals class in college that I never figured out programming with perl, Javascript, VB, and the like. You could teach an entire semester's worth of arrays. I bet some Comp Sci tracks do.
Anyway, good luck and happy hunting
chris. | | Distinguished Member with 3,877 posts. | | Join Date: Sep 2005 Location: NC, USA Experience: Learning everyday :) | | Thanks. I appreciated it. |  THIS THREAD HAS EXPIRED.
Are you having the same problem?
We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.
| | |
Smart Search
| Find your solution! | | | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | |  WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
| You Are Using: |
Advertisements do not imply our endorsement of that product or service.
All times are GMT -5. The time now is 03:43 PM.
Copyright © 1996 - 2009 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd. | |
|