Our first program will print the classic “hello world” message. Here’s the full source code.
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:
We create an empty ArrayList and check its properties.
We create an ArrayList with an initial capacity.
We add elements to the ArrayList.
We get elements from the ArrayList.
We demonstrate how to get the size of an ArrayList.
We show how to add multiple elements to an ArrayList.
We create a copy of an ArrayList.
We demonstrate how to get sublists from an ArrayList.
We show how to initialize an ArrayList in one line.
We compare two ArrayLists.
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.