Single Dimensional Array Tutorial

A single-dimensional (one-dimensional) array is basically a list of items of the same type. In Java, an array is an object. The length of the array is set when the array is created and that length is fixed cannot be changed later. An array consists of elements, each element can be modified by using its numerical index. Numeric indexes begin at the number 0, so the first element can be accessed at index 0, the second element is at index 1, and so on and so forth. An array with 5 elements looks like this:
Index            0 | 1 | 2 | 3 | 4 |
Element       1 | 2 | 3 | 4 | 5 |


There are a couple of different ways to declare an array reference variable:
(type) (reference variable) (pair of brackets []) ;
(type) (pair of brackets []) (reference variable) ;
int numberArray[];
int[] numberArray;
After we declare the reference variable, we then assign it to an array object of specified length. This is how it is done:
(reference variable) (assignment operator =) (keyword new) (type) (pair of brackets [array length]) ;
numberArray = new int[5];

We can do it all in one statement like this:
int numberArray[] = new int[5];

Once we have created our array object, we can then initialize its elements. We can directly initialize each element like this:
numberArray[0] = 67;
numberArray[1] = 1398;
numberArray[2] = 5;
numberArray[3] = 10_300;
numberArray[4] = 731;

There is also shorthand syntax that allows us to declare and initialize an array object without using the new keyword. The values for the elements are enclosed in curly braces and separated with commas; the length of the array is determined by the number of values inside the braces.
int numberArray[] = { 67, 1398, 5, 10_300, 731 };

I am going to cram one more concept into this tutorial. Almost every core Java object has useful built-in properties and methods. Because an array is an object, it has some useful built-in properties like length; The length property will return an int data type as the value of the number of elements contained in the array object.
int arrayLength = 0;
arrayLength = numberArray.length;



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md SingleArray
C:\Java>cd SingleArray
C:\Java\SingleArray>Notepad SingleArray.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class SingleArray {
    public static void main(String args[]) {
        int numberArray[]; // declare a reference variable
        numberArray = new int[5]; // allocate an int type array of 5 elements and assign to numberArray
        numberArray[0] = 67;
        numberArray[1] = 1398;
        numberArray[2] = 5;
        numberArray[3] = 10_300;
        numberArray[4] = 731;

        System.out.println("The number of elements in numberArray: "+ numberArray.length);
        
        for (int i = 0; i < 5; i++) {
            System.out.println("The value of element #" + (i+1) + ", index " + i + " is: "+ numberArray[i]);
        }
        System.out.println();
		
        String stringArray[] = { "Arrays ", "are ", "made ", "up ", "of ", "elements." };
        
        for (int x = 0; x < stringArray.length; x++) {
            System.out.print(stringArray[x]);
        }
        System.out.println();		
    }
}

Now switch back to the command prompt (CMD) and type in javac SingleArray.java and press Enter.
Now type in java SingleArray and press Enter.


C:\Java\SingleArray>javac SingleArray.java
C:\Java\SingleArray>java SingleArray
The number of elements in numberArray: 5
The value of element #1, index 0 is: 67
The value of element #2, index 1 is: 1398
The value of element #3, index 2 is: 5
The value of element #4, index 3 is: 10300
The value of element #5, index 4 is: 731

Arrays are made up of elements.


Final thoughts

Arrays are used quite often when writing code for Java. Understanding how to declare and initialize them properly is a really good thing to know.


Tutorials