Accessing Array Elements


Array elements are accessed through using the square brackets notations and specifying the element index within them. As said earlier, the first index in an array is 0, so it we were to assign the first element in an array to another variable it could look like this:


        int[] numbers = new int[] {10, 11, 12};
        
        //The variable 'aNumber' is assigned the value 10
        int aNumber = numbers[0];


The variable that is assigned the value must be of the same data type as the elements of the array. The exception is a super class type to a reference type.
As a matter of fact, the first element in the array as well as all other elements is variables too. The difference between them and other variables is that a normal variable is accessed through a name but an array variable has no name and is accessed through an index value.

Previous     Next

Tutorial Home


Do you know your Java?
Take a Ten-Question-Java-Quiz!

Bookmark and Share




Need help with your Java code? It's secure and confidential.
This is how it works:
Send a detailed description of what you need help with, the more details the better. Also provide a deadline for when it has to be finished. More time means better chance of putting your request into the schedule.

If the request is serious you will shortly receive an email with the price, to which you have to respond if you accept.

Once you have accepted, the work will begin on developing your code by an experienced Java developer. When the code is finished a link to a secure payment will be sent to you.

The source code is then sent to you once the payment is completed.

IMPORTANT! The request needs to be very detailed, else it may be ignored.


Write your detailed request here:

E-mail address: