Parallel array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dejavu33
    New Member
    • Mar 2007
    • 8

    #1

    Parallel array

    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...anyth ing to help me understand parallel arrays?

    Thank you.
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    Originally posted by dejavu33
    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...anyth ing 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).

    Comment

    • SammyB
      Recognized Expert Contributor
      • Mar 2007
      • 807

      #3
      Originally posted by dejavu33
      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...anyth ing 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:
      Code:
      		Dim Names() As String = {"Killer", "Sam", "Deja", "Sue"}
      		Dim Ages() As Integer = {23, 61, 17, 12}
      		Dim i As Integer = 0
      		Do While Names(i) <> "Sam"
      			i = i + 1
      		Loop
      		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:
      Code:
      Public Class Person
      	Public Name As String
      	Public Age As Integer
      	Public Sub New(ByVal sName As String, ByVal iAge As Integer)
      		Name = sName
      		Age = iAge
      	End Sub
      End Class
      And our object-oriented code would look like
      Code:
      		Dim Persons As List(Of Person)
      		Persons = New List(Of Person)
      		Persons.Add(New Person("Killer", 23))
      		Persons.Add(New Person("Sam", 61))
      		Persons.Add(New Person("Deja", 17))
      		Persons.Add(New Person("Sue", 12))
      		Dim p As Person
      		For Each p In Persons
      			If p.Name = "Sam" Then _
      				MsgBox("Sam is " & p.Age)
      		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

      Comment

      Working...