[Gambas-user] Multidimensional dynamic array basics

nando nando_f at ...951...
Wed Aug 19 11:39:12 CEST 2015


Personally, I like to think of a 2D array of integers as a
1D array of objects, with each element an array of integers

I've done something like this with complete success:

'------------------------------------


Public rec As New Object[100]    'declare rec as an array of objects and create it.      

...then...  in Sub _new()

Dim i As Integer
Dim ia As Integer[]      'This declares what ia is, but doesn't create it yet

For i = 0 To 99
  ia = New Integer[10]   'This creates a new array of integers[0..9] each loop
  rec[i] = ia            'Each element of rec[] now contains a created Integer[10] array
Next



'------------------------------------
...something else...  

rec[15][3] = 86400 'just like a 2D integer array

rec.delete(7)      'will remove rec[7] (the whole integer array at rec[7])
                    and bumps down whole arrays rec[8]..rec[99] to be rec[7]..rec[98]

rec[20].delete(3)  'will remove the rec[20][3] element. (a single integer)
                   'so, rec[20] is an Integer[] with 9 (not 10) elements:  [0]...[8]

rec[46] = rec[45]  'The Integer array at rec[46] pointed to is gone. Poof! Vaporized.
                   'rec[46] doesn't point to it anymore.
                   'rec[45] and rec[46] point to the same identical integer[] array
                   'which originally was created as rec[45]
                   'Then, if you wrote rec.delete(45), the array would not be gone because
                   'rec[46] still points to it....however, the .delete moves everything
                   'down one element:  rec[46]..[99] is now rec[45]..[98]

Swap rec[40], rec[41]   'the whole integer[] arrays 40 and 41 are swapped.
                        'data is not copied, just the pointer to the arrays are.


'------------------------------------

..something else...

Dim s as String[]
s = New String[]

rec.add(s,92)     'rec[0..91] each are an Integer[10] array
                  'rec[92] is a String[] array with 0 elements.
                  'rec[93..100] each are an Integer[10] array

rec[92].add("HELLO)   'rec[92] is now a String[] array with 1 element...
                      'which is: rec[92][0] = "HELLO"


'------------------------------------


..some where else...     ' I want an array of 500 Labels in rec[50]
Dim L as Label
Dim i as Integer
Dim o as New Object[500] 'declare AND create a new Object[] array with 500 elements

rec[50].add(o)           'rec[50] Integer[] is now rec[51].  Everything bumped up one []
For i = 0 To 499         '...and rec[50] is an Object[500]
  L = New Label          'create a New Label
  rec[50][i] = L         'now rec[50][i] points to the new label
Next                     'Every one of the 500 Labels in rec[50] are created and unique
                         '..and you could display each of them on a Form.

'-------------------------------------


It can get quite interesting because this works with any objects
including GUI Labels, Timers, Strings, Structs, Panels, and so on.
(when doing GUI things, you also have to connect them to event handlers too
 ...I didn't write that here)

The most important part is to understand the difference about
DECLARING variable names (for the program to compile) (using DIM or Private or Public)
and
CREATING new variables at run-time. (like ...As NEW Object[500] ...As NEW Label)

I'm pretty sure I got the syntax right.
-Nando




---------- Original Message -----------
From: Kevin Fishburne <kevinfishburne at ...1887...>
To: mailing list for gambas users <gambas-user at lists.sourceforge.net>
Sent: Wed, 19 Aug 2015 02:22:54 -0400
Subject: [Gambas-user] Multidimensional dynamic array basics

> I need to know how to declare, initialize and add elements to a 
> two-dimensional dynamic array. Using trial-and-error for every possible 
> syntax hasn't been too helpful so far. I'd post my code but it's pretty 
> useless and embarrassing and I think the basic question sums it up.
> 
> -- 
> Kevin Fishburne
> Eight Virtues
> www: http://sales.eightvirtues.com
> e-mail: sales at ...1887...
> phone: (770) 853-6271
> 
> ------------------------------------------------------------------------------
> _______________________________________________
> Gambas-user mailing list
> Gambas-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user
------- End of Original Message -------





More information about the User mailing list