Arrays

Arrays are always a problem. There are no cross-language standards.

Visual Basic | Delphi | C++ Builder | Java

Visual Basic 6.0

Search help for arrays, creating
Dim a()                  ' variable length array of variants
Dim b(5) as String       ' An array of 5 strings
Dim c(3 to 8) as Integer ' 6 elements
Dim d(4, 7)              ' Multiple dimensions, up to 60 allowed

for i from 1 to 5
  a(i) = i
next i

Use Option Base to set the default starting index value. If Option Base is not set, the default is zero.

Key Words

Dynamic Arrays

Dim a()                          ' variable length array of variants

ReDim a(10, 3 to 8)              ' Produces 2 dimensions and clears all data
ReDim Preserve b(200)            ' Keeps the old values
ReDim Preserve c(UBound(c) + 50) ' Add 50 more slots
You can not re-size an array passed as an argument. (Funny, I read this in the help but could not find it again! Even funnier, re-size seems to work fine.)

There is no command to determine if a dynamic array has been allocated space. This is important because UBound(c) fails with a "Subscript out of Range" error if no memory is allocated. I've tested

  • c = nothing
  • c = nil
  • c = null
  • UBound(c)
  • IsNull(c)
  • IsEmpty(c)
  • IsArray(c)
  • IsObject(c)
Type TUser  ' T means Type
  i1 As Integer
  i2 As Integer
End Type

Dim C() As TUser

Jostein Trondal has suggested the following work around which works in VB 6.0.

Duncan Drysdale has suggested the following to test for a blank or null VB dynamic array


Delphi


array [index-type] of element-type
type
  IntList  = array[1..100]     of Integer;
  CharData = array['A'..'Z']   of Byte;
  TMatrix  = array[0..9, 0..9] of real;

var
 MyArray: array[1..100] of Char;
 Camera : array[1..4]   of TEERCamera;
 M1     : TMatrix ;             // The variable M1 is an array

In code
 M1[2,4] is equivalent to M1[2][4]

Arrays of records
  Results[1].Count

If an array [1..30] is passed to a subroutine, inside the subroutine, the first element is zero (0).
However, if the array is defined using a defined type, then the first element will be one (1).


High() & Low()

I like to write routines that accept arrays of different sizes. This is done by using the High and Low functions. What isn't in the Delphi 5 help is how to use these functions with multideminsional arrays.


Variable Length Arrays

variant array (can be resized) I can't figure out how to create a variable length array of records in Delphi 2. Delphi 5 supports this capability.

Variable length array type data can be stored in

Must use create and free for these types of arrays.
  Collection     List     String     Outline  
Count
Items
Count
Items
List
Capacity
Count
Objects
Strings
Values
Text
Data
Index
FullPath
Expanded
...
Add
Clear
BeginUpdate
EndUpdate
Expand
Pack
Add
Delete
Insert
Remove
Move
Exchange
Add
Append
Delete
Insert
Move
Exchange
Clear
BeginUpdate
EndUpdate
SaveToFile
LoadFromFile
FullExpand
Collapse
Expand
See the TList.Items for some help.

Dynamic Arrays with TList


Dynamic (Variable Size) Arrays

type
  var MyFlexibleArray: array of Real;

  SetLength(MyFlexibleArray, 20);
  A := Copy(A, 0, 20) truncates all but the first 20 elements of A.


length(a)  number of elements
high(a)    largest index
low(a)     lowest index


Array of Named Strings

Use a TStringList to store the INI parameters. Assuming string data in the format
keyName=value
tempStr := a.name[i];                  // Returns the key at i
j       := a.IndexOfName('keyName');   // Returns the location of keyName
tempStr := Values['keyname'];          // Returns the associated value

function IndexOfName(const Name: string): Integer
property Values[const Name: string]: string;
property Names[Index: Integer]: string;           // Returns the Name at that index


C++ Builder

  long varName[8];           // Contains 8 values - 0 thru 7
  short b[] = {2, 5, 1, 7};  // Create and initialize array with 4 elements
  short* bb = b;             // b without a subscript is the address 
                             //   of the 1st element of the array
  long  c[4][3];
  short* xx = new short[x];  // Allocate an array at run time
  char str[] = "Any String"; // Stores a null terminated string

  for ( int i = 0; i < 10; i++ )
  {
     a[i] = i;
  }
C++ does not check array bounds. If you try to write past the end of an array, memory will be modified! But since the memory is not allocated to the array, the results are un-predicable.


Java

Arrays always start at zero.
Java does check array bounds.

2 different syntaxes can be used to declare arrays

  int[] array1;
  int   array1[];
However, this does not allocate any memory.
  int[] array1 = new int[200];
  int[] array2 = {30,31,29,30,30};
  int[][]   a1 = new int[6][3];
  int[][]   a1 = new int[6][];      // Each element can have a different
         a1[1] = new int[5];        // number of elements
         a1[2] = new int[8];
      a1[2][4] = 20;

  array1.length returns the number of elements

Author: Robert Clemenzi - clemenzi@cpcug.org
URL: http:// cpcug.org / user / clemenzi / technical / Languages / Arrays.htm