Array Variables

Contact Us or call 1-877-932-8228
Array Variables

Array Variables

The array as a whole can be referenced by the array name without the brackets, for example, as a parameter to or return value from a function

Code Sample:

Java-Arrays/Demos/Arrays2.java
public class Arrays2 {
  public static void main(String[] args) {
    String[] names = new String[3];
    names[0] = "Joe";
    names[1] = "Jane";
    names[2] = "Herkimer";
    printArray(names);
  }
  public static void printArray(String[] data) {
    for (int i = 0; i < data.length; i++) {
      System.out.println(data[i].toUpperCase());
    }
  }
}

The array names is passed to printArray, where it is received as data.

Note also the syntax to access a method directly for an array element: data[i].toUpperCase()

Since an array reference is a variable, it can be made to refer to a different array at some point in time

String[] names = new String[3];
names[0] = "Joe";
names[1] = "Jane";
names[2] = "Herkimer";
printArray(names);
names = new String[2];
names[0] = "Rudolf";
names[1] = "Ingrid";
printArray(names);
Next