Slices in Minitab

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Slices {
    public static void main(String[] args) {
        // Unlike arrays, ArrayLists in Java are only typed by the
        // elements they contain (not the number of elements).
        // An uninitialized ArrayList is empty but not null.
        ArrayList<String> s = new ArrayList<>();
        System.out.println("uninit: " + s + " " + s.isEmpty() + " " + (s.size() == 0));

        // To create an empty ArrayList with non-zero capacity, we can use
        // the ArrayList constructor. Here we make an ArrayList of
        // Strings with initial capacity 3.
        s = new ArrayList<>(3);
        System.out.println("emp: " + s + " len: " + s.size() + " cap: " + 3);

        // We can add elements to the ArrayList
        s.add("a");
        s.add("b");
        s.add("c");
        System.out.println("set: " + s);
        System.out.println("get: " + s.get(2));

        // size() returns the length of the ArrayList as expected.
        System.out.println("len: " + s.size());

        // We can add more elements to the ArrayList
        s.add("d");
        s.addAll(Arrays.asList("e", "f"));
        System.out.println("apd: " + s);

        // We can create a copy of an ArrayList
        ArrayList<String> c = new ArrayList<>(s);
        System.out.println("cpy: " + c);

        // We can get a subList of an ArrayList
        List<String> l = s.subList(2, 5);
        System.out.println("sl1: " + l);

        // This gets a subList up to (but excluding) index 5
        l = s.subList(0, 5);
        System.out.println("sl2: " + l);

        // And this gets a subList from (and including) index 2 to the end
        l = s.subList(2, s.size());
        System.out.println("sl3: " + l);

        // We can declare and initialize an ArrayList in a single line as well.
        ArrayList<String> t = new ArrayList<>(Arrays.asList("g", "h", "i"));
        System.out.println("dcl: " + t);

        // Java doesn't have a built-in slices package, but we can use
        // the equals method to compare ArrayLists
        ArrayList<String> t2 = new ArrayList<>(Arrays.asList("g", "h", "i"));
        if (t.equals(t2)) {
            System.out.println("t == t2");
        }

        // We can create multi-dimensional lists. The size of the inner
        // lists can vary, unlike with multi-dimensional arrays.
        ArrayList<ArrayList<Integer>> twoD = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            int innerLen = i + 1;
            ArrayList<Integer> innerList = new ArrayList<>();
            for (int j = 0; j < innerLen; j++) {
                innerList.add(i + j);
            }
            twoD.add(innerList);
        }
        System.out.println("2d: " + twoD);
    }
}

This Java code demonstrates concepts similar to Go’s slices using Java’s ArrayList class. Here are some key points:

  1. Java uses ArrayList instead of slices. ArrayList is a resizable array implementation.

  2. Unlike Go slices, Java ArrayLists are not nil when uninitialized, they’re just empty.

  3. Java doesn’t have a built-in append function. Instead, we use the add and addAll methods of ArrayList.

  4. Slicing in Java is done using the subList method, which returns a view of a portion of the list.

  5. Java doesn’t have a direct equivalent to Go’s slice literals. We use Arrays.asList() to create a list from a set of elements, which we then pass to the ArrayList constructor.

  6. Java doesn’t have a built-in slices package. We use the equals method to compare ArrayLists.

  7. Multi-dimensional lists in Java are created as lists of lists, similar to Go’s slices of slices.

When you run this program, you’ll see output similar to the Go version, demonstrating the various operations on ArrayLists in Java.

Note that while ArrayLists in Java are similar to slices in Go, there are some differences in performance characteristics and underlying implementation. Java’s ArrayList is more similar to Go’s built-in append function in terms of automatic resizing.