Concept

How do Java lists provide dynamic data structures?

ComputerScienceOne / Dynamic Data Structures

"The Java SDK provides a rich assortment of dynamic data structures as alternatives to arrays including lists, sets and maps. The classes that support and implement these data structures are defined in the Collections library under the java.util package. We will cover how to use some of these data structures, but we will not go into the details of how they are implemented nor the OOP concepts that underly them.\n\nThe Java List is an interface that defines a dynamic list data structure. This data structure provides a dynamic collection that can grow and shrink automatically as you add and remove elements from it. It is an interface, so it doesn’t actually provide an implementation, just a specification for the publicly available methods that you can use. Two common implementations are ArrayList, which uses an array to hold elements, and LinkedList which stores elements in linked nodes. To create an instance of either of these lists, you use the new keyword and the following syntax.\n\n1 List<Integer> a = new ArrayList<Integer>();\n\n2 List<String> b = new LinkedList<String>();\n\nThe first line creates a new instance of an ArrayList that is parameterized to hold Integer types. The second creates a new instance of a LinkedList that has been parameterized to only hold String types. The parameterization is specified by placing the parameterized type inside the angle brackets. Because of this parameterization, it would be a compiler error to attempt to add anything other than Integers to the first list or anything other than Strings to the second.\n\nOnce these lists have been created, you can add and remove elements using the add() method.\n\n1 a.add(42);\n\n2 a.add(81);\n\n3 a.add(17);\n\n4\n\n5 b.add(\"Hello\");\n\n6 b.add(\"World\");\n\n7 b.add(\"Computers!\");\n\nThe order that you add elements is preserved, so in the first list, the first element would be 42, the second 81, and the last 17. You can remove elements by specifying an index of the element to remove. Like arrays, lists are 0-indexed.\n\n1 a.remove(0);\n\n2\n\n3 b.remove(2);\n\n4 b.remove(0);\n\nAs you remove elements, the indices are “shifted” down, so that after removing the first element in the list a, 81 becomes the new first element. Removing the last then the first element in the list b leaves it with only one element, \"World\" as the first element (at index 0). You can also retrieve elements from a list using 0-indexing and the get() method.\n\n1 List<Double> values = new ArrayList<Double>();\n\n2 values.add(3.14);\n\n3 values.add(2.71);\n\n4 values.add(42.0);\n\n5\n\n6 double x = values.get(1); //get the second value, 2.71\n\n7 double y = values.get(2); //get the third value, 42.0\n\nAny attempt to access an element that lies outside the bounds of the List, will result in an IndexOutOfBoundsException just as with arrays. To stay within bounds you can use the size() method to determine how many elements are in the collection. In this example, values.size() would return an integer value of 3. Finally, most collections implement the Iterable interface which allows you iterate over the elements using an enhanced for loop just as with arrays.\n\n1 for(Double x : values) {\n\n2 System.out.println(x);\n\n3 }\n\nThere are dozens of other methods that allow you to insert, remove, and retrieve elements from a Java List; refer to the documentation for details."

Related Ideas

How do Java lists provide dynamic data structures? | ComputerScienceOne | Bifalgorithm | Bifalgorithm