Arrays in Fortress

Arrays in Java are a numbered sequence of elements of a specific length. In typical Java code, arrays are quite common and useful in many scenarios.

public class Arrays {
    public static void main(String[] args) {
        // Here we create an array 'a' that will hold exactly
        // 5 integers. The type of elements and length are both
        // part of the array's type. By default an array is
        // zero-valued, which for integers means 0s.
        int[] a = new int[5];
        System.out.println("emp: " + java.util.Arrays.toString(a));

        // We can set a value at an index using the
        // array[index] = value syntax, and get a value with
        // array[index].
        a[4] = 100;
        System.out.println("set: " + java.util.Arrays.toString(a));
        System.out.println("get: " + a[4]);

        // The length property returns the length of an array.
        System.out.println("len: " + a.length);

        // Use this syntax to declare and initialize an array
        // in one line.
        int[] b = {1, 2, 3, 4, 5};
        System.out.println("dcl: " + java.util.Arrays.toString(b));

        // In Java, you can't use '...' to have the compiler count
        // the number of elements for you. The size must be explicitly
        // specified or inferred from the initializer list.

        // Java doesn't have a direct equivalent to Go's index-based
        // initialization. You would need to initialize the array
        // and then set specific elements.
        int[] c = new int[5];
        c[0] = 100;
        c[3] = 400;
        c[4] = 500;
        System.out.println("idx: " + java.util.Arrays.toString(c));

        // Array types are one-dimensional, but you can
        // compose types to build multi-dimensional data
        // structures.
        int[][] twoD = new int[2][3];
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 3; j++) {
                twoD[i][j] = i + j;
            }
        }
        System.out.println("2d: " + java.util.Arrays.deepToString(twoD));

        // You can create and initialize multi-dimensional
        // arrays at once too.
        twoD = new int[][] {
            {1, 2, 3},
            {1, 2, 3}
        };
        System.out.println("2d: " + java.util.Arrays.deepToString(twoD));
    }
}

Note that arrays in Java are printed using java.util.Arrays.toString() for one-dimensional arrays and java.util.Arrays.deepToString() for multi-dimensional arrays.

When you run this program, you’ll see output similar to this:

emp: [0, 0, 0, 0, 0]
set: [0, 0, 0, 0, 100]
get: 100
len: 5
dcl: [1, 2, 3, 4, 5]
idx: [100, 0, 0, 400, 500]
2d: [[0, 1, 2], [1, 2, 3]]
2d: [[1, 2, 3], [1, 2, 3]]

Java arrays are similar to those in many other languages, but there are some key differences from Go:

  1. Java uses square brackets [] after the type to declare arrays, not before the type as in Go.
  2. Java doesn’t have a built-in way to let the compiler count the elements (Go’s ...).
  3. Java doesn’t have Go’s index-based initialization syntax.
  4. Printing arrays in Java requires using utility methods from java.util.Arrays, as Java doesn’t have built-in pretty-printing for arrays.

Despite these differences, the core concept of arrays as fixed-size, indexed collections of elements remains the same.