Once an array has been defined you can access it through it's reference.

The above code will print out the three names. Access to an array starts at 0. So the first value is accessed by the reference followed by 0 in square brackets. You can assign values using this method.

names[1] = "Zubair";

It obeys the normal rules where the value on the right is copied over to the left.

Arrays can also use variables to represent the index. For example

The above would print out "Zubair".

You can also access the length of the array by using the code "names.length".

System.out.println(names.length);

The above line of code would print out 3.

next multi-dimensional arrays >>

 
Basics
Menu
Search