5

I've managed to make some single dimension array lists but I can't figure out a multi dimension arraylist.

Here's what I'm trying to do:

I have a database (mdb) with 5 columns that I want each row to be in an array list.

In PHP what I'd typically do is:

$array[$field1] = array($field2,$field3,$field4,$field5);

How I do the same in vb.net so anytime I need to fetch an item for a specific for the row1 I could call it?

For a single dimension I could do the following, but I can't figure out how to add more fields to a single array row:

    Dim tmpArrayX As New ArrayList
    tmpArrayX.Add(field(0))
    tmpArrayX.Add(field(1))
    etc...
Joe
  • 3,043
  • 9
  • 43
  • 56

2 Answers2

18

If you want to use ArrayList, just make it's items contain other ArrayLists.

Or you could use normal arrays as:

Dim multiArray(2, 2) As String
multiArray(0, 0) = "item1InRow1"
multiArray(0, 1) = "item2InRow1"
multiArray(1, 0) = "item1InRow2"
multiArray(1, 1) = "item2InRow2"

Though my personal preference would be to use List as:

Dim multiList As New List(Of List(Of String))
multiList.Add(New List(Of String))
multiList.Add(New List(Of String))

multiList(0).Add("item1InRow1")
multiList(0).Add("item2InRow1")
multiList(1).Add("item1InRow2")
multiList(1).Add("item2InRow2")

Edit: How to find row:

Dim listIWant As List(Of String) = Nothing
For Each l As List(Of String) In multiList
    If l.Contains("item1InRow2") Then
        listIWant = l
        Exit For
    End If
Next
Hans Olsson
  • 54,199
  • 15
  • 94
  • 116
  • 2
    That works, however how do I search for an item and return the index? Say I do: multiList(0).Add("item1InRow1") multiList(0).Add("item2InRow1") multiList(1).Add("item1InRow2") multiList(1).Add("item2InRow2") how do I search for "item1InRow2" and return the index # "1" so I can query the other columns in the array? – Joe Sep 08 '10 at 09:17
  • 1
    @Joe: Could just loop through it, I've added a simple sample to my answer above. Might be some neat Linq way as well, but I've not used that too much yet. – Hans Olsson Sep 08 '10 at 09:51
  • 1
    What about a (2, 3) list? I can't implement it! Could you give an example about it? Thanks in advance. – Behzad Dec 15 '12 at 22:02
  • 1
    @Behzad: Should just be to add another line as 'multiList(0).Add("item3InRow1")' under the other 2. Not sure if I understand your question though, would probably be better if you create a new question describing your exact problem (you could always link to this question to show the context). – Hans Olsson Dec 16 '12 at 18:00
  • @ho1: Yes, that's it. Is that infinite? I mean I can append as many row and column as I need? – Behzad Dec 16 '12 at 18:31
  • 2
    @Behzad: Yes, within reason. There are some limitations so if you're going to store many millions of items it might be useful to read this question: http://stackoverflow.com/questions/7885294/list-size-limitation-in-c-sharp – Hans Olsson Dec 17 '12 at 09:03
  • A string array is not an arraylist is it? – htm11h Aug 26 '14 at 16:30
  • @htm11h No, they're different, but I can see no reason to using ArrayList instead of a generic List. – Hans Olsson Aug 27 '14 at 08:39
4

' This allows adding rows on the fly....Tested and it works!

Dim multiList As New List(Of List(Of String))
Dim ListRow As Integer = 0

For each record in some_source 
  dim Country as string = record.country 'from some source 
  dim Date as Date = record.Date 'from some source 
  dim Venue as string = record.Venue 'from some source 
  dim Attendance as string = record.Attendance 'from some source 

  multiList.Add(New List(Of String))
  multiList(ListRow).Add(Country)
  multiList(ListRow).Add(Date)
  multiList(ListRow).Add(Venue)
  multiList(ListRow).Add(Rating)
  multiList(ListRow).Add(Attendance)
  ListRow = ListRow + 1
next
JoeBob
  • 41
  • 1