with this … In this tutorial, you will learn-

What is an array?

Array Variables

First Array Program

Java Array: Pass by reference

Multidimensional arrays

how this helps is that a variable can reference the index (the number in the bracket[]) for easy looping.

Array Variables

Using an array in your program is a 3 step process –

  1. Declaring your Array
  2. Constructing your Array
  3. Initialize your Array

  1. Declaring your Array

Syntax or Example:

  1. Constructing an Array

Example: Declaration and Construction combined

  1. Initialize an Array

Declaring and initialize an Array Example:

First Array Program

Step 1) Copy the following code into an editor. Step 2) Save , Compile & Run the code. Observe the Output Output: Step 3) If x is a reference to an array, x.length will give you the length of the array. Uncomment line #10. Save, Compile & Run the code.Observe the Output Step 4) Unlike C, Java checks the boundary of an array while accessing an element in it. Java will not allow the programmer to exceed its boundary. Uncomment line #11. Save, Compile & Run the code.Observe the Output Step 5) ArrayIndexOutOfBoundsException is thrown. In case of C, the same code would have shown some garbage value.

Java Array: Pass by reference

Arrays are passed to functions by reference, or as a pointer to the original. This means anything you do to the Array inside the function affects the original. Example: To understand Array are passed by reference Step 1) Copy the following code into an editor Step 2) Save, Compile & Run the code. Observe the Output Output:

Multidimensional arrays

Multidimensional arrays are actually arrays of arrays. To declare a multidimensional array variable, specify each additional index using another set of square brackets. When you allocate memory for a multidimensional array, you need only specify the memory for the first (leftmost) dimension. You can allocate the remaining dimensions separately. In Java, array length of each array in a multidimensional array is under your control. Example Output: