The array data structure
 

How are arrays used in a high level programming language:

  • Use of arrays:

      • Arrays are used to store similar information of many items

    Example: grades of (many) students

  • An array consists a series of variables

  • Each variable in an array (called an array element) has the same data type

How is an array stored in memory
 

The array elements of an array are stored contiguously in memory:

 

Notice: there are no gaps between consecutive array elements

Notice that each array element occupies the same number of bytes of memory !!!

How to store a byte typed array
 

A byte (typed) array uses 1 byte to store each of its array element

How a byte typed array is stored in memory:

 

Remember: There are no gaps between array elements !

How to store a short typed array
 

A short (typed) array uses 2 bytes to store each of its array element

How a short typed array is stored in memory:

 

Remember: There are no gaps between array elements !

How to store a int typed array
 

A int (typed) array uses 4 bytes to store each of its array element

How an int typed array is stored in memory:

 

Remember: There are no gaps between array elements !

Review: Java's syntax to define an array

  • Java defines an int array A of 10 elements as follows:

         int[] A;
         A = new int[10];                 

     

     

     

     

     

     

     

Review: Java's syntax to define an array

  • Java defines an int array A of 10 elements as follows:

         int[] A;
         A = new int[10];                 

    int[] A will reserve memory for the reference variable A:

               

Review: Java's syntax to define an array

  • Java defines an int array A of 10 elements as follows:

         int[] A;
         A = new int[10];                 

    new int[10] reserves memory for 10 int and returns the base address:

               

Review: Java's syntax to define an array

  • Java defines an int array A of 10 elements as follows:

         int[] A;
         A = new int[10];                 

    "A = " assigns the return value (= addr of array) of new to the ref var A:

               

Review: Java's syntax to define an array

  • Java defines an int array A of 10 elements as follows:

         int[] A;
         A = new int[10];                 

    Result:

               

Java's array vs. C's array
 

  • Java has dynamic arrays

      • Java uses a reference variable to point to the actual array:

            

Dynamic arrays are not the norm in high level programming languages

Java's array vs. C's array

  • Why is Java's array called dynamic array

      • Because the location (and size) of the array can change while the program is running:

            

Java's array vs. C's array

  • C (and most compiled languages) has static arrays

      • A static array uses a label to mark an array:

            

  • A label (= symbolic constant) cannot change (= "static") while the program is running

We will use static arrays in CS255 !