Intro to Arrays

  1. Although you have used arrays in Java in your porgrams before, you probably did NOT realised that they were different from those in C, C++, Pascal (except maybe that the "notation was a bit strange"...)

    How to use arrays in Java is the same in C, C++, Pascal, BUT....

    Arrays in Java are fundamentally different from those in C, C++, Pascal

  2. An array (of any type) in Java is an object.
    (You have seen how to define objects.... defining array is similar)

  3. Defining "array object" reference variables:

    Example: defining array object reference variables
       int[]         myArray1;
       double[]      myArray2;
       BankAccount[] myArray3;    
    
    Alternate form:
    
       int         myArray1[];
       double      myArray2[];
       BankAccount myArray3[];
    
    

    Result of these definitions:

  4. Creating array objects:

    
         new TYPE [ SIZE ]    
    
    Examples:
    
         new int[10]
         new double[N]
         new BankAccount[20*K]   
    
    

    Examples:

    Array of primitive type:

    Array of primitive type:

    Array of user defined type:

  5. Example Array Definitions & Their results:

    Array of primitive type:

    Array of user defined type: