Slices in Logo

Our first program will print the classic “hello world” message. Here’s the full source code.

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

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

        // To create an empty ArrayList with non-zero capacity, use
        // the ArrayList constructor. Here we create 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 size 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);

        l = s.subList(0, 5);
        System.out.println("sl2: " + l);

        l = s.subList(2, s.size());
        System.out.println("sl3: " + l);

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

        // We can 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 ArrayLists
        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 program demonstrates the usage of ArrayLists in Java, which are similar to slices in other languages. ArrayLists are dynamic arrays that can grow or shrink in size.

Here’s a breakdown of the operations:

  1. We create an empty ArrayList and check its properties.
  2. We create an ArrayList with an initial capacity.
  3. We add elements to the ArrayList.
  4. We get elements from the ArrayList.
  5. We demonstrate how to get the size of an ArrayList.
  6. We show how to add multiple elements to an ArrayList.
  7. We create a copy of an ArrayList.
  8. We demonstrate how to get sublists from an ArrayList.
  9. We show how to initialize an ArrayList in one line.
  10. We compare two ArrayLists.
  11. Finally, we create a 2D ArrayList.

Note that while ArrayLists are different from arrays, they are rendered similarly when printed.

To run this program, save it as Slices.java, compile it with javac Slices.java, and then run it with java Slices.

The output will show the results of various ArrayList operations, demonstrating their flexibility and utility in Java programming.

Now that we’ve seen ArrayLists, we’ll look at Java’s other key built-in data structure: Maps.