10.9. ArrayList ADT.pdf - 12/8/2019 10.9. ArrayList ADT...

This preview shows page 1 - 3 out of 11 pages.

The preview shows page 1 - 3 out of 11 pages.
12/8/201910.9. ArrayList ADT1/1110.9 ArrayList ADTThis section has been set as optional by your instructor.TheJava Collection Framework(or JCF) de±nes interfaces and classes for common ADTsknown as collections in Java. ACollectionrepresents a generic group of objects known aselements. Java supports several different Collections, including List, Queue, Map, and others.Refer toIntroduction to Collection InterfacesandJava Collections Framework overviewfromOracle's Java documentation for detailed information on each Collection type. Each Collectiontype is an interface that declares the methods accessible to programmers. TheListinterface isone of the most commonly used Collection types as it represents an ordered group of elements-- i.e., a sequence. Both an ArrayList and LinkedList are ADTs implementing the List interface.Although both ArrayList and LinkedList implement a List, a programmer should select theimplementation that is appropriate for the intended task. For example, an ArrayList offers fasterpositional access -- e.g.,myArrayList.get(2)-- while a LinkedList offers faster elementinsertion and removal.The ArrayList type is an ADT implemented as a class (actually as a generic class that supportsdifferent types such as ArrayList<Integer> or ArrayList<String>, although generics are discussedelsewhere).For the commonly-used public member functions below, assume an ArrayList de±ned as:ArrayList<T> arrayList = new ArrayList<T>();where T represents the ArrayList's type, such as:ArrayList<Integer> teamNums = new ArrayList<Integer>();Assume ArrayList teamNums has existing Integer elements of 5, 9, 23, 11, 14.Table 10.9.1: ArrayList ADT methods.get()T get(int index)Returns element at speci±ed index.x = teamNums.get(3);// Assignelement 3's value 11 to xset()T set(int index, T newElement)Replaces element at speci±ed index withnewElement. Returns element previously atspeci±ed index.
12/8/201910.9. ArrayList ADT2/11teamNums.set(0, new Integer(25));// Assigns 25 to element 0x = teamNums.set(3, 88);// Assigns 88 to element 3.// Assigns previous element's// value of 11 to x.size()int size()Returns the number of elements in theArrayList.if (teamNums.size() > 0){// Sizis 5 so condition is true...}isEmpty()boolean isEmpty()Returns true if the ArrayList does not containany elements. Otherwise, returns false.if (teamNums.isEmpty()){// Sizeis 5 so condition is false...}clear()void clear()Removes all elements from the ArrayList.teamNums.clear();// ArrayList now has no elementsSystem.out.println(teamNums.size()// Prints 0add()boolean add(T newElement)Adds newElement to the end of the ArrayList.ArrayList's size is increased by one.boolean add(int index, T newElement)Adds newElement to the ArrayList at thespeci±ed index. Elements at that speci±edindex and higher are shifted over to makeroom. ArrayList's size is increased by one.

Upload your study docs or become a

Course Hero member to access this document

Upload your study docs or become a

Course Hero member to access this document

End of preview. Want to read all 11 pages?

Upload your study docs or become a

Course Hero member to access this document

Term
Spring
Professor
N/A
Tags
Neutron, Chemical element

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture