Bifalgorithm post · Computer science one · Dynamic Data Structures

How do Java lists provide dynamic data structures?

How do Java lists provide dynamic data structures? (Computer science one · Dynamic Data Structures) — "The Java SDK provides a rich assortment of dynamic data…

Ideas in this flowchart

  • Java SDK dynamic data structures, especially Java Lists
  • Lists, sets, and maps provide dynamic alternatives to arrays
  • Their supporting classes are defined in the Collections library under the java.util package
  • The discussion focuses on using these structures, not on their implementation details or underlying OOP concepts
  • A Java List is an interface that specifies a dynamic list data structure
  • A List grows and shrinks automatically when elements are added or removed
  • As an interface, List specifies public methods but does not provide the implementation
  • Common List implementations include ArrayList and LinkedList
  • ArrayList uses an array to hold its elements
  • LinkedList stores its elements in linked nodes
  • Generic parameterization places the element type inside angle brackets, as in List<Integer> a = new ArrayList<Integer>() and List<String> b = new LinkedList<String>()
  • Parameterization causes a compiler error when an element has the wrong type
  • The add() and remove() methods modify Lists; insertion order is preserved, Lists are 0-indexed, and later indices shift after removal
  • The get() method retrieves elements, while size() reports the number of elements; an invalid index causes IndexOutOfBoundsException
  • The Iterable interface allows enhanced for loops to iterate over collection elements
  • Java Lists provide additional methods for inserting, removing, and retrieving elements; the documentation contains further details
Feed/

How do Java lists provide dynamic data structures?

No cover

Channel

Computer science one

1 page

"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."

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.