IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
To/From Code
-Collapse +Expand Cross Ref Guide
-Collapse +Expand Members-Only
Sign in to see member-only pages.
   ► KBTo/From GuidesJavaData Structures  Print This     

Cross Ref > Data Structures

By Mike Prestwood

Java versus Delphi: A side by side comparison between Java and Delphi.

 
Data Structures
 

Data structures allow you to store and work with data. Common data structures include arrays, associative arrays, etc.

Array

[Other Languages] 

Languages Focus

A data structure in which individual values (called elements or items) may be located by reference to one or more integer index variables, the number of such indices being the number of dimensions in the array.

Arrays can start with an index value of 0 or 1, sometimes referred to as 0 based or 1 based.

[Not specified yet. Coming...]
Delphi:   x=Array[0..3] of string;

Delphi supports both static and dynamic arrays as well as single and multi dimensional arrays.

Syntax Example:
var
  MyArray: array[0..3] of string;
  i: Integer;
begin
  MyArray[0] := 'Mike';
  MyArray[1] := 'Lisa';
  MyArray[2] := 'Felicia';
  MyArray[3] := 'Nathan';
  
  for i := 0 to High(MyArray) do
    ShowMessage(MyArray[i]);
end;




Associative Array

[Other Languages] 
A set of unique keys linked to a set of values. Each unique key is associated with a value. Think of it as a two column table. MyArray['CA'] = 'California' MyArray['AR'] = 'Arizona'

Languages Focus

Associative arrays are also known as a dictionary or a hash table in other languages.

Java:   HashMap()

An associative array links a set of keys to a set of values. In Java, associative arrays are implemented as Maps.

This will print "Arizona."

Syntax Example:
import java.util.*;

public class Maps
{
    public static void main(String[] args)
    {
        Map states = new HashMap();
       
        states.put("CA", "California");
        states.put("FL", "Florida");
        states.put("AZ", "Arizona");

        System.out.println(states.get("AZ"));
    }
}
Delphi:   TStringList Assoc Array

Object Pascal doesn't have a native associative array, but you can use a TStringList the same way. (Alternatively, search the Internet for TStringHash and THashedStringList classes for implementations of a true associative array).

Syntax Example:
var
  StateList : TStringList;
begin
StateList := TStringList.Create; 
  StateList.CommaText := 'CA=California, FL=Florida';
  ShowMessage('FL is ' + StateList.Values['FL']);
end;




Pointers

[Other Languages] 

General Info: Pointers / References

A pointer is a variable type that allows you to refer indirectly to another object. Instead of holding data, a pointer holds the address to data -- the address of another variable or object. You can change the address value a pointer points to thus changing the variable or object the pointer is pointing to.

A reference is a type of pointer that cannot change and it must always point to a valid storage (no nulls).

Java:   Not Supported

Java does not offer developer defined pointers.

Delphi: 

Although pointer data types in Delphi coding are less important and not required for most general coding, Delphi fully supports developer defined pointers. Use a carrot (^) to declare a pointer data type. Use the @ operator or Addr function to return the current address of a variable.

Delphi provides typed pointer types such as PChar and PExtended as well as a generic point to anything Pointer type.

Nil is a special pointer value that you can assign to any type of pointer. Nil never points to any valid memory and indicates an unassigned or empty pointer.

Syntax Example:
//Declare a pointer of type integer.
PCounter : ^Integer;
  
//Assign a value to the location of a pointer.
//Also known as dereferencing.
PCounter^ := 8;
  
//Assign address of A to B.
PointerB := @PointerA;  //or...PointerB := Addr(PointerA);




Sales Website: www.prestwood.com Or visit our legacy sales site: 
legacy.prestwood.com


©1995-2024 Prestwood IT Solutions.   [Security & Privacy]