By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,097 Members | 1,216 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,097 IT Pros & Developers. It's quick & easy.

Parallel array

P: 8
Hello!

Im learning arrays and am having trouble with parallel arrays. I dont understand exactly what they are. Can anyone give me an explanation of what they are or maybe how they can be declared or a small example...anything to help me understand parallel arrays?

Thank you.
Mar 25 '07 #1
Share this Question
Share on Google+
2 Replies


Expert 5K+
P: 8,434
Im learning arrays and am having trouble with parallel arrays. I dont understand exactly what they are. Can anyone give me an explanation of what they are or maybe how they can be declared or a small example...anything to help me understand parallel arrays?
I think I've heard the expression "parallel arrays" once or twice. My guess would be that this refers to the common practice of defining multiple arrays with the same number of occurrences, to hold related information. For instance, you might have one array to hold names, another to hold ages, and so on.

I could be wrong, of course. Try some searches on TheScripts and on the web in general (you can do both from the Search box at the top-right of this page).
Mar 25 '07 #2

SammyB
Expert 100+
P: 807
Hello!

Im learning arrays and am having trouble with parallel arrays. I dont understand exactly what they are. Can anyone give me an explanation of what they are or maybe how they can be declared or a small example...anything to help me understand parallel arrays?

Thank you.
Killer's exactly right. For example, you might have an array of people's names and a parallel array of their ages. So, if you search thru the name array and find Sam at index 2, then you you can use the same index into the age array to find Sam's age:
Expand|Select|Wrap|Line Numbers
  1.         Dim Names() As String = {"Killer", "Sam", "Deja", "Sue"}
  2.         Dim Ages() As Integer = {23, 61, 17, 12}
  3.         Dim i As Integer = 0
  4.         Do While Names(i) <> "Sam"
  5.             i = i + 1
  6.         Loop
  7.         MsgBox("Sam is " & Ages(i))
However, since parallel arrays indicate that something belongs together, now we usually create an object to hold all of the parallel data. Doing the example object-oriented, we would not use parallel arrays but instead create a Person object with two properties, name and age, and create List of Persons. Our Person class would look like:
Expand|Select|Wrap|Line Numbers
  1. Public Class Person
  2.     Public Name As String
  3.     Public Age As Integer
  4.     Public Sub New(ByVal sName As String, ByVal iAge As Integer)
  5.         Name = sName
  6.         Age = iAge
  7.     End Sub
  8. End Class
And our object-oriented code would look like
Expand|Select|Wrap|Line Numbers
  1.         Dim Persons As List(Of Person)
  2.         Persons = New List(Of Person)
  3.         Persons.Add(New Person("Killer", 23))
  4.         Persons.Add(New Person("Sam", 61))
  5.         Persons.Add(New Person("Deja", 17))
  6.         Persons.Add(New Person("Sue", 12))
  7.         Dim p As Person
  8.         For Each p In Persons
  9.             If p.Name = "Sam" Then _
  10.                 MsgBox("Sam is " & p.Age)
  11.         Next
The object-oriented way is probably beyond you now, but by the end of the class you'll be there. Keep smiling, it could be worse: trying to learn all this at 61. :D Sam
Mar 26 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.